GeoDjango:如何获得两点之间的距离? [英] GeoDjango: How can I get the distance between two points?

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

问题描述

我的个人资料模特有这个字段:

  location = models .PointField(geography = True,dim = 2,srid = 4326)

我想计算使用GeoDjango,这两个位置之间的距离(考虑到地球是球体),以便我可以将该距离存储在数据库中。


  1. 如何用GeoDjango计算这个距离?

  2. 结果中有哪些单位?

  3. 存储这些数据是否有最好的方式?浮动?小数?

我已经回顾了以前的类似问题,并没有发现它们有用。没有答案给出足够的解释发生了什么或为什么它的工作。



我正在使用Django 1.8和GeoDjango所需的库的最新版本。



谢谢!

解决方案

以下 Abhyudit Jain 的评论,我正在使用 geopy 来计算距离。我将它作为一个财产添加,而不是存储它,根据 e4c5 的建议:

 从django.contrib.gis.measure进口距离,D 
从geopy.distance进口距离

@property
def距离(自):
返回距离(m = distance(self.user_a.profile.location,self.user_b.profile.location).meters)

Geopy默认为 Vincenty的公式 ,并带有错误高达0.5%,并且包含将来将使用的许多其他功能。



上述返回一个GeoDjango Distance对象,可以在测量之间轻松转换。感谢您的帮助!



$ b

My Profile model has this field:

location = models.PointField(geography=True, dim=2, srid=4326)

I'd like to calculate the distance between the two of these locations (taking into account that the Earth is a spheroid) using GeoDjango, so that I can store this distance in the database.

  1. How can I calculate this distance with GeoDjango?
  2. What units are the results in?
  3. Is there a 'best' way to store this data? Float? Decimal?

I've reviewed previous, similar questions, and haven't found them useful. No answer gives enough explanation of what's happening or why it works.

I'm using Django 1.8 and the latest versions of required libraries for GeoDjango.

Thanks!

解决方案

Following Abhyudit Jain's comment, I'm using geopy to calculate distance. I'm adding it as a property as opposed to storing it, as per e4c5's advice:

from django.contrib.gis.measure import Distance, D
from geopy.distance import distance

@property
def distance(self):
    return Distance(m=distance(self.user_a.profile.location, self.user_b.profile.location).meters)

Geopy defaults to Vincenty’s formulae, with an error of up to 0.5%, and contains a lot of other functionality I'll use in future.

The above returns a GeoDjango Distance object, ready for easy conversion between measurements.

Thanks for the help!

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

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