You are on page 1of 13

QGIS Python Scripting

Geomeetup SF

Alex Mandel

Geography Phd Candidate Davis, California 95616

tech@wildintellect.com

September 24, 2012

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

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

QGIS at the Center of Everything

QGIS at the Center of Everything
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

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

PluginFolder

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

PluginDialog Interface Interaction, Middle Man PluginUI User Interface resources and images (logo.png)
PluginDialog Interface Interaction, Middle Man PluginUI User Interface resources and images (logo.png)

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 layer

 

=

qgis . utils . iface . activeLayer ()

2 featlist

=

layer . selectedFeatures ()

3 geom

=

featlist [0]. geometry ()

 

4 len ( geom . asPolygon () [0])

PyQt tips

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

pyuic4

pyrcc4

o

o

metaeditUi . py r e s o u r c e s

metaedit . ui r e s o u r c e s . qrc

r c . py

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

Source Geospatial Foundation (OSGeo) This presentation and others are available on http://www.scribd.com/wildintellect