Skip to content

Instantly share code, notes, and snippets.



Last active Dec 16, 2015
What would you like to do?
Example code for in-memory proximity search with Python (1).
from scipy.spatial import cKDTree
from scipy import inf
# ... Some code removed for clarity ...
class PortlandCrimeTracker(object):
def __init__(self, db_filename=DEFAULT_DATABASE_NAME):
crime_db = self.load_crimes_db(db_filename)
self.crimes = crime_db['crimes']
self.points = self.crimes.keys()
self.crime_kdtree = cKDTree(self.points)
def get_points_nearby(self, point, max_points=250):
Find the nearest points within 1/2 a mile of the tuple ``point``, to a
maximum of ``max_points``.
# Find crimes within approximately 1/2 a mile. 1/4 mile is .005,
# 1/2 mile is .01, full mile is .02.
distances, indices = self.crime_kdtree.query(point, k=max_points,
point_neighbors = []
for index, max_points in zip(indices, distances):
if max_points == inf:
return point_neighbors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment