如何计算两个PointField之间的距离? [英] How to calculate distance between two PointField?

查看:96
本文介绍了如何计算两个PointField之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Postgis与Django一起使用来存储地理位置.我有一个如下模型:

I use Postgis with Django to store geographic points. I have a model like the following:

from django.contrib.gis.db import models
class Event(models.Model)
    position = models.PointField()

我有两个事件(ev1,ev2).这是每个位置值:

I have two events(ev1, ev2). Here is the position value of each:

SRID=4326;POINT (-73.6335140000000052 45.5472019999999986)
SRID=4326;POINT (-73.6267909999999972 45.5459189999999978)

我的目标是得出两点之间的距离(以米为单位).如果我这样做:

My goal is to get distance in meters between those two points. If i do:

ev1.position.distance(ev2.position)

我得到0.006844327432269004 如何以米为单位转换此值?

I get 0.006844327432269004 How can I convert this value in meters?

谢谢!

推荐答案

我能够使其工作,但只能通过queryset进行.在这里,我想了解事件3与所有其他事件之间的距离:

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))

位置"是PointField的名称

"position" is the name of the PointField

这篇关于如何计算两个PointField之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆