Skip to content

Instantly share code, notes, and snippets.

@byrney
Created December 15, 2016 23:10
Show Gist options
  • Save byrney/068e8fba690031e48aeeba1e11f2c61c to your computer and use it in GitHub Desktop.
Save byrney/068e8fba690031e48aeeba1e11f2c61c to your computer and use it in GitHub Desktop.
Add a postgis layer with a custom filter to QGIS
host='localhost'
port="5432"
db='rob'
user='rob'
password='password'
filter = "name = 'M5'"
uri = QgsDataSourceURI()
uri.setConnection(host, port, db, user, password)
uri.setDataSource("public", "dft_major_roads", "geom", filter ,"gid")
print uri.uri()
vlayer = QgsVectorLayer(uri.uri(), "M5", "postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment