Skip to content

Instantly share code, notes, and snippets.

@abrookins abrookins/gist:5479607

Last active Dec 16, 2015
What would you like to do?
Example code for in-memory proximity search with Python (2).
import flask
from flask.ext.cache import Cache
from siren import util, crime_tracker
from siren.decorators import jsonp
app = flask.Flask(__name__)
app.config.from_envvar('SIREN_SETTINGS', silent=True)
cache = Cache(app)
crime_db = crime_tracker.PortlandCrimeTracker()
def get_point(latitude, longitude):
Return ``latitude`` and ``longitude`` coerced to floats.
Fail the request with a 400 status if one or both are not coercible.
latitude = float(latitude)
longitude = float(longitude)
except ValueError:
return latitude, longitude
def get_crimes(latitude, longitude):
Return all crimes found within 1/2 mile of ``latitude`` and ``longitude``.
point = get_point(latitude, longitude)
ignore = ['callback', '_']
filters = {k: v for k, v in flask.request.args.items() if k not in ignore}
return crime_db.get_crimes_nearby(point, filters)
def crime_stats(latitude, longitude):
Return statistics -- right now, only sums by category -- about crimes
within 1/2 mile of ``latitude`` and ``longitude``.
nearby_crimes, errors = get_crimes(latitude, longitude)
stats = crime_db.get_stats_for_crimes(nearby_crimes)
data = {'stats': stats}
if errors:
data['errors'] = errors
return flask.jsonify(result=data)
def crimes(latitude, longitude):
Return raw crime data for all crimes within 1/2 mile of ``latitude`` and
crimes, errors = get_crimes(latitude, longitude)
# JSON requires that object keys be strings, so convert tuples to strings
# like "37.785834,-122.406417"
crimes = [dict(crime=c, point=','.join(str(p))) for p, c in crimes.items()]
data = {'crimes': crimes}
if errors:
data['errors'] = errors
return flask.jsonify(result=data)

This comment has been minimized.

Copy link

jhagege commented Sep 29, 2014

I just found your code and I am trying to do a very similar task, but I am having a lot of issues with reconstructing the kd-tree using a task queue for distributed processing, as this is in-memory data structure.
I would really much appreciate if you can have a short-look at this question I askedon stackoverflow.

Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.