Geopy using maps.google.com.hk and Unicode.

145 views
Skip to first unread message

Jack Low

unread,
Mar 17, 2010, 12:26:50 AM3/17/10
to geopy
I am looking to Geocode an address in unicode (Chinese characters).
I'm getting this error:

Traceback (most recent call last):
File "F:\Program Files\Google\google_appengine\google\appengine\ext
\webapp\__init__.py", line 507, in __call__
handler.get(*groups)
File "F:\map\engineapp\restaurant.py", line 605, in get
place, (lat, lng) = g.geocode(obj[6] + obj[7] + obj[8])
File "F:\map\engineapp\geopy\geocoders\google.py", line 65, in
geocode
url = self.url % urlencode(params)
File "F:\Python26\lib\urllib.py", line 1268, in urlencode
v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-6: ordinal not in range(128)

Alex Ezell

unread,
Mar 22, 2010, 11:27:27 PM3/22/10
to ge...@googlegroups.com
Hi Jack,
The error is actually being generated by `urllib` from the standard library when it tries to urlencode the values you're passing in the URL. This module is notoriously incompatible with unicode characters. You'll find different errors generated by different versions of Python. According to what I've read, none of them really handles the strings correctly, nor do the errors always seem to make sense.

At any rate, it's possible that geopy should encode the unicode to utf-8, which urllib typically can handle correctly. You might patch this in geopy by changing line 65 in the traceback below to be something like this:

url = self.url % urlencode(dict([k, v.encode('utf-8')] for k, v in params.items()]))

That's untested, so let us know if it works for you.

/alex


--
You received this message because you are subscribed to the Google Groups "geopy" group.
To post to this group, send email to ge...@googlegroups.com.
To unsubscribe from this group, send email to geopy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/geopy?hl=en.


Reply all
Reply to author
Forward
0 new messages