Reputation: 1880
This geocoding code works locally, but not in the server, it gets the address in the form of self.request.get('address')
and returns the ndb.GeoPt()
value from a get_lat_long(address)
function.
So, how come I get the error:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~map-jobs/2.371278632227250958/main.py", line 453, in post
profile.geopoint = get_lat_long(self.request.get('address'))
File "/base/data/home/apps/s~map-jobs/2.371278632227250958/main.py", line 17, in get_lat_long
lat = data['results'][0]['geometry']['location']['lat']
IndexError: list index out of range
With this code:
def get_lat_long(address):
address = cgi.escape(address.replace(" ","+"))
sensor = "true"
url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor="+ sensor
result = urlfetch.fetch(url)
data = json.loads(result.content)
lat = data['results'][0]['geometry']['location']['lat']
long = data['results'][0]['geometry']['location']['lng']
return ndb.GeoPt(str(lat) +","+ str(long))
What is the correct way of writing this in the google-app-engine server?
Upvotes: 0
Views: 555
Reputation: 82470
data['results'][0]
is actually empty.
This happens when there are not results. Usually, I just put it in a try/catch
block, and except an IndexError
:
try:
data['results'][0]....
except IndexError, e:
print "No data found" # You can use a logger here too
In your case, put a try/catch
block around the following code, and find a way to deal with the error:
lat = data['results'][0]['geometry']['location']['lat']
long = data['results'][0]['geometry']['location']['lng']
This happened to me a lot, when I was working with google drive, since a lot of the doucments did not have text/html
as an output format, I was always getting KeyError
. In your case you are dealing with google maps, and if there are results there should be geometry
and the other keys in the dict, however, you may also choose to add KeyError
to your except clause to be extra precautious.
Upvotes: 1