Geographic Information Systems Asked on August 24, 2021
In my standalone script I want to use the SAGA tool "merge vector layers". According to the docs I need the following.
Thus, I wrote the following code to implement the tool:
def merge(self):
Parcel_channel2 = self.Parcel_Channel2()
intersection = self.intersection()
param = {'INPUT': [Parcel_channel2, intersection],
'MERGED': 'TEMPORARY_OUTPUT',
'SRCINFO': False,
'MATCH': True,
}
merge = processing.run('saga:mergevectorlayers', param)
vlayer = merge['MERGED']
QgsProject.instance().addMapLayer(vlayer)
return vlayer
When I try to run the script I get the following error message:
QgsProject.addMapLayer(): argument 1 has unexpected type ‘str’
With native Qgs tools, this code works fine. However, it seems that SAGA doesn’t return a VectorLayer but just the string in my ‘outDir’ variable. How do I get the output when using SAGA?
I’m using QGIS 3.10
Edit: I figured out, that native Qgs tools retun QgsMapLayer objects and saga tools return the path and name of a .shp file
C:/Users/denni/AppData/Local/Temp/processing_c0c1104bbc4447558e00113072ccc4d8/91e580e9c006429597540594cb000407/MERGED.shp
but I need a QgsMapLayer as result… how would I do that?
If you print the vlayer
variable, you will see it's a string referencing the QGIS layer path, so the correction would be the following (standalone, tested with only two separated points layers)
layers = QgsProject.instance().mapLayers()
param = {'INPUT': list(layers.values()),
'MERGED': 'TEMPORARY_OUTPUT',
'SRCINFO': False,
'MATCH': True,
}
merge = processing.run('saga:mergevectorlayers', param)
# Here, it's the layer path not a vector layer
layer_path = merge['MERGED']
# The main change to fix your issue
vlayer = QgsVectorLayer(layer_path, "My new merged layer", "ogr")
QgsProject.instance().addMapLayer(vlayer)
Correct answer by ThomasG77 on August 24, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP