2014-06-26 178 views
2

我想根据使用GeoDjango或GeoPy的方向和距离来计算点。例如,如果我有一个点(-24680.1613,6708860.65389),我想使用Vincenty距离公式找出一个点为北1KM,东1KM,1KM Sourh和1KM西。根据距离和方向计算点

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

任何帮助,非常感谢。

+0

'(-24680.1613,6708860.65389)'< - 在这种坐标系这一点给? –

回答

6

编辑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。首先,您需要在已知地理坐标系中定义起点。乍看之下,你似乎不能仅仅向某个方向“添加”一定的距离。我认为,原因是没有简单的逆解决方案,距离的计算就成了一个问题。或者我们将如何反转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 

对于由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. 
+0

嗨扬 - 菲利普,目的是找出每一个的坐标,并用它在中心点周围绘制一个矩形1公里。 –

+0

所以你想根据原点得到四个点,每个点偏移1公里进入四个基本方向之一。你应该相应地调整你的问题;) –

+0

我已经发布了一个具体的解决方案,请参阅我的编辑。 –

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