查找由纬度/经度GPS对构成的矩形区域内的坐标 [英] Find coordinates inside a rectangular area constructed by lat/long GPS pairs
问题描述
我从不处理基于位置的数据,因此对于整个GPS编码相关的问题来说,这是非常新的。我有一个问题,我似乎没有找到一种非常有效的方法来解决它,或者可能是我不太确定这种算法。
让你说给出了4个纬度/经度坐标,它们构成了某种矩形区域:(X0,Y0),(X1,Y0),(X0,Y1),(X1,Y1)
------------------------ -----------
| b |
|一个|
| | d
| |
| c |
-----------------------------------
e
是否可以找到给定矩形区域内的所有点: a,b,c
区域之外的所有点? e,d
我可以轻松地构建2D矩阵来做到这一点,但这仅在坐标为整数,但是对于经纬度/经度对,坐标通常是浮点数,我们不能用它来构建2D表。
有什么好主意吗?
编辑1:
此射线广播算法?
如果矩形是轴对齐的,这是用于GPS坐标的好算法吗?
@Eyal的答案是正确的答案(您实际上不需要8个值,但4个就足够了。)
如果处理旋转的矩形(适用于任何矩形)四边形),则采用射线法是合适的:考虑穿过测试点的水平线Y = Yt并找到与测试点交叉的边缘(上方一个端点,下方一个端点)。将有0或2个这样的边。如果是0,则您在外面。否则,计算这些边缘与直线的交点的横坐标。如果0或2交点位于测试点的左侧,则您在外面。
Xi = Xt +(Yt-Y0 )(X1-X0)/(Y1-Y0)
I've never deal much with location-based data, so very much new to the whole GPS coding related questions. I have a problem that I don't seem to find a very efficient way in solving it or maybe there's an algorithm that I'm not too sure.
Let said you have given 4 lat/long coordinates which construct some kind of a rectangular area: (X0, Y0), (X1, Y0), (X0, Y1), (X1, Y1)
-----------------------------------
| b |
| a |
| | d
| |
| c |
-----------------------------------
e
Is there a way to find all the point that are inside the given rectangular area : a, b, c
And all the points outside of the area? e, d
I can easily to construct a 2D matrix to do this, but that's only if the coordinates are in integer, but with lat/long pairs, the coordinates are usually in float numbers which we cannot use it to construct a 2D table.
Any cool ideas?
Edited 1:
What about this Ray-casting algorithm? Is this a good algorithm to be used for GPS coordinates which is a float number?
If your rectangle is axis-aligned, @Eyal's answer is the right one (and you actually don't need 8 values but 4 are enough).
If you deal with a rotated rectangle (will work for any quadrilateral), the ray-casting method is appropriate: consider the horizontal line Y=Yt through your test point and find the edges that cross it (one endpoint above, one endpoint below). There will be 0 or 2 such edges. In case 0, you are outside. Otherwise, compute the abscissas of the intersections of these edges with the line. If 0 or 2 intersection are on the left of the test point, you are outside.
Xi= Xt + (Yt - Y0) (X1 - X0) / (Y1 - Y0)
这篇关于查找由纬度/经度GPS对构成的矩形区域内的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!