def _guessYelpId(placeName, lat, lon): safePlaceId = hashlib.md5(placeName).hexdigest() cachedId = db.child(eventsTable).child("cache/" + safePlaceId).get().val() if cachedId: return cachedId opts = { # 'term': placeName, # Yelp does a bad job with term searching 'limit': 20, #'radius_filter': 1000, #'sort_by': 'distance', 'sort': 1, } r = yelpClient.search_by_coordinates(lat, lon, **opts) if len(r.businesses) > 0: location = (lat, lon) businessesWithCoords = filter( lambda b: (b.location is not None) and (b.location.coordinate is not None), r.businesses) biz = min(businessesWithCoords, key=lambda b: geo.distance(location, (b.location.coordinate.latitude, b.location.coordinate.longitude)) ) log.debug("%s --> %s" % (placeName, biz.name)) researchVenue(biz) # Add bizId to cache record = { "cache/" + safePlaceId: str(biz.id) } db.child(eventsTable).update(record) return biz.id else: log.info("Can't find %s" % placeName) return None
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 place_ids_in_radius = set() for place_id, vals in location_table.items(): lat, lng = vals['l'] coord = (lat, lng) if geofire.distance(center, coord) <= radius_km: place_ids_in_radius.add(place_id) return place_ids_in_radius
def findSearchRecord(center, radius=1000): import app.geofire as geo import time queries = geo.geohashQueries(center, radius) now = time.time() for query in queries: results = db.child(searchesTable).order_by_key().start_at(query[0]).end_at(query[1]).get() for result in results.each(): record = result.val() if record.get("time", 0) + searchCacheExpiry < now: db.child(searchesTable).child(result.key()).remove() continue # double check that we're within distance circleDistance = geo.distance(center, record["l"]) * 1000 # 1000 m in 1 km (geo.distance is in km, searchCacheRadius is in m) if circleDistance < searchCacheRadius: return record log.info("Circle distance is " + str(circleDistance))
venueData = venueTableComplete["details"] cacheData = venueTableComplete["cache"] import json stats = { "website": 0, "TOTAL": 0, } factualStats = {} for venue in venueData.values(): yelpID = venue["id"] coord = venue["coordinates"] coord = (coord["lat"], coord["lng"]) if geo.distance(coord, focus) > 500: # 500 km continue identifiers = None try: identifiers = cacheData[yelpID]["identifiers"] except KeyError: identifiers = dict() for p in identifiers.keys(): factualStats[p] = factualStats.get(p, 0) + 1 print("%.8f, %.8f" % coord) providers = venue.get("providers", {}).keys() for p in providers: stats[p] = stats.get(p, 0) + 1
# Also: this does not attempt to wrap around, or top out at the top or bottom of the globe. # i.e. this does not generalize to any bounded box on the planet. def grid_points(north_lat, west_lng, south_lat, east_lng, grid_size_m=100.0): lat, lng = (south_lat, west_lng) while lat < north_lat: lng = west_lng while lng < east_lng: yield (lat, lng) lng += geo.metersToLongitudeDegrees(grid_size_m, lat) lat_delta = grid_size_m / geo.g_METERS_PER_DEGREE_LATITUDE lat += lat_delta grid = grid_points(north_lat, west_lng, south_lat, east_lng, grid_size_m) grid = [focus] + sorted(grid, key=lambda pt: geo.distance(pt, focus)) office_locations = [ (50.815078, -0.137089), # brighton (51.385114, -0.008778), # croydon # (-15.5095764,167.1841149), # vanuatu, lol not really. (43.6472912, -79.3966112), # toronto (37.7895639, -122.3911524), # san francisco (49.2824693, -123.1113848), # vancouver (52.5417121, 13.3869854), # berlin (48.8721423, 2.3389602), # paris (51.5045923, -0.0992805), # london (45.5234539, -122.6848713), # portland (37.387319, -122.0622035), # mountain view (35.6652311, 139.725706), # tokyo ]