I’ve been working on the data collection part of my cycle route modelling. I’m hoping that I can, as a first output, put together a map of where people are cycling in Melbourne. A crowd-sourced view of the best places to cycle, if you will. Given I will probably be running this in the cloud ((your recommendations for cloud-based services please, must be able to run Flask and PostGIS and be super cheap)), I thought it was best to actually store the data in a GIS database, rather than lots and lots of flat files.
A quick Google turned up GeoAlchemy, which are GIS extensions for SQLAlchemy. Provides lots of the standard things you want to do as methods on fields, but this is only a limited set of what you can do with PostGIS. Since I’m going to be wanting to do things like binning data, I thought it was worth figuring out how hard it was to call other PostGIS methods.
GeoAlchemy supports subclassing to create new dialects, but you have to subclass 3 classes, and it’s basically a pain in the neck when you just want to extend the functionality of the PostGIS dialect. Probably what I should do is submit a pull request with the rest of the PostGIS API as extensions, but I’m lazy. Henceforth, for the second time this week I am employing monkey patching to get the job done (and for the second time this week, kittens cry).
Functions in GeoAlchemy require two things, a method stub saying how we collect the arguments and the return (look at geoalchemy.postgis.pg_functions) and a mapping from this to the SQL function. Since we only care about one dialect, we can make this easier on ourselves by combining these two things. Firstly we monkeypatch in the method stubs:
from geoalchemy.functions import BaseFunction from geoalchemy.postgis import pg_functions @monkeypatchclass(pg_functions) class more_pg_functions: """ Additional functions to support for PostGIS """ class length_spheroid(BaseFunction): _method = 'ST_Length_Spheroid'
Note the _method attribute which isn’t something used anywhere else. We can then patch in support for this:
from geoalchemy.dialect import SpatialDialect @monkeypatch(SpatialDialect) def get_function(self, function_cls): """ Add support for the _method attribute """ try: return function_cls._method except AttributeError: return self.__super__get_function(function_cls)
The monkeypatching functions look like this:
def monkeypatch(*args): """ Decorator to monkeypatch a function into class as a method """ def inner(func): name = func.__name__ for cls in args: old = getattr(cls, name) setattr(cls, '__super__{}'.format(name), old) setattr(cls, name, func) return inner def monkeypatchclass(cls): """ Decorator to monkeypatch a class as a baseclass of @cls """ def inner(basecls): cls.__bases__ += (basecls,) return basecls return inner
Finally we can do queries like this:
>>> track = session.query(Track).get(1) >>> session.scalar(track.points.length_spheroid('SPHEROID["WGS 84",6378137,298.257223563]')) 6791.87502950043
Code on GitHub.