根据距离和方向计算点数 [英] Calculate point based on distance and direction

查看:247
本文介绍了根据距离和方向计算点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我最近可以找到的是distance.py中的目的地功能( https://code.google.com/p/geopy/source/browse/trunk/geopy/distance.py?r = 105 )。虽然我没有找到这个记录在任何地方,我还没有弄清楚如何使用它。



任何帮助非常感谢。

解决方案

编辑2



-box解决方案与geopy,它只是没有很好的记录:

  import geopy 
import geopy.distance

#定义起点。
start = geopy.Point(48.853,2.349)

#定义一个距离为1公里的一般距离对象。
d = geopy.distance.VincentyDistance(km = 1)

#使用'destination'方法,方位为0度(北)为
#从'开始'1公里到北。
print d.destination(point = start,bearing = 0)

输出为 48 52m 0.0s N,2 21m 0.0s E (或 Point(48.861992239749355,2.349,0.0))。 p>

90度的轴承对应于东方,180度为南方,依此类推。



答案:



一个简单的解决方案是:

  def get_new_point():
#经过1公里北,东1公里,南1公里,西1公里西
#我们回到了前面的地方。
返回(-24680.1613,6708860.65389)

但是,我不知道是否为您服务目的在于所有的一般性。



好的,认真的,你可以开始使用geopy。首先,您需要在已知的地理位置坐标系中定义起点。乍一看,似乎你不能只是在某个方向添加一定距离。我认为的原因是距离的计算是一个没有简单逆解的问题。或者我们如何反转度量值 distance.py#217rel =nofollow noreferrer> https://code.google.com/p/geopy/source/browse/trunk/geopy/distance.py#217



因此,您可能需要采取迭代的方法。



如下所述: https://stackoverflow.com/a/9078861/145400 ,您可以计算两个给定点之间的距离:

  pt1 = geopy.Point(48.853,2.349)
pt2 = geopy.Point(52.516,13.378)
#distance.distance()是VincentyDistance默认。
dist = geopy.distance.distance(pt1,pt2).km

通过一公里,您将迭代地将纬度更改为正方向,并检查距离。您可以使用简单的迭代求解器自动化此方法。 SciPy:通过 geopy.distance.distance()。km - 1 的根.org / doc / scipy / reference / optimize.html#root-findingrel =nofollow noreferrer> http://docs.scipy.org/doc/scipy/reference/optimize.html#root-finding



我认为很明显,你可以通过改变经度,将纬度变为负方向,向西和东方向南移动。



我没有这种地理计算的经验,这种迭代方法只有在没有简单的直接方式向北一定距离才有意义。



编辑:我的提案的示例实现:

  import geopy 
import geopy.distance
import scipy.optimize


def north(startpoint,distance_km):
返回其参数为正纬度的目标函数
相对于`startpoint`更改(以度为单位),并且具有ro ot
对应于距离起点的距离为
`distance_km`公里的纬度偏移。

def target(latitude_positive_offset):
return geopy.distance.distance(
startpoint,geopy.Point(
latitude = startpoint.latitude + latitude_positive_offset,
longitude = startpoint.longitude)
).km - distance_km
返回目标


start = geopy.Point(48.853,2.349)
打印开始:%s%start

#查找目标函数的根,改变
#0和2度之间的偏移纬度偏移(这足以确定一个1公里的距离,但必须
#调整较大的距离)
latitude_positive_offset = scipy.optimize.bisect(north(start,1),0,2)


#空间中标识点的构建点对象
end = geopy.Point(
latitude = start.latitude + latitude_positive_offset,
longitude = start.longitude


打印1 km north:%s%end

#进行控制
打印控制两点之间的距离:%.4f km。%(
geopy.distance.distance(start,end).km)

输出:

  $ python test.py 
开始:48 51m 0.0s N,2 21m 0.0s E
1 km north:48 52m 0.0s N,2 21m 0.0s E
两点之间的控制距离:1.0000公里。


I would like to calculate a point based on direction and distance using GeoDjango or GeoPy.

For example, If I have a point that is (-24680.1613, 6708860.65389) I would like to find out a point 1KM North, 1KM East, 1KM Sourh and 1KM west using Vincenty distance formula.

I closest thing I can find is a "destination" function in distance.py (https://code.google.com/p/geopy/source/browse/trunk/geopy/distance.py?r=105). Although I cannot find this documented anywhere and I'm yet to figure out how to use it.

Any help is much appreciated.

解决方案

Edit 2

Okay, there is an out-of-the-box solution with geopy, it is just not well-documented:

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)

The output is 48 52m 0.0s N, 2 21m 0.0s E (or Point(48.861992239749355, 2.349, 0.0)).

A bearing of 90 degrees corresponds to East, 180 degrees is South, and so on.

Older answers:

A simple solution would be:

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)

However, I am not sure if that serves your purposes in all generality.

Okay, seriously, you can get started using geopy. First of all, you need to define your starting point in a coordinate system known to geopy. At the first glance, it seems that you cannot just "add" a certain distance into a certain direction. The reason, I think, is that calculation of the distance is a problem without simple inverse solution. Or how would we invert the measure function defined in https://code.google.com/p/geopy/source/browse/trunk/geopy/distance.py#217?

Hence, you might want to take an iterative approach.

As stated here: https://stackoverflow.com/a/9078861/145400 you can calculate the distance between two given points like that:

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

For going north by one kilometer you would iteratively change the latitude into a positive direction and check against the distance. You can automate this approach using a simple iterative solver from e.g. SciPy: just find the root of geopy.distance.distance().km - 1 via one of the optimizers listed in http://docs.scipy.org/doc/scipy/reference/optimize.html#root-finding.

I think it is clear that you go south by changing the latitude into a negative direction, and west and east by changing the longitude.

I have no experience with such geo calculations, this iterative approach only makes sense if there is no simple direct way for "going north" by a certain distance.

Edit: an example implementation of my proposal:

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)

Output:

$ 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.

这篇关于根据距离和方向计算点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆