Loading a vector layer from a spatial database
The PostGIS geodatabase is based on the open source Postgres database. The geodatabase provides powerful geospatial data management and operations. PyQGIS fully supports PostGIS as a data source. In this recipe, we'll add a layer from a PostGIS database.
Getting ready
Installing and configuring PostGIS is beyond the scope of this book, so we'll use a sample geospatial database interface from the excellent service www.QGISCloud.com. www.QGISCloud.com has its own Python plugin called QGIS Cloud. You can sign up for free and create your own geodatabase online by following the site's instructions, or you can use the example used in the recipe.
How to do it...
Perform the following steps to load a PostGIS layer into a QGIS map:
- First, create a new
DataSourceURI
instance:uri = QgsDataSourceURI()
- Next, create the database connection string:
uri.setConnection("spacialdb.com", "9999", "lzmjzm_hwpqlf", "lzmjzm_hwpqlf...