Skip to content

Instantly share code, notes, and snippets.

@abrookins
Last active December 16, 2015 18:39
Show Gist options
  • Save abrookins/5479607 to your computer and use it in GitHub Desktop.
Save abrookins/5479607 to your computer and use it in GitHub Desktop.
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.
"""
try:
latitude = float(latitude)
longitude = float(longitude)
except ValueError:
flask.abort(400)
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)
@app.route('/crime/stats/<latitude>,<longitude>')
@cache.cached()
@jsonp
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)
@app.route('/crime/<latitude>,<longitude>')
@cache.cached()
@jsonp
def crimes(latitude, longitude):
"""
Return raw crime data for all crimes within 1/2 mile of ``latitude`` and
``longitude``.
"""
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)
@jhagege
Copy link

jhagege commented Sep 29, 2014

Hello,
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.
http://stackoverflow.com/questions/26088868/asynchronous-task-queue-processing-of-in-memory-data-stucture-in-django?noredirect=1#comment40883596_26088868

Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment