Skip to content
This repository has been archived by the owner on Apr 28, 2018. It is now read-only.

Commit

Permalink
Issue #25: Add geo.get_place_ids_in_radius.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcomella authored and liuche committed Feb 1, 2017
1 parent ef5e289 commit 01aef58
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def _findTablePrefix():
_venues = "venues"
_events = "events"
_searches = "searches"
_locations = "locations"

# number of seconds after a search in a given area that it should be cached.
searchCacheExpiry = 60 * 60 * 20 # s
Expand All @@ -32,6 +33,7 @@ def _findTablePrefix():
venuesTable = _tablePrefix + _venues
eventsTable = _tablePrefix + _events
searchesTable = _tablePrefix + _searches
locationsTable = _tablePrefix + _venues + '/' + _locations

statusTable = "api_availability"

Expand Down
39 changes: 39 additions & 0 deletions app/geo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Geo-related utility functions.
TODO:
- Should this be in a util folder?
- Inefficient to have a firebase instance for every file?
"""
from app.constants import locationsTable
from config import FIREBASE_CONFIG

import app.geofire as geofire
import pyrebase

_firebase = pyrebase.initialize_app(FIREBASE_CONFIG)


def get_place_ids_in_radius(center, radius_km, cached_locations_table=None):
"""Returns all place_ids in the DB within a radius from the given center.
:param center: is (lat, lng)
:param cached_locations_table: a direct pull of the geofire `.../location/` table; use this to avoid downloading the table again.
:return: A set of place ids within the given radius.
"""
if cached_locations_table is None:
location_table = _get_locations_table()
else:
location_table = cached_locations_table

out = set()
for place_id, vals in location_table.iteritems():
lat, lng = vals['l']
coord = (lat, lng)
if geofire.distance(center, coord) > radius_km:
continue
out.add(place_id)
return out


def _get_locations_table():
return _firebase.database().child(locationsTable).get().val()

0 comments on commit 01aef58

Please sign in to comment.