4

Have a point layer (from geopackage) with value ralations widget in several columns. Trying to add new feature to this layer with the following code:

import uuid
layers = QgsProject.instance().mapLayersByName('L10_Public--Point')
layer = QgsVectorLayer(layers[0].dataProvider().dataSourceUri(), '', 'ogr')
print(layer)

caps = layer.dataProvider().capabilities()

if caps & QgsVectorDataProvider.AddFeatures:
    feat = QgsFeature(layer.fields())
    feat.setAttributes([2, 'Public', NULL, str(uuid.uuid4()), str(uuid.uuid4()), '602010904', NULL, NULL, '97629445', NULL, 5.0, 2.0, 3.0, 4.0, NULL, NULL, 4.0, NULL, NULL, NULL, NULL, 4.0, 4.0])
    feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(1605057.281,645118.237)))
    res, outFeats = layer.dataProvider().addFeatures([feat])

And getting:

Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 2, in <module>
IndexError: list index out of range

Should be mentionel that the same code works well for another layer, which has no widgets in columns. Anybody knows what IndexError means here? Or how can I programmatically add new features in such a layer?

  • If you remove the relation (temporarily, to debug), does it works? From the error, I suspect your issue doesn't come from the relation but of attributes not matching the number of fields you declare. It could also come from res, outFeats = layer.dataProvider().addFeatures([feat]): You are unpacking a return that is not a list but a boolean e.g qgis.org/api/… – ThomasG77 Jun 15 at 13:34
  • Thank you, @ThomasG77. I should mention - original layer was not SHP, but from geopackage... May be this is a main reason? After I removed value relations and saved it as SHP, I managed to add new feature by this script. Problem, I suppose, is not in the number of fields, because list of values was taken from original layer. – Burtsev Jun 15 at 14:53
4

IndexError: list index out of range means that you are trying to access an element of the list which does not exist. That's, layers list is empty, there is no a layer named L10_Public--Point in your project or somehow layers = QgsProject.instance().mapLayersByName... should be returning empty list.

| improve this answer | |
  • Thank you, @Kadir Şahbaz, I really made a stupid mistake (Public instead of PUBLIC) in the layer name. Now I'm getting no IndexError, but seems that script makes no changes in the layer... No new features appeared (( – Burtsev Jun 15 at 15:14
  • @Burtsev Try to refresh map screen and reopen attribute table. – Kadir Şahbaz Jun 15 at 15:16
  • Already tryed. Same result – Burtsev Jun 15 at 15:54
  • Finally succeed! Last bug was with the attributes I took for new feature, which were the same as an existing feature of this layer has. I just can't distinguish an old and the new one ))) – Burtsev Jun 15 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.