根据距离和方向计算点的位置

19

我希望能够使用GeoDjango或GeoPy根据方向和距离计算出一个点。

例如,如果我有一个坐标点为(-24680.1613, 6708860.65389),我想要使用Vincenty距离公式找到一个东北南西各相隔1公里的点。

我能找到的最接近的函数是在distance.py中的“destination”函数(https://code.google.com/p/geopy/source/browse/trunk/geopy/distance.py?r=105)。尽管我无法在任何地方找到它的文档,并且我还没有弄清楚如何使用它。

非常感谢您的任何帮助。


(-24680.1613, 6708860.65389) <- 这个点是在哪个坐标系统下给出的? - Dr. Jan-Philip Gehrcke
3个回答

24

编辑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://dev59.com/Cmox5IYBdhLWcg3wnltI#9078861,您可以按以下方式计算两个给定点之间的距离:

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

如果您要向北前进1公里,可以将纬度迭代地改变为正方向,并根据距离检查。您可以使用例如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.

嗨,Jan-Philip,目的是找出每个坐标并使用它来绘制中心点周围1公里的矩形。 - LondonAppDev
所以你想要基于原点获取四个点,每个点向四个主要方向之一偏移1公里。你应该相应地调整你的问题 ;) - Dr. Jan-Philip Gehrcke
然后这里有一个很好的写作,包括参考信息和实现:http://www.movable-type.co.uk/scripts/latlong.html - Dr. Jan-Philip Gehrcke
1
现在只要geopy有相反的功能,就是找到两点之间的Vincenty方向... - Michael
@Jan-PhilipGehrcke 我有一个类似的问题,不过我想在两个点之间移动,而不是向“北”移动。这可行吗?问题在这里:https://stackoverflow.com/questions/38619835/finding-a-gps-location-at-a-certain-time-given-two-points - tempomax

20

基于Dr. Jan-Philip Gehrcke的回答,对该问题进行了2020年更新。

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)

final是一个新的Point对象,打印时返回48 51m 43.1721s N, 2 20m 56.4s E。如您所见,它比Vincenty更准确,并且在极地附近应该保持更好的准确性。

希望对您有所帮助!


1
我必须处理将经度和纬度添加到米的问题。
以下是我所做的,受this source的启发:
import math
from geopy.distance import vincenty

initial_location = '50.966086,5.502027'
lat, lon = (float(i) for i in location.split(','))
r_earth = 6378000
lat_const = 180 / math.pi
lon_const = lat_const / math.cos(lat * math.pi / 180)

# dx = distance in meters on x axes (longitude)
dx = 1000
new_longitude = lon + (dx / r_earth) * lon_const
new_longitude = round(new_longitude, 6)
new_latitude = lat + (dy / r_earth) * lat_const
new_latitude = round(new_latitude, 6)

# dy = distance on y axes (latitude)
new_latitude = lat + (dy / r_earth) * lat_const
new_latitude = round(new_latitude, 6)
new_location = ','.join([str(y_lat), str(x_lon)])

dist_to_location = vincenty(location, new_location).meters

但是看起来你没有使用initial_location。我想你需要用initial_location替换location或者反过来。 - mike123

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接