def get_places_list(location, keywords, radius_mi): food_client = GoogleFood() success = food_client.create_list(location, keywords, radius_mi) if success: return food_client.get_result() else: assert False return None
def get_places_list(self, location, keywords, radius_mi): if self.food_client == None: self.food_client = GoogleFood() success = self.food_client.create_list(location, keywords, radius_mi) if success: return self.food_client.get_result() else: assert False
class FoodGeospacial: def __init__(self): self.db = None self.db_client = None self.geocoder = None self.food_client = None def get_places_list(self, location, keywords, radius_mi): if self.food_client == None: self.food_client = GoogleFood() success = self.food_client.create_list(location, keywords, radius_mi) if success: return self.food_client.get_result() else: assert False def reset_db(self): assert self.db != None self.db.places.drop() print "Dropped places collection from database." def query_db_lng_lat_keywords(self, lng, lat, radius_mi, keywords): assert type(lng) is int or type(lng) is float assert type(lat) is int or type(lat) is float assert type(radius_mi) is int or type(radius_mi) is float assert type(keywords) is str assert self.db != None query = SON( [ ("geoNear", "places"), ("near", {"type": "Point", "coordinates": [lng, lat]}), ("spherical", "true"), ("query", {"keywords": {"$regex": ".*%s.*" % keywords}}), ("maxDistance", radius_mi * constants.METERS_PER_MILE), ] ) i = 0 for result in self.db.command(query)["results"]: print ( "%s: %s - keywords '%s'" % (result["obj"]["name"], result["obj"]["formatted_address"], result["obj"]["keywords"]) ) i += 1 print "" print "Found %d documents." % i print "" def query_db_lng_lat(self, lng, lat, radius_mi): assert type(lng) is int or type(lng) is float assert type(lat) is int or type(lat) is float assert type(radius_mi) is int or type(radius_mi) is float assert self.db != None query = SON( [ ("geoNear", "places"), ("near", {"type": "Point", "coordinates": [lng, lat]}), ("spherical", "true"), ("maxDistance", radius_mi * constants.METERS_PER_MILE), ] ) i = 0 for result in self.db.command(query)["results"]: print ( "%s: %s - keywords '%s'" % (result["obj"]["name"], result["obj"]["formatted_address"], result["obj"]["keywords"]) ) i += 1 print "" print "Found %d documents." % i print "" def load_db(self, location, keywords, radius_mi): assert type(location) is str assert type(keywords) is str assert type(radius_mi) is int or type(radius_mi) is float i = 0 dups = 0 print "Searching Google Places in a %d mile radius around %s with keywords '%s'..." % ( radius_mi, location, keywords, ) place_list = self.get_places_list(location, keywords, radius_mi) for k, v in place_list.items(): # print "%s: %s\n" % (k,v) cursor = self.db.places.find({"place_id": v["place_id"]}) if cursor.count() == 0: self.db.places.insert( { "loc": { "type": "Point", "coordinates": [float(v["lat_lng"]["lng"]), float(v["lat_lng"]["lat"])], }, "keywords": keywords, "name": v["name"], "place_id": v["place_id"], "formatted_address": v["formatted_address"], "local_phone_number": v["local_phone_number"], "international_phone_number": v["international_phone_number"], "website": v["website"], "url": v["url"], } ) i += 1 else: dups += 1 print "Added %d locations to database." % i if dups > 0: print "Found %d duplicate locations already in local database." % dups def connect_db(self): if self.db_client == None: try: self.db_client = MongoClient() except Exception: print "Could not connect to MongoDB" return False self.db = self.db_client.test_database # # Create a Compound Index with 2dsphere index key. # self.db.places.ensure_index([("loc", pymongo.GEOSPHERE)]) return True def address_to_lat_long(self, address): if self.geocoder == None: self.geolocator = GoogleV3() location = self.geolocator.geocode(address) return location.longitude, location.latitude def query_db_addr(self, address, radius_mi): assert self.db != None lng, lat = self.address_to_lat_long(address) print "Searching DB for %.2f mile radius centered around %s" % (radius_mi, address) print "" self.query_db_lng_lat(lng, lat, radius_mi) print "" def query_db_addr_keywords(self, address, radius_mi, keywords): assert type(radius_mi) is int or type(radius_mi) is float assert self.db != None lng, lat = self.address_to_lat_long(address) print "Searching DB for %.2f mile radius centered around %s with keywords '%s'" % (radius_mi, address, keywords) print "" self.query_db_lng_lat_keywords(lng, lat, radius_mi, keywords) print ""