2013-10-03 15 views
5

Se posso fare questo:Geodjango: PointField e GEOSGeometry

>>> from django.contrib.gis.geos import GEOSGeometry 
>>> from django.contrib.gis.geos import Point 
>>> point = GEOSGeometry('POINT(1 5)') 
>>> print point 
POINT (1.0000000000000000 5.0000000000000000) 

Perché non posso fare questo:

>>> lat = 1 
>>> lon = 5 
>>> point = GEOSGeometry('POINT(lat lon)') 

GEOS_ERROR: ParseException: Expected number but encountered word: 'lat' 
GEOSException: Error encountered checking Geometry returned from GEOS C function "GEOSWKTReader_read_r". 

Come posso usare una variabile per creare un oggetto GEOSGeometry?

+1

LAT e LONG appaiono all'indietro secondo gis stackexchange post http://gis.stackexchange.com/questions/11626/does-y-mean-latitude-and-x-mean-longitude-in-every-gis-software –

risposta

8

si può sicuramente fare, ma con una leggera modifica

point = GEOSGeometry('POINT(%s %s)' % (lat, lon)) 

O

point = GEOSGeometry('POINT(%d %d)' % (lat, lon)) 

Quando si esegue

`'POINT(lat lon)'` 

si sono non sostituzione delle variabili locali lat e lon con i loro valori delle variabili locali appropriati e vengono valutati alla lettera. Quindi, dovresti usare la sostituzione.

+0

Risposta perfetta. Grazie mille! –

13

La risposta accettata non è corretta. I punti assumono la forma di "PUNTO (latitudine di longitudine)". Li hai invertiti.

+0

Mi ci è voluto un po 'di tempo per capirlo. Immagino che questo dovrebbe essere menzionato nei documenti. –

Problemi correlati