gpt4 book ai didi

python - 根据距离和方向计算点

转载 作者:太空狗 更新时间:2023-10-29 22:07:58 25 4
gpt4 key购买 nike

我想使用 GeoDjango 或 GeoPy 根据方向和距离计算一个点。

例如,如果我有一个点是 (-24680.1613, 6708860.65389),我想使用 Vincenty 距离公式找出北 1KM、东 1KM、南 1KM 和西 1KM 的点。

我能找到的最接近的东西是 distance.py ( https://code.google.com/p/geopy/source/browse/trunk/geopy/distance.py?r=105 ) 中的“目的地”函数。虽然我在任何地方都找不到这个文档,但我还没有弄清楚如何使用它。

非常感谢任何帮助。

最佳答案

编辑2

好吧,geopy 有一个开箱即用的解决方案,只是文档不完整:

import geopy
import geopy.distance

# Define starting point.
start = geopy.Point(48.853, 2.349)

# Define a general distance object, initialized with a distance of 1 km.
d = geopy.distance.VincentyDistance(kilometers = 1)

# Use the `destination` method with a bearing of 0 degrees (which is north)
# in order to go from point `start` 1 km to north.
print d.destination(point=start, bearing=0)

输出为 48 52m 0.0s N, 2 21m 0.0s E(或 Point(48.861992239749355, 2.349, 0.0))。

90 度方位对应东,180 度对应南,依此类推。

较早的答案:

一个简单的解决方案是:

def get_new_point():
# After going 1 km North, 1 km East, 1 km South and 1 km West
# we are back where we were before.
return (-24680.1613, 6708860.65389)

但是,我不确定这是否总的来说符合您的目的。

好的,说真的,你可以开始使用 geopy 了。首先,您需要在 geopy 已知的坐标系中定义起点。乍一看,您似乎不能只是向某个方向“添加”某个距离。我认为,原因是距离的计算是一个没有简单逆解的问题。或者我们如何反转 https://code.google.com/p/geopy/source/browse/trunk/geopy/distance.py#217 中定义的 measure 函数?

因此,您可能需要采用迭代方法。

如此处所述:https://stackoverflow.com/a/9078861/145400你可以这样计算两个给定点之间的距离:

pt1 = geopy.Point(48.853, 2.349)
pt2 = geopy.Point(52.516, 13.378)
# distance.distance() is the VincentyDistance by default.
dist = geopy.distance.distance(pt1, pt2).km

要向北行驶一公里,您需要将纬度迭代更改为正方向并检查距离。您可以使用来自例如的简单迭代求解器自动执行此方法。 SciPy:只需通过 http://docs.scipy.org/doc/scipy/reference/optimize.html#root-finding 中列出的优化器之一找到 geopy.distance.distance().km - 1 的根.

我认为很明显,通过将纬度更改为负方向向南,通过更改经度向西和向东。

我没有进行此类地理计算的经验,只有在没有简单直接的方法“向北”一定距离的情况下,这种迭代方法才有意义。

编辑:我的提案的实现示例:

import geopy
import geopy.distance
import scipy.optimize


def north(startpoint, distance_km):
"""Return target function whose argument is a positive latitude
change (in degrees) relative to `startpoint`, and that has a root
for a latitude offset that corresponds to a point that is
`distance_km` kilometers away from the start point.
"""
def target(latitude_positive_offset):
return geopy.distance.distance(
startpoint, geopy.Point(
latitude=startpoint.latitude + latitude_positive_offset,
longitude=startpoint.longitude)
).km - distance_km
return target


start = geopy.Point(48.853, 2.349)
print "Start: %s" % start

# Find the root of the target function, vary the positve latitude offset between
# 0 and 2 degrees (which is for sure enough for finding a 1 km distance, but must
# be adjusted for larger distances).
latitude_positive_offset = scipy.optimize.bisect(north(start, 1), 0, 2)


# Build Point object for identified point in space.
end = geopy.Point(
latitude=start.latitude + latitude_positive_offset,
longitude=start.longitude
)

print "1 km north: %s" % end

# Make the control.
print "Control distance between both points: %.4f km." % (
geopy.distance.distance(start, end).km)

输出:

$ python test.py 
Start: 48 51m 0.0s N, 2 21m 0.0s E
1 km north: 48 52m 0.0s N, 2 21m 0.0s E
Control distance between both points: 1.0000 km.

关于python - 根据距离和方向计算点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24427828/

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