Hello,
I use json for output instead of cvs, and it works. Also I convert the
lattitude and longitude into float.
enjoy
def get_lat_long(location):
key = settings.GOOGLE_API_KEY
output = "json"
location = urllib.quote_plus(location)
request =
"http://maps.google.com/maps/geo?q=%s&output=%s&key=%s
<http://maps.google.com/maps/geo?q=%s&output=%s&key=%s>" % (location,
output, key)
data = urllib.urlopen(request).read()
dlist = simplejson.loads(data)
if dlist['Status']['code'] == 200:
lat = float(dlist['Placemark'][0]['Point']['coordinates'][0])
long = float(dlist['Placemark'][0]['Point']['coordinates'][1])
return (lat, long)
else
return ''
Le 25/09/2009 09:47, please smile a écrit :
> Hi all,
> I have a problem to convert latitude and longitude values into
> point field value.
>
>
> def get_lat_long(location):
> key = settings.GOOGLE_API_KEY
> output = "csv"
> location = urllib.quote_plus(location)
> request =
> "http://maps.google.com/maps/geo?q=%s&output=%s&key=%s
> <http://maps.google.com/maps/geo?q=%s&output=%s&key=%s>" % (location,
> output, key)
> data = urllib.urlopen(request).read()
> dlist = data.split(',')
> if dlist[0] == '200':
> #return "%s, %s" % (dlist[2], dlist[3])
> return (dlist[2], dlist[3])
> else:
> return ''
>
>
> My models.py
> --------------
> class Person(models.Model):
> address = models.CharField(_('address'), max_length=200,
> blank=True)
> city = models.CharField(_('city'), max_length=100, blank=True)
> state = USStateField(_('state'), blank=True)
> zipcode = models.CharField(_('zip code'), max_length=5,
> blank=True)
> point = models.PointField(blank=True, null=True)
>
> objects = models.GeoManager()
>
>
>
> def save(self, force_insert=False, force_update=False):
>
> if self.point == None:
> location = "%s+%s+%s+%s" % (self.address, self.city,
> self.state, self.zipcode)
> point_lat, point_long = get_lat_long(location)
> pnt = Point(point_long, point_lat)
> self.point = pnt
>
> Here I convert latitude , longitude value into point field value. But
> i am getting "Exception Value: Invalid parameters given for Point
> initialization."
>
> please advise...
> Thank you....
>
> Trace back
> ---------------
> Environment:
>
> Request Method: POST
> Request URL: http://192.168.1.57:8000/admin/people/person/add/
> Django Version: 1.1
> Python Version: 2.5.2
> Installed Applications:
> ['peoplesearch.people',
> 'django.contrib.admin',
> 'django.contrib.gis',
> 'django.contrib.auth',
> 'django.contrib.contenttypes',
> 'django.contrib.sessions',
> 'django.contrib.sites',
> 'django.contrib.databrowse',
> 'peoplesearch.haystack']
> Installed Middleware:
> ('django.middleware.common.CommonMiddleware',
> 'django.contrib.sessions.middleware.SessionMiddleware',
> 'django.contrib.auth.middleware.AuthenticationMiddleware')
>
>
> Traceback:
> File "/root/myprojects/directory/django/core/handlers/base.py" in
> get_response
> 92. response = callback(request, *callback_args,
> **callback_kwargs)
> File "/root/myprojects/directory/django/contrib/admin/options.py" in
> wrapper
> 226. return self.admin_site.admin_view(view)(*args,
> **kwargs)
> File "/root/myprojects/directory/django/views/decorators/cache.py" in
> _wrapped_view_func
> 44. response = view_func(request, *args, **kwargs)
> File "/root/myprojects/directory/django/contrib/admin/sites.py" in inner
> 186. return view(request, *args, **kwargs)
> File "/root/myprojects/directory/django/db/transaction.py" in
> _commit_on_success
> 240. res = func(*args, **kw)
> File "/root/myprojects/directory/django/contrib/admin/options.py" in
> add_view
> 734. self.save_model(request, new_object, form,
> change=False)
> File "/root/myprojects/directory/django/contrib/admin/options.py" in
> save_model
> 557. obj.save()
> File "/root/myprojects/peoplesearch/../peoplesearch/people/models.py"
> in save
> 118. pnt = Point(point_long, point_lat)
> File "/root/myprojects/directory/django/contrib/gis/geos/point.py" in
> __init__
> 32. raise TypeError('Invalid parameters given for Point
> initialization.')
>
> Exception Type: TypeError at /admin/people/person/add/
> Exception Value: Invalid parameters given for Point initialization.
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---