How to calculate distance between two PointField?

徘徊边缘 提交于 2019-12-06 09:23:20
Michael

I was able to make it work, but only through a queryset. Here I wanted to get the distance between Event 3 and all other Events:

from django.contrib.gis.measure import Distance

p2 = Event.objects.get(id=3).position
for event in Event.objects.all().exclude(id=3).annotate(distance=Distance('position', p2)):
    print('{0} - {1}'.format(event.id, event.distance.m))

"position" is the name of the PointField

pnt.distance(pnt2) * 100

will give you distance in km.Change accordingly

according to this: https://docs.djangoproject.com/en/1.9/ref/contrib/gis/measure/#example

if you have:

in_meters = ev1.position.distance(ev2.position).m

you will get results in meters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!