Python for GIS People


Sara Safavi  / @sarasomewhere

What is Python?


A programming language that's been around since 1991


Designed to be simple, beautiful, and accessible


Free and open source

What makes it Special?

    help is everywhere

    easy to get started

    extensible by design

Why do people love it?


"Python fits your brain"


Easy to read, easy to use: readability is a core philosophy


	#include <iostream>
		using namespace std;
		int main()
		    cout << "Hello World!";
		    return 0;


		public class HelloWorld {
		    public static void main(String[] args) {
		        System.out.println("Hello, World!");


	print("Hello, World!")

That's great... so what?

Why is Python good for GIS people?


  • Integrated into software you already use
  • Lots of existing geo-friendly tools
  • Widespread popularity ➜ community support

  • ...and programming is just fun!


General Python Concepts

Whitespace, not squiggles

Some languages use tokens like { } to create logical separations in the code...
Python just uses whitespace.

# Do this
if good_whitespace is True:
  print("Everything is awesome!")

# Don't do this
if good_whitespace is False:
print("This is broken")


What to remember: consistency is important!
A "tab" is not the same as "four spaces"

How it works

Python is interpreted - not compiled
Unfortunately, does not apply to Python...


Python code is saved to a text file with ".py" extension
Use the Python interpreter to run that code

How to start

What you need:


  • Python installed on your computer
  • A decent* text editor
  • Basic familiarity with the command line


*e.g., Notepad++, gedit, TextMate, etc.
Anything that won't mess with your whitespace.


(That's it!)

Running a Python program

Write this in a text editor, and save as

	print('Hello, world!')


Use Python from the command prompt to run your code:

	> python
	  Hello, world!

Learning: "Just Do It"

Hands-on practice is the best way to make progress:
you can learn a lot from making mistakes.


Tip: Python's official documentation is surprisingly easy to read!

How is it used in GIS today?

Bucket #1:
Non-Web things


Everything that's not geared for online consumption
(desktop-oriented work)

ETL fun


  • Fiona: use Python to read and write just about any kind of spatial vector data

  • Rasterio: same as above, but for rasters

  • Pyshp: lightweight Python library for reading & writing shapefiles only

  • Pyproj: Python tool for projecting spatial layers

  • GeoAlchemy: read & write to spatial databases like PostGIS, using Python


Note: a lot of ETL-like tools do geoprocessing too!

Shapely: After creating or extracting useful data, use this Python library for more complex manipulation & analysis.

  • Create buffers around features

  • Calculate convex hulls, unions, intersections, centroids...

  • Find spatial differences (change detection) on vector data

Combine Python tools like Fiona + Shapely to chain reading, converting, and analysing spatial data:

>>> with'city_parks.shp', 'r') as collection:
>>>     parks = [shapely.geometry.shape(c['geometry']) for c in collection]

>>> park = parks[0] # grab the first park out of the feature list

# ask Shapely what type of geometry this feature is
>>> park.type

>>> park.area

>>> park.buffer(10.0).area

>>> (park.centroid.x, park.centroid.y)
(3100374.119480808, 10106879.690095564)
use Fiona to open a shapefile of Austin city parks in Python,
then use Shapely to read & manipulate its geometry

Bucket #2:
Web Things


Getting spatial data online, making it mobile, or
putting it in the "cloud"

Generating web maps

What, I thought you needed to learn JavaScript for that?!

Folium: Python library that wraps wraps Leaflet.js
so you can use Python to generate web maps.


"Manipulate your data in Python, then visualize it
on a Leaflet map via Folium."


Your Python is now Javascript

14 lines of Python generates the HTML + JavaScript needed to display existing data on a web map

Building spatial-ized web applications

Web apps are interactive: taking input, storing data, producing output
Geodjango: create web apps that incorporate spatial data


GeoDjango lets you use a spatial database (e.g., PostGIS) to power a spatial-ized app: run spatial queries, return geometries, build maps

Making life better

Easy ways to start using Python as a GIS person

Get Fancy with the Field Calculator

Use Python to generate calculated field values:

Do it in QGIS with the FieldPyculator plugin

Check your work

Humans make mistakes: use Python to make QA/QC tools


  • Programmatically verify schemas & data consistency

  • Check for geometry issues (gaps, overlaps, slivers, etc)

  • Clean messy field values (mixed caps/lowercase, spaces vs. underscores, etc)



Let Python do repetitive tasks for you.

Life is too short to manually reproject 50 shapefiles,
then convert them all to GeoJSON...


Python makes a lot of things a lot easier

You don't have to be a programmer to take advantage

Easy to get started, endless possibilities!