PyQgis: Attribute field does not get added

18 Views Asked by At

My script add fields to a pointlayer. This happens while iterating a polygon layer. For each feature I look for the closest point from a dataset. This point gets written to a pointlayer (REL_LAWIS_profiles). Code:

## D.4 Write LAWIS snowprofile layer
lawislayer = QgsVectorLayer('Point?crs = epsg:4326', 'lawisprofiles', 'memory')
lawislayer_path = str(outpath_REL + "lawis_layer.shp")
_writer = QgsVectorFileWriter.writeAsVectorFormat(lawislayer,lawislayer_path,'utf-8',driverName='ESRI Shapefile')
REL_LAWIS_profiles = iface.addVectorLayer(lawislayer_path,"REL_LAWIS_profiles", "ogr")
      
## D.5 LAWIS snowprofile layer write attribute fields
lawisprovider = REL_LAWIS_profiles.dataProvider()
lawisprovider.addAttributes([QgsField("REL_ID", QVariant.String),#QVariant.String
                QgsField("ID", QVariant.Int),
                QgsField("NAME", QVariant.String),
                QgsField("DATE", QVariant.String),
                QgsField("ALTIDUDE", QVariant.Int),
                QgsField("ASPECT", QVariant.String),
                QgsField("SLOPE", QVariant.Int),
                QgsField("SD", QVariant.Int),
                QgsField("ECTN1", QVariant.Int),
                QgsField("ECTN2", QVariant.Int),
                QgsField("ECTN3", QVariant.Int),
                QgsField("ECTN4", QVariant.Int),
                QgsField("COMMENTS", QVariant.String),
                QgsField("PDF", QVariant.String)])
REL_LAWIS_profiles.updateFields()

## get layer for data collection
lawis_Pts = QgsProject.instance().mapLayersByName('REL_LAWIS_profiles')[0]

## look for closest point and get data for fields....

In a second step features are added and values get assigned to the fields:

## GET FIELD ID FROM lawis_pts
rel_id_idx = feat.fields().lookupField('REL_ID') # feat because inside a for loop of anotehr layer
id_idx = lawis_Pts.fields().lookupField('ID')
name_idx = lawis_Pts.fields().lookupField('NAME')
date_idx = lawis_Pts.fields().lookupField('DATE')
l_alti_idx = lawis_Pts.fields().lookupField('ALTIDUDE')
l_aspect_idx = lawis_Pts.fields().lookupField('ASPECT')
l_slo_idx = lawis_Pts.fields().lookupField('SLOPE')
l_sd_idx = lawis_Pts.fields().lookupField('SD')
l_ectn_idx1 = lawis_Pts.fields().lookupField('ECTN1')
l_ectn_idx2 = lawis_Pts.fields().lookupField('ECTN2')
l_ectn_idx3 = lawis_Pts.fields().lookupField('ECTN3')
l_ectn_idx4 = lawis_Pts.fields().lookupField('ECTN4')
com_idx = lawis_Pts.fields().lookupField('COMMENTS')
pdf_idx = lawis_Pts.fields().lookupField('PDF')

## ADD FEATURES TO lawis_Pts.
lawis_Pts.startEditing()
lawisfeat = QgsFeature()
lawisfeat.setGeometry( QgsGeometry.fromPointXY(QgsPointXY(lawisprofile_long,lawisprofile_lat)))
lawisprovider.addFeatures([lawisfeat])
lawis_Pts.commitChanges()

## CHANGE VALUES OF SELECTED FEATURE
lawis_Pts.startEditing()
for lfeat in selection:
        lawis_Pts.changeAttributeValue(lfeat.id(), rel_id_idx, REL_LAWIS_ID)
        lawis_Pts.changeAttributeValue(lfeat.id(), id_idx, LAWIS_id)
        lawis_Pts.changeAttributeValue(lfeat.id(), name_idx, LAWIS_NAME)
        lawis_Pts.changeAttributeValue(lfeat.id(), date_idx, LAWIS_DATE)
        lawis_Pts.changeAttributeValue(lfeat.id(), l_alti_idx, LAWIS_ALTIDUDE)
        lawis_Pts.changeAttributeValue(lfeat.id(), l_aspect_idx, LAWIS_ASPECT)
        lawis_Pts.changeAttributeValue(lfeat.id(), l_slo_idx, LAWIS_SLOPE)
        lawis_Pts.changeAttributeValue(lfeat.id(), l_sd_idx, LAWIS_SD)
        lawis_Pts.changeAttributeValue(lfeat.id(), l_ectn_idx1, LAWIS_ECTN1)
        lawis_Pts.changeAttributeValue(lfeat.id(), l_ectn_idx2, LAWIS_ECTN2)
        lawis_Pts.changeAttributeValue(lfeat.id(), l_ectn_idx3, LAWIS_ECTN3)
        lawis_Pts.changeAttributeValue(lfeat.id(), l_ectn_idx4, LAWIS_ECTN4)
        lawis_Pts.changeAttributeValue(lfeat.id(), com_idx, LAWIS_COMMENTS)
        lawis_Pts.changeAttributeValue(lfeat.id(), pdf_idx, LAWIS_PDFlink) 
lawis_Pts.commitChanges()

In the table the layer has 14 fields, but if the values are not written to the fields. I checked values but no issue there. Then i checked if all fields exists and field Nr 12 does (at least for python) not exist. With this:

fields = lawis_Pts.fields()
for field in fields:
    print(field.name())

I checked right after adding the fields if all fields get added. But there are only 13 fields (REL_ID,ID,NAME,DATE,ALTIDUDE,ASPECT,SLOPE,SD,ECTN1,ECTN2,ECTN3,COMMENTS,PDF). So I found out the problem is ECTN4. Also checken the Idx of ECTN4 by

print(l_ectn_idx4)

which gave me -1, which means it's not existing. But If I remove the layer which is added by the script and then add the layer manually and look for the field it is there, also using code. I assume there has to be a problem how I add the layer, but I just can't find the reason for this behavior. I'm thankful for any ideas!

0

There are 0 best solutions below