pyqgis3 AggregateStreamSegments AttributeError: 'NoneType' object has no attribute 'getFeature'
Opened this issue · 0 comments
LouisManiere commented
C'est un problème dans l'utilisation de AggregateStreamSegments avec pyqgis.
AttributeError: 'NoneType' object has no attribute 'getFeature' est renvoyé quand fct:aggregatestreamsegments est exécuté sans que la couche soit chargée dans l'interface de QGIS.
Par exemple :
from qgis.core import *
from qgis.core import QgsApplication, QgsVectorLayer, QgsFeature, QgsGeometry, QgsField, QgsVectorDataProvider, QgsVectorFileWriter
from qgis.PyQt.QtCore import QVariant
import processing
# Create a memory layer for the Hydrographic network
layer_name = "HydrographicNetwork"
layer = QgsVectorLayer("LineString?crs=EPSG:2154", layer_name, "memory")
provider = layer.dataProvider()
# Add fields to the layer
provider.addAttributes([QgsField("id", QVariant.Int), QgsField("name", QVariant.String)])
# Start editing the layer
layer.startEditing()
# Add hydrographic features to the layer
hydrographic_features = [
(1, "River A", [QgsPointXY(0, 0), QgsPointXY(0, 100000), QgsPointXY(100000, 150000), QgsPointXY(200000, 100000)]),
(2, "River B", [QgsPointXY(200000, 100000), QgsPointXY(300000, 80000), QgsPointXY(400000, 120000)]),
(3, "River C", [QgsPointXY(200000, 100000), QgsPointXY(250000, 70000), QgsPointXY(300000, 50000)]),
(4, "River D", [QgsPointXY(300000, 50000), QgsPointXY(350000, 20000), QgsPointXY(400000, 15000)]),
]
for feature_id, name, coords in hydrographic_features:
geometry = QgsGeometry.fromPolylineXY(coords)
feature = QgsFeature()
feature.setGeometry(geometry)
feature.setAttributes([feature_id, name])
provider.addFeature(feature)
# Commit changes and stop editing
layer.commitChanges()
IdentifyNetworkNodes = processing.run('fct:identifynetworknodes',
{
'INPUT': layer,
'QUANTIZATION': 100000000,
'NODES': 'TEMPORARY_OUTPUT',
'OUTPUT': 'TEMPORARY_OUTPUT'
})['OUTPUT']
# A décommenter pour faire fonctionner aggregatestreamsegments
# QgsProject.instance().addMapLayer(IdentifyNetworkNodes)
AggregateSegment = processing.run('fct:aggregatestreamsegments',
{
'INPUT': IdentifyNetworkNodes,
'CATEGORY_FIELD' : '',
'COPY_FIELDS' : 'id',
'FROM_NODE_FIELD' : 'NODEA',
'TO_NODE_FIELD' : 'NODEB',
'OUTPUT' : 'TEMPORARY_OUTPUT'
})['OUTPUT']
QgsProject.instance().addMapLayer(AggregateSegment)
QgsProject.instance().addMapLayer(IdentifyNetworkNodes ) est à décommenter pour faire fonctionner aggregatestreamsegments.
Version de QGIS 3.28.13
FCT [1.0.11]
Merci!