QGIS Python Scripting
Geomeetup SF
Alex Mandel
Geography Phd Candidate Davis, California 95616 [Link] tech@[Link]
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 isnt it?
Its 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 [Link]
QGIS at the Center of Everything
Some Plugins I Like
[Link]
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/o 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 le Python Console - Interactive python in QGIS
Anatomy of Plugin
PluginFolder
init .py Metadata PluginCode Core Functions not tied to interface specics PluginDialog Interface Interaction, Middle Man PluginUI User Interface resources and images ([Link])
Important Resources
QGIS Plugins Code [Link] QGIS Workshop [Link] QGIS Python Cookbook [Link] QGIS API [Link] Qt Docs [Link] PyQt Docs [Link] static/Docs/PyQt4/html/[Link]
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])
[Link] [Link]
PyQt tips
Learn Signals & Slots [Link] 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 le to a .py pyrcc4, bundles up things like icons into a single le 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 les 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 [Link]