Stupid Geo Tricks

For Python* Developers

*(or Python-friendly)

make your dev-life better
with geojson

GeoJSON All the Things!
  • good for web
  • good for interchange
  • yay, standards

A Pythonista's BFF: __geo_interface__


  • JSONify spatial objects
  • Community standard
  • Implemented by all your favorite libraries


With __geo_interface__ in your toolbox, as long as you can read spatial data, you can write GeoJSON

Like pyshp?

import shapefile

def features(data):
  sf = shapefile.Reader(data)
  fields = sf.fields[1:]
  field_names = [field[0] for field in fields]

  for r in sf.shapeRecords():
    attr = dict(zip(field_names, r.record))
    geom = r.shape.__geo_interface__
    yield dict(geometry=geom, properties=attr)
>>> my_features = features(my_awesome_shapefile.shp)
# start with a shapefile, end with a geoJSON representation of each feature
See also: Shapely, fiona, mapnik... even arcpy*
*for practical purposes, in 10.2+ only

Python + pipelines
for fun & profit

Unix pipelines for regular people:

	$ cat my_awesome_document.txt | wc -l

	# returns number of lines in my_awesome_document.txt


(Feel free to just smile & nod here)
Unix pipelines for geo people:

	$ fio cat my_awesome_shapefile.shp | wc -l

	# returns number of spatial features in my_awesome_shapefile.shp



	$ fio cat my_red_shapefile.shp my_blue_shapefile.shp \
	  | fio load my_purple_shapefile.shp
joins two shapefiles together and creates a new shapefile

	$ fio cat boring_shapefile.shp | \
	  | fio load shiny_new_shapefile.shp
Pipe arbitrary input through Python scripts & save the output as new spatial data

What is that?!


Python library fiona's built-in CLI "fio"
fio's cat works like unix-y cat, with added spatial goodness


(For the raster nerds, check out Rasterio, which has a CLI "rio")

Version control: just do it


You're a developer. You use source control already*.


Stick spatial data in a repo for the same reason you do it to your code.




GeoGit GeoGig


Git for shapefiles & databases.

	$ geogig init
	$ geogig import my_awesome_shapefile.shp
	$ geogig add
	$ geogig commit -m "initial commit"
	$ geogig status
	  On branch master
	  # blah blah etc... 
	  # basically the git we all know & love... PLUS SPATIAL.
Keep on eye on them. Start playing around now.



GeoJSON + GitHub

  1. Push a GeoJSON file to GitHub.
  2. View your GeoJSON file on GitHub.
  3. There is no step three.

track data's history

Maps rendered automagically

Spatial diffs on GitHub

omgwtf github are you a wizard