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
About the Author
About the Reviewers

Using the __geo_interface__ protocol

The __geo_interface__ protocol is a new protocol created by Sean Gillies and is targeted mainly at Python to provide a string representation of geographic data following Python's built-in protocols. The string representation for geographic data is basically GeoJSON.


You can read more about this protocol at

Two developers, Nathan Woodrow and Martin Laloux, refined a version of this protocol for QGIS Python data objects. This recipe borrows from their examples to provide a code snippet that you can put at the beginning of your Python scripts to retrofit QGIS features and geometry objects with a __geo_interface__ method.

Getting ready

This recipe requires no preparation.

How to do it...

We will create two functions: one for features and one for geometry. We'll then use Python's dynamic capability to patch the QGIS objects with a __geo_interface__ built-in method. To do this, we need to perform the following steps:

  1. First...