Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Point in polygon PHP

i have a typical question with the Geometric datatype of mysql, polygon.

I have the polygon data, in the form of an array of latitudes and longitudes, ex:

[["x":37.628134,  "y":-77.458334], ["x":37.629867,   "y":-77.449021], ["x":37.62324,    "y":-77.445416], ["x":37.622424,   "y":-77.457819]] 

And i have a point (Vertex) with coordinates of latitude and longitude, ex:

$location = new vertex($_GET["longitude"], $_GET["latitude"]); 

Now i want to find whether this vertex (point) is inside the polygon. How can i do this in php ?

like image 466
shasi kanth Avatar asked Feb 21 '11 10:02

shasi kanth


People also ask

How do you find a point inside a polygon?

Draw a horizontal line to the right of each point and extend it to infinity. Count the number of times the line intersects with polygon edges. A point is inside the polygon if either count of intersections is odd or point lies on an edge of polygon.

How do you determine if a point is inside a convex polygon?

The point will be inside a convex polygon if and only if it lies on the same side of the support line of each of the segments. That is, either it lies on the left of every line or it lines on the right of every line.

How do you get a list of points in a polygon in Python?

One of the simplest and most efficient ways of getting a list of points inside a polygon with Python is to rely on the Geopandas library and perform a spatial join. To do this, we simply create one geodataframe with points and another one with polygons, and join them with 'sjoin'.

Which test is used to check whether a point is inside or outside of the polygon?

One simple way of finding whether the point is inside or outside a simple polygon is to test how many times a ray, starting from the point and going in any fixed direction, intersects the edges of the polygon. If the point is on the outside of the polygon the ray will intersect its edge an even number of times.


1 Answers

This is a function i converted from another language into PHP:

$vertices_x = array(37.628134, 37.629867, 37.62324, 37.622424);    // x-coordinates of the vertices of the polygon $vertices_y = array(-77.458334,-77.449021,-77.445416,-77.457819); // y-coordinates of the vertices of the polygon $points_polygon = count($vertices_x) - 1;  // number vertices - zero-based array $longitude_x = $_GET["longitude"];  // x-coordinate of the point to test $latitude_y = $_GET["latitude"];    // y-coordinate of the point to test  if (is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)){   echo "Is in polygon!"; } else echo "Is not in polygon";   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; } 

Additional: For more functions i advise you to use the polygon.php class available here. Create the Class using your vertices and call the function isInside with your testpoint as input to have another function solving your problem.

like image 89
Thariama Avatar answered Oct 01 '22 19:10

Thariama