4

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)
  • I did an update – Azd325 Jul 31 '15 at 9:05
  • I use now twice the 2nd version – Azd325 Aug 3 '15 at 16:12
1

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.