问题描述
我们目前正在使用以下算法来检测地理点是否位于复杂多边形内.这可以正常工作,除非多边形穿过 180° 经线.
We are currently using the following algorithm to detect if a geographic point is inside a complex polygon or not. This works fine, except when the polygon crosses the 180° longitude line.
例如在多边形 160,65,0 160,15,0 -160,15,0 -160,65,0 160,65,0 中未检测到点 (-170, 60)
For example the point (-170, 60) is not detected in polygon 160,65,0 160,15,0 -160,15,0 -160,65,0 160,65,0
看下图:[图片]http://tinypic.com/r/14x2xl1[/img]我想要红色框中的所有内容.不是黄框!
Look at the following image: [Img]http://tinypic.com/r/14x2xl1[/img] I want everything in the red box. Not the yellow box!
public static bool IsCoordinateInPolygon(IList<KMLCoordinate> polygon, KMLCoordinate testPoint)
{
bool result = false;
int j = polygon.Count - 1;
for (int i = 0; i < polygon.Count; i++)
{
if (polygon[i].Latitude < testPoint.Latitude && polygon[j].Latitude >= testPoint.Latitude || polygon[j].Latitude < testPoint.Latitude && polygon[i].Latitude >= testPoint.Latitude)
{
if (polygon[i].Longitude + (testPoint.Latitude - polygon[i].Latitude) / (polygon[j].Latitude - polygon[i].Latitude) * (polygon[j].Longitude - polygon[i].Longitude) < testPoint.Longitude)
{
result = !result;
}
}
j = i;
}
return result;
}
有人有更好的算法吗?
推荐答案
球坐标系有它的怪癖
为了避免它们,请改用 3D 正交/正交笛卡尔坐标系
To avoid them use 3D orthogonal/orthonormal cartesian coordinate system instead
转换多边形顶点和地理位置
所以 (long,lat,alt) ->(x,y,z)
.这里您可以找到操作方法.您不需要仅应用第一个球面到 3D 笛卡尔变换的局部变换(项目符号 #1).
so (long,lat,alt) -> (x,y,z)
. here you find how to do it. You do not need to apply local transform just the first spheric to 3D cartesian transformation (bullet #1.)
使用任何内部多边形测试...
我通常会计算从您的地理位置投射到任何方向的线与多边形边界线之间的交叉点数.
I usually count the number of intersections between line cast from your geolocation to any direction and polygon boundary lines.
- 如果是奇数则点在里面
- 如果是偶数,那么点在外面
- 如果点位于多边形的任何一条线上,那么它在里面
- 如果您的投射线击中任何顶点,请记住(不要计算此顶点的多次击中)或稍微改变方向并重试
[备注]
别忘了把所有的都当作 3D 向量而不是 2D 来处理!!!
这篇关于检测地理位置是否在复杂多边形中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!