2

I've noticed that in GeoDjango the distance method behaves differently if it's being used inside an annotation, a query etc.. or if it's used between two points or geometries.

Look at this example:

bss = BusStop.objects.filter(point__distance_gt=(base_point, D(m=1000))).filter(point__distance_lt=(base_point, D(m=1200))).annotate(distance=Distance('point', base_point)).order_by('distance')
In [20]: for bs in bss:
...:     print(bs.amic_code, bs.distance)
...:
1800SBH0781 1002.00849246 m
1800SB30351 1002.56747241 m
1800SB04941 1003.2266972 m
1800SB30331 1008.46770311 m

The distance is correctly expressed in meters, as I expect.

This is instead what happens if I use the distance method between two points:

In [23]: type(base_point)
Out[23]: django.contrib.gis.geos.point.Point

In [24]: bss[0].point.distance(base_point)
Out[24]: 0.014417147670674185

I can't really understand how the distance is being expressed. What I get back is just a float. Having the distance in meters would be nice, but even better if I can have a measurement object where I can get all the other formats: https://docs.djangoproject.com/en/1.10/ref/contrib/gis/measure/

Note: I've read this https://docs.djangoproject.com/en/1.10/ref/contrib/gis/geos/#django.contrib.gis.geos.GEOSGeometry.distance where it says "GEOS distance calculations are linear – in other words, GEOS does not perform a spherical calculation even if the SRID specifies a geographic coordinate system.", but it doesn't explain how to do a spherical calculation.

How can I get the distance in meters like in the first use case?

Update: I've tried to use this library https://pypi.python.org/pypi/geopy/1.11.0 and I've nothing against using it, but... if I try to calculate the distance between one of the points and the base_point I get 1.6 Km that is quite unexpected, given that I have a list of points which have a distance > 1000 meters and < 1200 meters. Something is still wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.