QGIS Python Scripting

Geomeetup SF

Alex Mandel
Geography Phd Candidate Davis, California 95616 blog.wildintellect.com tech@wildintellect.com

September 24, 2012

Creative Commons Attribution Share-alike Non-commercial

Quantum GIS

What is it?
A GUI based Desktop GIS application for data viewing, exploration, manipulation and output. A customizable and expandable GIS platform. GPL Licensed

What isn’t it?
It’s not a 100% everything you ever wanted to do with a GIS The only cartographic tool you need *(to be a pro cartographer) The only analysis tool you need *(to be a research scientist)

A Typical GIS

Features I care about
Data driven Symbology and Labelling including font, font size, rotation, multi-rule color selection. Vector data editing, by hand or by number Data editing by table or by custom forms WMS, WFS, WPS, WMTS, Postgis, Spatialite + anything GDAL/OGR A WMS Server (Optional QGIS Server) Raster Calculations Batch geoprocessing (Vector and Raster) Plugins, plugins and plugins... Reference FOSS4G 2011 workshop http://sourcepole.ch/foss4g/

QGIS at the Center of Everything

Some Plugins I Like

SEXTANTE - Analysis Tools Atlas - Batch map production QSpatialite, PostGIS Manager, RT Postgres Extractor Time Manager - Time Slider and video exporter OpenLayers - Load Google, OSM etc in the canvas ManageR - Pass data to/from R

Plugin Manager - Turn on/off plugins from Menu/Toolbars Fetch Python Plugins - Download and Update plugins from online repositories

Plugin Management

Getting Started

Plugin Builder - Build a skeleton for a new plugin Plugin Reloader - Reload plugin code without QGIS restart ScriptRunner - Test small python snippets from file Python Console - Interactive python in QGIS

Anatomy of Plugin
init .py Metadata PluginCode Core Functions not tied to interface specifics PluginDialog Interface Interaction, Middle Man PluginUI User Interface resources and images (logo.png)

Important Resources

QGIS Plugins Code http://hub.qgis.org/ QGIS Workshop http://www.qgisworkshop.org/ QGIS Python Cookbook http://www.qgis.org/pyqgis-cookbook/ QGIS API http://www.qgis.org/api/ Qt Docs doc.qt.digia.com PyQt Docs http://www.riverbankcomputing.co.uk/ static/Docs/PyQt4/html/classes.html

How to read the API & Qt Docs for Python

Example: Number of nodes in a Polygon for selected features in the active Layer QGIS Interface . activeLayer . selectedFeatures . featurelist . geometry . asPolygon
1 2 3 4



layer = qgis . utils . iface . activeLayer () featlist = layer . selectedFeatures () geom = featlist [0]. geometry () len ( geom . asPolygon () [0])
http://qgis.org/api/1.8/group__gui.html http://www.qgis.org/pyqgis-cookbook/vector.html


PyQt tips
Learn Signals & Slots http://www.riverbankcomputing.co.uk/static/Docs/ PyQt4/html/new_style_signals_slots.html pyuic4 and pyrcc4 - Every time you make a change to a plugin UI you need to run these.
pyuic4, converts a .ui file to a .py pyrcc4, bundles up things like icons into a single file p y u i c 4 −o m e t a e d i t U i . py m e t a e d i t . u i p y r c c 4 −o r e s o u r c e s r c . py r e s o u r c e s . q r c

Setting up a Dev Environment (Qt Designer, PyQT, Python)
Linux easy via package manager MAC use Kyngchaos binaries, must use Python native to OS X Windows OSGeo4w, osgeo4w shell and bat files to set paths are critical, no make shortcuts

Special Thanks
Tim Sutton, Pirmin Kalberer, Aaron Racicot, Gary Sherman QGIS Developers & Contributors Open Source Geospatial Foundation (OSGeo)
This presentation and others are available on http://www.scribd.com/wildintellect

Sign up to vote on this title
UsefulNot useful