def create_geo(locations): # Create new entries into the Geo entity in the datastore. for location in locations: geo = Geo(location=location, geo='None') geo.put() logging.info('New locations have been added to the datastore: %s' % locations)
def geocode(): query = Geo.all() query.filter('geo =', 'None') results = query.fetch(1) for result in results: try: # If the location format is already a Geo string lat, lon = result.location.split(",") lat = float(lat) lon = float(lon) result.geo = "%s,%s" % (lat, lon) result.put() break except: pass # Prepare and fire a request to the Google Maps API form_fields = {'q': result.location.encode('utf-8'), 'output': 'csv', 'key': 'ABQIAAAAXG5dungCtctVf8ll0MRanhR9iirwL7nBc9d2R7_tFiOfa5aC4RSTKOF-7Bi7s8MaO5KAlewwElCpIA'} form_data = urllib.urlencode(form_fields) google_maps = urlfetch.fetch(url='http://maps.google.com/maps/geo?' + form_data) if google_maps.status_code == 200 and len(google_maps.content): try: # Parse and record the result if it's valid status, n, lat, lon = google_maps.content.split(',') result.geo = "%s,%s" % (lat, lon) result.put() except: pass
'name': location, 'lat': 0, 'lon': 0, 'users': [follower] } # Loop through all the locations and query the Datastore for their # Geo points (latitude and longitude) which could be pointed out # on the map. new_locations = [] for location in locations.keys(): if not location: del locations[location] continue query = Geo.all() query.filter('location =', location) g = query.get() # We don't want empty locations, neither do we want locations # recorded as None, which are temporary stored until the cron # jobs resolves them into valid geo points. if g: if g.geo != 'None': if g.geo != '0,0': # If a location exists, we set its lat and lon values. locations[location]['lat'], locations[location][ 'lon'] = g.geo.split(',') else: # If the location's geo address is 0,0 we remove it from the locations list # since we don't want to show irrelevant locations.
location = location.replace('\n', ' ').strip() if location in locations: locations[location]['users'].append(follower) else: locations[location] = {'name': location, 'lat': 0, 'lon': 0, 'users': [follower]} # Loop through all the locations and query the Datastore for their # Geo points (latitude and longitude) which could be pointed out # on the map. new_locations = [] for location in locations.keys(): if not location: del locations[location] continue query = Geo.all() query.filter('location =', location) g = query.get() # We don't want empty locations, neither do we want locations # recorded as None, which are temporary stored until the cron # jobs resolves them into valid geo points. if g: if g.geo != 'None': if g.geo != '0,0': # If a location exists, we set its lat and lon values. locations[location]['lat'], locations[location]['lon'] = g.geo.split(',') else: # If the location's geo address is 0,0 we remove it from the locations list # since we don't want to show irrelevant locations. del locations[location]