This repository has been archived by the owner on Apr 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #25: Add geo.get_place_ids_in_radius.
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |