[QGIS-Developer] error adding new feature to empy layer

pierluigi de rosa pierluigi.derosa at gmail.com
Thu Apr 4 00:58:08 PDT 2024


Dear All,
I have a simple case where I created an empty memory line layer in this way:

vl = QgsVectorLayer("LineString", "braiding_river", "memory")
vl.setCrs(crs)
pr = vl.dataProvider()
# add fields
pr.addAttributes([
    QgsField("reach", QVariant.String),
                  QgsField("braiding", QVariant.Double),
                  QgsField("Length", QVariant.Double)])
vl.updateFields()
feat_list=[]

then I populate the layer where inside a for cycle I have

fet = QgsFeature()
fet.setGeometry(QgsGeometry.fromPolylineXY(reach)) #reach is a list of
QgsPointXY
fet.setAttributes([ str(breakval),  brIndex,  lenReach])
feat_list.append(fet)

Outside for cycle I do:

(result, newFeatures) = pr.addFeatures(feat_list)

But I have always result to False so the feature is not added.

I think could be a problem related to the id of feature so I tried
also to use fet.setId() ad passing a specific unique value but the
result is the same.

can Anyone help me please?
Thanks
Pierluigi


-- 
Ing. Pierluigi De Rosa (PhD in Earth Science)
Contract Professor of Geographic Information System at University of Perugia
cel: 3497558268 / fax: 075 7823038
skype: pierluigi.derosa


More information about the QGIS-Developer mailing list