查找 lat/long 点是否在坐标定义的多边形内

Finding if lat/long point is inside a polygon defined by coordinates

我目前正在获取用于定义所见多边形的 KML here

我正在使用算法 detailed in this answer 来确定点是否在多边形内。

我遇到的问题是该点是否在多边形的空白部分内,如下所示。

Click to see the image!

如果我传递印第安纳波利斯的坐标(箭头所示),算法仍然说该点在浅绿色多边形内,这是错误的。

当我使用定义上图的 KML 传递印第安纳波利斯的坐标时,这些是结果。

string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Slight Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Slight Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is outside Marginal Risk
string(22) "39.7774501,-86.1090119" 39.7774501,-86.1090119 is inside General Thunder

有人知道如何修改算法使其正常工作吗?

位于绿色边界内,但您只想用另一个多边形覆盖它。

假设您有多个多边形,可能值得跟踪多边形是带阴影的 (True) 还是不带阴影的 (False),或者根据您的情况确定的其他组合算法确实如此。

将着色状态作为参数传递给 testpointInPolygon 函数(类似于函数 test($lat, $lng, $shaded)),如果 [=17] 则反转 pointInPolygon 的输出=] 是 False.

function pointInPolygon($p, $polygon, $shaded) {
    //if you operates with (hundred)thousands of points
    //rest of code
    $output = $c%2!=0;
    if (!$shaded) { //if the area is not shaded, negate the output
        return !$output;
    } else {
        return $output;
    }
}

要找到外部循环,请制作一个多边形数组,然后遍历,检查所有组合,其中的组合在其中(使用您已有的算法)。检查一个点到另一个完整的多边形应该可行。从不在其他多边形内部的多边形将是外部循环(阴影)。其他的都在里面,而且是倒置的,没有阴影。

当然,这是假设循环从不交叉。

您需要使用该算法测试所有多段线中的点。如果它在外部循环内,而不是在 any 内部循环内,则它是有阴影的;如果它在外部循环内,并且在内部循环内,则它没有阴影。