Python 基于距离和方向计算点
我想使用GeoDjango或GeoPy基于方向和距离计算一个点 例如,如果我有一个点是(-24680.1613,6708860.65389),我想用Vincenty距离公式找出一个点,北1KM,东1KM,南1KM,西1KM 我能找到的最接近的东西是distance.py()中的“destination”函数。尽管我在任何地方都找不到这个文档,我还没有弄清楚如何使用它Python 基于距离和方向计算点,python,django,geodjango,geopy,Python,Django,Geodjango,Geopy,我想使用GeoDjango或GeoPy基于方向和距离计算一个点 例如,如果我有一个点是(-24680.1613,6708860.65389),我想用Vincenty距离公式找出一个点,北1KM,东1KM,南1KM,西1KM 我能找到的最接近的东西是distance.py()中的“destination”函数。尽管我在任何地方都找不到这个文档,我还没有弄清楚如何使用它 非常感谢您的帮助。编辑2 好的,geopy有一个现成的解决方案,只是没有很好的文档记录: import geopy import
非常感谢您的帮助。编辑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)
输出为4852m0.0sn,221m0.0se
(或点(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已知的坐标系中定义起点。乍一看,似乎你不能只是在某个方向上“增加”某个距离。我认为,原因是距离的计算是一个没有简单逆解的问题。或者,我们将如何反转中定义的度量
函数
因此,您可能希望采用迭代方法
如下所述:您可以这样计算两个给定点之间的距离:
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:只需通过中列出的一个优化器找到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 Gehrcke博士的回答,这个问题的2020年更新
VincentyDistance
已被正式弃用,而且从未完全精确,有时甚至不准确
此代码段显示了如何与最新版本一起使用(GeoPy-Vincenty的未来版本将在2.0中被弃用)
final
是一个新的点
对象,打印时返回48 51m 43.1721s N,2 20m 56.4s E
如您所见,它比Vincenty更精确,并且在极点附近应保持更好的精度
希望有帮助 (-24680.1613,6708860.65389)
你好,Jan Philip,目的是计算出每个点的坐标,并用它围绕中心点画一个1km的矩形。因此,您需要基于原点获得四个点,每个点在四个基本方向之一偏移1km。你应该相应地调整你的问题;)然后这里有一个很好的文字,有参考信息和实现:现在如果geopy有相反的能力,在两点之间找到vincenty方向…@Jan PhilipGehrcke我有一个类似的问题,除了不移动“北”,我尝试在两点之间移动。这可能吗?问题是:但似乎您的初始位置未被使用。我想您需要将位置替换为初始位置或相反的位置。
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
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)