问题
I am trying to check if the lat,lon is in the polygon or not. Here is my array :
$vertices_x :
Array
(
[0] => -32.581189
[1] => -38.785885
[2] => -39.26384
[3] => -34.919383
[4] => -32.284464
)
$vertices_y:
Array
(
[0] => 170.643905
[1] => 170.424179
[2] => -178.15004
[3] => -176.524063
[4] => -178.325821
)
$longitude_x : 173.5385
$latitude_y : -34.472
$points_polygon = count($vertices_x) - 1;
I am using below function to check :
function is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y) {
$i = $j = $c = 0;
for ($i = 0, $j = $points_polygon; $i < $points_polygon; $j = $i++) {
if ((($vertices_y[$i] > $latitude_y != ($vertices_y[$j] > $latitude_y)) &&
($longitude_x < ($vertices_x[$j] - $vertices_x[$i]) * ($latitude_y - $vertices_y[$i]) / ($vertices_y[$j] - $vertices_y[$i]) + $vertices_x[$i])))
$c = !$c;
}
return $c;
}
And this function always gives me 0 (Not in polygon) but if you check then my point $longitude_x : 173.5385 , $latitude_y : -34.472
is in that polygon area.
I think the above algorithm in the function is only work with positive values.
回答1:
I don't think your point (173.5385,-34.472) is in the polygon.
Your x value is much larger than the largest x vertex. You can see this if you plot it. You can see from the orange point that even if you just have your lat/long mixed up it's still not in the polygon.
EDIT:
Upon first inspection of your new polygon and point it looks as though this one is more likely to be contained within the polygon (x_min < x < x_max and y_min < y < y_max).
-36.236432, 176.467563
-37.936530, 172.688266
-39.801068, 177.895786
-35.345287,-177.446011
-34.625208,-177.907437
point:
(-37.0278,176.6158)
However, plotting this again reveals that the point is outside the polygon:
... and zooming in ...
I plotted these in python with matplotlib and I recommend you do something similar when debugging this type of thing. If you want to stay with php, html, etc., you could use svg polygons or html canvas instead.
来源:https://stackoverflow.com/questions/61302366/point-in-polygon-algorithm-giving-wrong-results-for-negative-points