获得从一个点的最短距离 [英] Get the shortest distance from a point

查看:168
本文介绍了获得从一个点的最短距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个表源码

Locations
ID
Lat ( latitude)
Lon ( longitude) 
Type
Name
City

我有,例如100条记录 我需要的是得到(用我自己的坐标)的最近点在我的表。

I have for example 100 records what I need is to get (using my own coordinates) the nearest point in my table.

我所做的是获得的最短距离我目前的点,每一个在表之间,并返回最短的一个,但我正在寻找一个更好的解决方案。

What I did is to get the shortest distance between my current point and each one in the table, and return the shortest one, but I am searching for a better solution

感谢

推荐答案

一个可能的解决方案是使用一个网格为全地图的有兴趣和$ P $对 - 分配指向一个特定的行/列。然后:

A possible solution is to use a grid for the whole map your are interested in and pre-assign points to a particular row/column. Then:

  1. 在计算新点的网格位置 - 一列添加到数据库,这个
  2. 计算在当前电网的所有坐标的距离 - 如果存在
  3. 您还需要计算下一个网格在所有的距离(你是不太可能完全集中在当前的广场上,你总是需要从一个你最好的比赛是在检查一格的距离了。)

如果你需要做计算的数量减少了很多。

Should cut down a lot on the number of calculations you need to do.

如果你希望总能找到在X距离的位置,您可以查询X / Y COORDS将属于该范围内的COORDS +/- x KM(正方形)内计算,如果他们再掉下来的XKM圆圈内你的观点,然后选择最短的。

If you expect to always find a location within X distance you could query for x/y coords that would fall within that range your coords +/- x KM (a square), calculate if they then fall within the xKM circle from your point, and then choose the shortest.

更新 - 网格选项

我假设你已经在做两点之间计算的距离并不会描述。

I am assuming you already are doing the distance between two points calculation and will not describe that.

如果你有一本地图册方便,你可以看到一个例子看的地方在索引中。它会给你一个网页,就像M5一个网格位置。如果你去到该页面将有行标有数字和字母列,如果你在第M行和列5相交,你会发现这个城市有广场。要做到这一点为你的系统,你需要:

If you have an atlas handy you can see an example by looking a place up in the index. It will give you a page and a grid location like M5. If you go to that page it will have rows and columns labeled with numbers and letters and if you look in the square where row M and column 5 intersect you will find the city there. To do it for your system you need to:

  1. 确定网格应为多大。(如何密集的是你点 - 不会有什么好有一个大的网格,所有的点土地一平方)
  2. 在每个点计算它是在网格中。如果你的多边形是复杂的存在多边形code吨的点在那里进行复制。如果(我的例子),你只需要使用方块,你只需要确定哪些行/列各点之间。
  3. 查看地图用户位置和最近点的例子:

因此​​,如果用户是绿色标记,他将在C4中。您会搜索所有其他点在C4和确定该最接近的是#2。然后,你还必须签一个网格出周围的所有道路,以确保有没有比你找到一个更紧密的项目,所以这包括正方形:B3,B4,B5,C3,C5,D3,D4,D5 。当你做,你会选择#3,从C3,你就完成了。

So if the user is the green marker, he would be in C4. You would search all other points in C4 and determine that the closest is #2. Then you would also have to check one grid out all the way around to make sure there wasn't a closer item than the one you found, so this includes squares: B3,B4,B5,C3,C5,D3,D4,D5. When you do you will pick #3 from C3 and you are finished.

如果用户已经在广场D2里没有其他的点你会发现你的第一场比赛在说C2。当检查的C1,C2,C3,D1,D3,E1,E2,E3。一旦找到,那么你就需要再次检查其它半径出来,这将对为:B0-4,C0,C4,D0,D4,E0,E4,F0-4。等等。你可以看到,网格的选择将是重要的,使这个尽可能高效。

If the user had been in square D2 where there are no other points your would have found your first match in say C2. When checking C1,C2,C3,D1,D3,E1,E2,E3. Once found you would then again need to check another radius out, which would have be: B0-4, C0,C4,D0,D4,E0,E4,F0-4. etc. You can see that grid selection will be important to make this as efficient as possible.

另外请注意,此假定您的网格相等不像我的手绘制的例子。

Also Note this assumes your grids are equal unlike my hand drawn example.

选项2:

如果您希望X公里之内的结果,而你想要的东西你的数据库会计算你很快就能做到这一点:

If you expect a result within X km, and you want something your DB will calculate quickly you can do this:

LatMin = currentLatCoord-radiusValInDegrees
LatMax = currentLatCoord+radiusValInDegrees
LonMin = currentLonCoord-radiusValInDegrees
LonMax = currentLonCoord+radiusValInDegrees

SELECT * 
From Locations 
WHERE Lat BETWEEN LatMin AND LatMax
  AND Lon BETWEEN LonMin AND LonMax

现在这个给你所有的结果在一个广场。重要的是,你再检查他们实际上在圈内 - 你需要将降大任于角落,有可能比那些在圆圈的边缘接近坐标。因此,对于每个点检查它是否是圈内第一(<一href="http://stackoverflow.com/questions/481144/equation-for-testing-if-a-point-is-inside-a-circle">Equation用于测试点是否圆的内部)然后计算距离,并​​保持最近的一个。如果你没有得到结果,拓宽循环。

Now this gives you all results in a square. It is important that you then check they are actually in the circle - you need to drop any in the corners as there may actually be closer coordinates than those on the edge of the circle. So for each point check if it is inside the circle first (Equation for testing if a point is inside a circle) then calculate the distance and keep the closest one. If you do not get a result, widen the circle.

此外,选择一个良好的半径将取决于您的数据。

Again, selecting a good radius will depend on your data.

这篇关于获得从一个点的最短距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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