This document describes a GeoJSON-like protocol for geospatial vector data.
Python has a number of built-in protocols (descriptor, iterator, etc). A very simple one involves string representations of objects. The built-in str()
function delegates to the __str__()
method of its single argument. Example:
>>> class A(object): ... def __str__(self): ... return "Eh!" ... >>> a = A() >>> str(a) 'Eh!' >>> "%s" % a 'Eh!'
By implementing __str__()
, instances of any class can be printed by any other Python program. What if we could do something like this for geospatial (GIS) objects? It might, for example, let any object be analyzed using Shapely [1]
as simply as:
>>> from shapely.geometry import shape >>> shape(obj).buffer(1.0).area # obj is a "point" of some kind 3.1365484905459389
Shapely's shape()
function would access relevant data of its single argument using an agreed upon method or attribute.
Following the lead of numpy's Array Interface [2], let's agree on a __geo_interface__
attribute. To avoid creating
even more protocols, let's make the value of this attribute a Python mapping. To further minimize invention, let's
borrow from the GeoJSON format [3] for the structure of this mapping.
[1] | https://github.com/Toblerity/Shapely |
[2] | http://docs.scipy.org/doc/numpy/reference/arrays.interface.html |
[3] | http://geojson.org |
Pretty cool. Would be good to implement this into pyqgis too.