Fast In-memory spatial radius queries with Python

Alexander Mueller
7 min readAug 9, 2017

Efficient geo-spatial radius queries on a given dataset are a problem that we’re very regularly facing. Usually, we handle the problem by spinning up a PostgreSQL database and installing PostGIS with all its nice and efficient spatial indexing capabilities. Recently, however, we decided to investigate some other possibilities to do fast geospatial radius queries in-memory to get some load off our live Postgres instances. In this blog post, I want to share three alternatives that we discovered during our search and benchmark them against each other.

Our Use Case

Map illustrating the Use Case (© for the map goes to OpenStreetMap)

The general problem is that we want to get points that are within a radius of a given spatial event (see figure above). So we’re looking for a function in the form of:

def radius_search(point_database,spatial_event,radius)->list

Where the point_database is a list of tuples containing geo-coordinates latitude and longitude. The same goes for the spatial_event variable, which is also just a tuple containing latitude and longitude. The radius is the maximum allowed distance (in meters) between the spatial_event and the returned list of nearby points.

Because the code will work with long-lived objects that are maybe updated once or twice a day I mostly care about the query runtime while evaluating…

--

--

Alexander Mueller

AI Entrepreneur, Deep Learning & Python Enthusiast, Founder of workist