Created
January 6, 2010 22:06
-
-
Save mattdennewitz/270720 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# quick & dirty example | |
def get_placemark_point(raw_location): | |
"""Attempt to resolve ``raw_location`` using Google's geocoder. | |
Returns a tuple with the canonical address and GEOS point | |
""" | |
import cjson | |
from urllib import urlencode | |
from urllib2 import urlopen | |
from django.contrib.gis.geos import fromstr | |
# encode the request | |
data = urlencode({ | |
'q': raw_location, | |
'output': "json", | |
'oe': "utf8", | |
'sensor': "false", | |
'key': "my_app_key" | |
}) | |
url = "http://maps.google.com/maps/geo?%s" % data | |
response = urlopen(url) | |
geo_content = cjson.decode(response.read()) | |
placemark = geo_content['Placemark'][0] | |
# convert to a geodjango point | |
lng, lat = placemark['Point']['coordinates'][:2] | |
point = fromstr("POINT(%s %s)" % (lng, lat)) | |
return (placemark['address'], point) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment