lundi 13 juin 2016

GeoDjango - lon/lat PointFields distance using geometry instead of geography

I'm trying to use GeoDjango to store lat/lon in a PointField and then query the distance between two PointFields in kilometres. Something is not working to cause the distance calculation to return the geometric distance rather than the geographic distance. I'm using geography=True in the model but it doesn't seem to help. I'm using postgis and postgresql

Models:

from django.contrib.gis.db import models

class Customer(models.Model):
    location = models.CharField(max_length=100)
    gis_location = models.PointField(u"longitude/latitude",
                                     geography=True, 
                                     blank=True, 
                                     null=True)

Test:

from django.contrib.gis.geos import Point

# some set up

customer1.gis_location = Point(-79.3, 43.6)
print('Customer 1:', customer1.gis_location)

customer2.gis_location = Point(-89.2, 48.4)
print('Customer 2:', customer2.gis_location)

distance = customer1.gis_location.distance(customer2.gis_location)
print('Distance: ', distance)

Output:

Customer 1:  SRID=4326;POINT (-79.2999999999999972 43.6000000000000014)  
Customer 2:  SRID=4326;POINT (-89.2000000000000028 48.3999999999999986)  
Distance: 11.002272492535353

This is returning just the geometric distance between two points instead of the geographic distance. Does anyone have any advice for how I can get this to return KMs distance on a spheroid?

Thanks!

Aucun commentaire:

Enregistrer un commentaire