-2

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.

  • As you can see [here](https://stackoverflow.com/questions/5065039/find-point-in-polygon-php) that algorithm has problems with negative values. Alternatives and solutions are available on that thread – Claudio Apr 19 '20 at 10:17
  • That thread is also not helpful –  Apr 19 '20 at 11:18

1 Answers1

2

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.

enter image description here

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:

enter image description here

... and zooming in ...

enter image description here


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.

compuphys
  • 1,289
  • 12
  • 28
  • And what about this polygon : `-36.236432,176.467563|-37.93653,172.688266|-39.801068,177.895786|-35.345287,-177.446011|-34.625208,-177.907437` and point is `-37.0278,176.6158` –  Apr 22 '20 at 06:03
  • Thank you for your answer but if you check in google maps then it shows this point in new polygone. I have taken this polygon from google maps only . –  Apr 22 '20 at 07:55
  • Ahh we're talking about lat/long actually in a 3D sense ... I thought they were just your variable names ... this is a whole different ball game. Does this post help: https://gis.stackexchange.com/a/46720? – compuphys Apr 22 '20 at 08:05