Book Image

QGIS Python Programming Cookbook

Book Image

QGIS Python Programming Cookbook

Overview of this book

Table of Contents (16 chapters)
QGIS Python Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. First, create a new DataSourceURI instance:

    uri = QgsDataSourceURI()
    
  2. Next, create the database connection string:

    uri.setConnection("spacialdb.com", "9999", "lzmjzm_hwpqlf", "lzmjzm_hwpqlf", "0e9fcc39")
    
  3. Now, describe the data source...