Thursday 26 September 2019

pyqgis - Creating new Virtual Layer programmatically in QGIS?


Is it possible to create a Virtual Layer through a python script. For example, I have a layer "road", and I would like to perform sql " SELECT * FROM road WHERE type = 'Expressway' "


Will this be possible? Is there any example i can refer to?



Answer



You could use something like the following:



from qgis.core import QgsVectorLayer, QgsMapLayerRegistry

vlayer = QgsVectorLayer( "?query=SELECT * FROM road WHERE type = 'Expressway'", "vlayer", "virtual" )
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

You can find examples on how to use virtual layers through python from the author's GitHub:


https://github.com/mhugo/qgis_vlayers/blob/master/README.md


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...