[GIS] Django Postgis Distance from point

geodjangopointpostgis

I have a point where the objcet is and another point for mylocation.

I want to calculate the distance

>> object_location
<Point object at 0x7f9ceaac7be0>

>> mylocation
<Point object at 0x7f9cf9311b50>

>> mylocation.distance(location)
0.13980702957351368

But the result is wrong is there some different way with geodjango to calculate the distance between two points

This way in ipdb is correct

ipdb> Item.objects.filter(id=self.object.id).distance(mylocation, field_name="address__location")[0].distance
Distance(m=15368.0088756)

Best Answer

This might just be a problem of projections/units, it looks like the projection of mylocation.srid and object_location.srid might be WGS84 (the default projection in GeoDjango).

If you want to have distance in meters without making another database lookup, you could transform your points with something like this:

mylocation.transform(3857)
object_location.transform(3857)

and then calculate the distance, which will be returned in meters. The distance would be linear distance between the two points. The exact results will still depend on the projection you choose. Also the accuracy will be limited because it does not calculate spherical distances according to the GeoDjango documentation. So depending on your use case, using a DB query is still the better solution.