计算位置和位置列表之间的距离 [英] calculate distance between a location and a list of locations

查看:64
本文介绍了计算位置和位置列表之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位置列表,遵循以下模式:

I have a list of locations , following this pattern:

[1] = {lat = -40.2452, longitude = -76.2489},
[2] = {lat = -40.2452, longitude = -76.2489},
[3] = {lat = -40.2452, longitude = -76.2489},
[4] = {lat = -40.2452, longitude = -76.2489}

和一个地方

location = {lat = -40.2452, longitude = -76.2489}

我想计算距离内的哪些位置.

and I want to calculate Which of the locations this Within the distance.

我找到了一个计算两点之间距离的公式.

I found a formula that calculates the distance between two points.

但是如果这个列表很大!

but if this list is big!

有没有更快的方法?

或者你可以循环遍历列表?

or you can go through the list in a loop ?

FOR LOCATION IN LISTLOCATION DO
    IF GETDISTANCE(LOCATION, LOCATION2) <= DISTANCE
        SAVE THIS LOCATION
    END
END

这些值是一个例子

推荐答案

基本上你必须检查每一个点,没有其他选择.距离公式(Haversine)确实很慢,因为它使用的三角函数很少.您真正想要的是围绕您的点绘制一个圆,其半径 R 是距离,并检查每个点是否在该圆内:

Basically you have to check each and every point, no other option. The distance formula (Haversine) is indeed slow, since it uses few trigonometric functions. What you actually want is to plot a circle around your point, whose radius R is the distance, and check for each point if it's inside that circle:

问题是您的点是以 (lat, long) 对而不是 (x,y) 对给出的,因此您不能使用常规"三角函数方法,例如圆的方程.
相反,您必须找到一个包围该圆的正方形.向北和向南走 90 度,找到那个正方形的上下经度.对东西方做同样的事情,求上下纬度:

The problem is that your point are given as (lat, long) pair, not (x,y) pair, so you can't use "regular" trigonometric methods, like the circle's equation.
Instead, you have to find a square that bounds that circle. Go 90 degrees to the north and south, find the upper and lower longitudes of that square. Do the same to rhe east and west, find the upper and lower latitudes:

现在您可以检查每个点是否在框内,并且您可以通过简单的比较轻松完成:

Now you can check for each point if it's inside the box, and you can do it easily with simple comparsions:

if lon > lon1 and lon < lon2
   and lat > lat2 and lat < lat1

这在计算上真的很便宜.
唯一的问题是蓝色区域内的点:

which is really computionally cheap.
The only problem is with points that are inside the blue area:

它们在正方形内但不在圆内,因此您必须对它们使用Haversine 公式.
如果您的大部分点不在正方形内,此方法将节省您的时间,因为消除它们非常容易.

They are inside the square but not inside the circle, so you'll have to use the Haversine formula for them.
If most of your points are not in the square, this method will save you time, because eliminating them is pretty easy.

这篇关于计算位置和位置列表之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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