gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:23:06 36 4
gpt4 key购买 nike

我正在使用

  • 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

最佳答案

让我们分解问题:

  1. Distance类文档中,我们可以看到如下内容:

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

    所以 Distance(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

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

    我会坚持使用 annotate 解决方案,它看起来更可靠! (自以为是的回应)


  1. Distance 计算两点之间的二维距离。为了获得 3D 计算,您需要自己创建一个。
    你可以从这个问题看看我的方法:Calculating distance between two points using latitude longitude and altitude (elevation)

    2019 年编辑:自最初的回答以来,我在这里编写了一个问答式示例:How to calculate 3D distance (including altitude) between two points in GeoDjango 在 2 个点与高度之间使用了更好(并且更不容易出错)的距离计算。

    排序:

    我们需要使用 great-circle distanceHaversine formula 计算两点之间的 2D Vicenty formula,然后我们可以将它与两点之间的高度差(delta)结合起来计算它们之间的 Euclidean distance,如下所示:

    dist = sqrt(great_circle((lat_1, lon_1), (lat-2, lon_2).m**2, (alt_1 - alt_2)**2)

    该解决方案假定高度以米为单位,因此也将 great_circle 的结果转换为米。


留在这里是为了继续评论。

2. Distance 计算两点之间的二维距离。为了获得 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:

    x = alt * cos(lat) * sin(long)
    y = alt * sin(lat)
    z = alt * cos(lat) * cos(long)

    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:

    dist = sqrt((x_2-x_1)**2 + (y_2-y_1)**2 + (z_2-z_1)**2)

关于python - 使用 GeoDjango 进行 3d 距离计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43474570/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com