ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – 使用GeoDjango进行3d距离计算

2019-10-02 04:55:43  阅读:246  来源: 互联网

标签:geodjango python django postgis gis


我在用

> python 2.7.12
> django 1.10.6
> postgreSQL 9.5.6
> postGIS 2.2.2

第一个问题

我需要使用GeoDjango来计算两点之间的距离.当我检查documentation时,它表示不推荐使用GeoQuerySet.distance(),而是使用django.contrib.gis.db.models.functions中的Distance().

以下代码可以正常工作:

from django.contrib.gis.db.models.functions import Distance

p1 = Instrument.objects.get(pk=151071000).coordinates
p2 = Instrument.objects.filter(pk=151071008)

for i in p2.annotate(distance=Distance('coordinates', p1)):
    print i.distance
    print i.distance.__class__

输出:

461.10913945 m
<class 'django.contrib.gis.measure.Distance'>

我的模特:

class Instrument(models.Model):
    ...
    coordinates = gis_models.PointField(null=True, blank=True, dim=3)

但是我只有两点,所以当我尝试使用没有annotate()的Distance()时,它会返回类django.contrib.gis.db.models.functions.Distance()的实例,而不是django.contrib.gis.measure.Distance ():

p1 = Instrument.objects.get(pk=151071000).coordinates
p2 = Instrument.objects.get(pk=151071008).coordinates
print Distance(p1, p2)

输出:

Distance(Value(SRID=4326;POINT Z (-76.48623600000001 44.260223 0)), GeomValue(SRID=4326;POINT Z (-76.490923 44.262658 0)))

如何获得与使用annotate()相同的结果?

第二个问题

我必须计算考虑深度/高度的3d距离.但是当我尝试这样做时,我得到与2d相同的结果.下面我在第一个对象中将高程更改为200:

p1 = Instrument.objects.get(pk=151071000)
p1.coordinates = 'SRID=4326;POINT Z (-76.48623600000001 44.260223 200)'
p2 = Instrument.objects.filter(pk=151071008)

for i in p2.annotate(distance=Distance('coordinates', p1.coordinates)):
    print i.distance

输出:

461.10913945 m

解决方法:

让我们打破问题:

>在Distance类文档中,我们可以阅读以下内容:

Accepts two geographic fields or expressions and returns the distance between them, as a Distance object.

所以距离(p1,p2)返回Distance object.
如果你这样做:

p1 = Instrument.objects.get(pk=151071000).coordinates
p2 = Instrument.objects.get(pk=151071008).coordinates
d = Distance(m=p1.distance(p2))
print d.m

您将获得以米为单位的测量值.

我会坚持使用注释解决方案,这似乎更加坚固! (见解答)
>距离计算两点之间的2D距离.为了获得3D计算,您需要自己创建一个.
你可以从这个问题看看我的方法:Calculating distance between two points using latitude longitude and altitude (elevation)

  • Let polar_point_1 = (long_1, lat_1, alt_1) and polar_point_2 =
    (long_2, lat_2, alt_2)

  • Translate each point to it’s Cartesian equivalent by utilizing this
    formula:

    06001

    and you will have p_1 = (x_1, y_1, z_1) and p_2 = (x_2, y_2, z_2) points respectively.

  • Finally use the Euclidean formula:

    06002

标签:geodjango,python,django,postgis,gis
来源: https://codeday.me/bug/20191002/1841296.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有