Friday 26 July 2019

postgresql - Load PostGIS layer/table to QGIS canvas using Python


I want to load a PostGIS layer to QGIS canvas. I am using the following code but it is not loading the layer in canvas.


sql = "(SELECT * FROM table WHERE id = 20)"
uri = QgsDataSourceURI()
uri.setConnection("host", "5432", "databasename", "user", "password")

uri.setDataSource("public","table", "the_geom", sql,"gid")
print uri.uri()
vlayer = QgsVectorLayer(uri.uri(), "test", "postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
canvas.refresh()

But the layer is not loaded. I don't know what I am doing wrong here.




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...