【问题标题】:Calculate point based on distance and direction根据距离和方向计算点
【发布时间】:2014-06-26 10:13:44
【问题描述】:

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

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

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

非常感谢任何帮助。

【问题讨论】:

  • (-24680.1613, 6708860.65389)

标签: python django geodjango geopy


【解决方案1】:

编辑 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.

【讨论】:

  • 嗨 Jan-Philip,我们的目的是计算出每个人的坐标,并用它在中心点周围画一个 1 公里的矩形。
  • 所以你想基于原点得到四个点,每个点偏移 1 公里到四个基本方向之一。你应该相应地调整你的问题;)
  • 然后这里有一篇不错的文章,附有参考信息和实现:movable-type.co.uk/scripts/latlong.html
  • 现在如果只有 geopy 有相反的能力,找到两点之间的文森特方向的能力......
  • @Jan-PhilipGehrcke 我有一个类似的问题,除了移动'北',我试图在两点之间移动。这可能吗?问题在这里:stackoverflow.com/questions/38619835/…
【解决方案2】:

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

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

这个 sn-p 展示了如何使用最新的(以及未来版本的 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 更准确,并且应该在两极附近保持更好的准确度。

希望对你有帮助!

【讨论】:

    【解决方案3】:

    我不得不在经度和纬度上加上米。

    这是我所做的,灵感来自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 似乎没有被使用。我想您需要将 location 替换为 initial_location 或相反。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多