根据距离和方向计算点

Lon*_*Dev 6 python django geodjango geopy

我想使用GeoDjango或GeoPy计算基于方向和距离的点.

例如,如果我有一个点(-24680.1613,6708860.65389)我想找到一个点1KM North,1KM East,1KM Sourh和1KM west使用Vincenty距离公式.

我最接近的是distance.py中的"目的地"功能(https://code.google.com/p/geopy/source/browse/trunk/geopy/distance.py?r=105).虽然我无法在任何地方找到这个记录,但我还没弄明白如何使用它.

任何帮助深表感谢.

Jua*_*cia 15

此问题的 2020 年更新,基于 Jan-Philip Gehrcke 博士的回答。

VincentyDistance 已正式弃用,并且从未完全准确,有时甚至不准确。

此代码段显示了如何使用最新版本(以及未来版本的 GeoPy - Vincenty 将在 2.0 中弃用)

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.distance(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.
final = d.destination(point=start, bearing=0)
Run Code Online (Sandbox Code Playgroud)

final是一个新Point对象,打印时返回48 51m 43.1721s N, 2 20m 56.4s E

如您所见,哪个比 Vincenty,并且应该在极点附近保持更好的准确度。

希望能帮助到你!


Jan*_*cke 14

编辑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)
Run Code Online (Sandbox Code Playgroud)

输出是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)
Run Code Online (Sandbox Code Playgroud)

但是,我不确定这是否符合您的目的.

好的,说真的,你可以开始使用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
Run Code Online (Sandbox Code Playgroud)

如果向北行驶一公里,您将迭代地将纬度改为正方向并检查距离.您可以使用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)
Run Code Online (Sandbox Code Playgroud)

输出:

$ 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.
Run Code Online (Sandbox Code Playgroud)