I am working on a script that runs a model and then appends the result to a spatialite layer. The model runs zonal statistics using 4 different rasters and appends them to the attribute table of a vector polygon layer with a single feature. The attribute table of the model output (habitatModel
) looks as follows:
id | hs90_mean | hs14_mean | ls90_mean | ls14_mean
----+-----------+-----------+-----------+----------
1 | 46.043816 | 44.009554 | 47.859847 | 45.308090
The type of the id
field is integer, and I need to replace that with a string identifier, so I load the layer as a QgsVectorLayer
, add a genspec
field and delete the id
field:
habitatModelLayer = QgsVectorLayer(habitatModel,genspec,'ogr')
habitatModelLayer.dataProvider().addAttributes([QgsField("genspec", QVariant.String)])
habitatModelLayer.dataProvider().deleteAttributes([habitatModelLayer.fields().lookupField('id')])
habitatModelLayer.updateFields()
Then I try to update the attribute values using dataprovider().changeAttributeValues()
:
iterator = habitatModelLayer.getFeatures(QgsFeatureRequest().setFilterFid(0))
feature = next(iterator)
attrs = {habitatModelLayer.fields().lookupField('genspec'):'2759-9',
habitatModelLayer.fields().lookupField('hs90_mean'):feature['hs90_mean'],
habitatModelLayer.fields().lookupField('ls90_mean'):feature['ls90_mean'],
habitatModelLayer.fields().lookupField('hs14_mean'):feature['hs14_mean'],
habitatModelLayer.fields().lookupField('ls14_mean'):feature['ls14_mean']}
habitatModelLayer.dataProvider().changeAttributeValues({0:attrs})
habitatModelLayer.commitChanges()
I then add the layer to the spatialite file database using qgis:importintospatialite
:
processing.run("qgis:importintospatialite", {
'INPUT':habitatModelLayer,
'DATABASE':spatialiteLayer,
'TABLENAME':'models',
'PRIMARY_KEY':'genspec',
'GEOMETRY_COLUMN':'geom',
'ENCODING':'UTF-8',
'OVERWRITE':True,
'CREATEINDEX':True,
'LOWERCASE_NAMES':False,
'DROP_STRING_LENGTH':False,
'FORCE_SINGLEPART':False
})
The resulting layer has the correct values for the zonal stats, but the genspec field is empty. Any idea why this would be?
A strange side-note is that when the habitatModel
layer is added to the canvas as a result of the script, it initially still shows the new attributes squashed into the old fields (and trying to conform to the old types):
id | hs90_mean | hs14_mean | ls90_mean | ls14_mean
----+-----------+-----------+-----------+----------
46 | 44.009554 | 47.859847 | 45.308090 | 2795
but after a few minutes the attribute table corrects itself:
hs90_mean | hs14_mean | ls90_mean | ls14_mean | genspec
-----------+-----------+-----------+-----------+---------
46.043816 | 44.009554 | 47.859847 | 45.308090 | 2759-9
Is there some kind of buffer that needs to be flushed before I write to the spatialite database?