I am working on a QGIS plugin for which I am iterating over an inputlayer (point) and requesting a responselayer (polygon) for each inputfeature. The responselayer then contains several responsefeatures for each inputfeature.
I am trying to create a new vector layer having the geometry of each responsefeature and its attributes. Also the inputfeatures attributes shall be copied over.
So far I have managed to create the new vector layer and adding the inputfeatures x times to it. But I am struggling to set the geometry and attributes of it to the responselayer's ones.
This is my relevant code:
# creating outputlayer
Isochrones_Memorylayer_VL = QgsVectorLayer("Polygon?crs=epsg:4326", "Isochrones", "memory") # Create temporary polygon layer (output file)
Isochrones_Memorylayer_PR = Isochrones_Memorylayer_VL.dataProvider()
Isochrones_Memorylayer_VL.startEditing()
Isochrones_Memorylayer_PR.addAttributes(isochrones_selectedLayer.fields()) # Copy all fieldnames of inputlayer to outputlayer
Isochrones_Memorylayer_PR.addAttributes([QgsField("Isochrone_UID", QVariant.Int),QgsField("Isochrone_Error", QVariant.String),QgsField("Isochrone_URL", QVariant.String),QgsField("Isochrone_Time",QVariant.Int)]) # Add Error and URL Field to outputlayer
Inputlayer_outFeat = QgsFeature() # set QgsFeature
# iterating through input layer
for Inputlayer_Feature in Inputlayer_Features:
# doing stuff
# e.g. requesting response layer having several features for each feature of input layer
# iterating through response layer
for feat in Isochrone_Feature:
Isochrones_Memorylayer_PR.addFeatures([Inputlayer_outFeat]) # Add attributes of inputlayer to outputlayer
#Isochrones_Memorylayer_PR.addFeature(feat)
#Isochrones_Memorylayer_PR.changeAttributeValues(feat.id(), 2, 30)
#Isochrones_Memorylayer_PR['Isochrone_URL'] = str(isochrone_url)
#Isochrones_Memorylayer_PR.changeFeatures(feat.geometry())
As you can see the second loop is a mess of a few things I have tried. I think the way to go should be changeAttributeValues and changeGeometryValues, but from the docs I have no idea how to actually use it. An example would help here. I already took a look at the cookbook. The only question with an example I can find is six years old and doesn't help solving my question. When trying the answer, I am getting TypeError: QgsVectorDataProvider.changeAttributeValues(): argument 1 has unexpected type 'int'
. Maybe due to changes in the API? But since I am still a beginner in pyqgis it could also be some simple stuff.
How can I set the geometry of my new vector layer to the responselayers geometry and add the responselayers attributes (only "time", the other attributes like url, error, etc. are generic) to it?