PHP point-in-polygon algorithm

The point-in-polygon algorithm allows you to programmatically check if a particular point is inside a polygon or outside of it. A common way to tackle the problem is to count how many times a line drawn from the point (in any direction) intersects with the polygon boundary. If the line and the polygon intersect an even number of times (or not at all), then the point is outside. If they intersect an odd number of times, the point is inside. It is true even for complex forms that have a lot of coordinates and thus create a very precise boundary.

Let’s see a sample image before we get to the code.

This shows how it can be determined if a point is inside or outside a polygon.Here, the lines drawn from point 1 intersect twice or not at all, because it is outside.

Point 2 is inside and thus the lines drawn from it intersect once or three times.

Even in special cases, such as point 3, we see that this method works: the line intersects twice and the point is therefore outside.

I use this approach in the PHP code below, which returns one of these 4 possible values:

  • inside if the point is inside the polygon.
  • outside if, you guessed it, the point is outside of the polygon.
  • vertex if the point sits exactly on a vertex AND $pointOnVertex = true (line 2)
  • boundary if the point sits on the boundary. If $pointOnVertex = false, then boundary is also returned if the point is on a vertex.

Using the point-in-polygon PHP code

Set the point(s) value(s) and an array containing your polygon vertices (in the form “Xcoordinate Ycoordinate”), then call the pointInPolygon function. The first and last polygon coordinates must be identical, to “close the loop”.

As you can see in the following example, it is easy to check multiple points at once. The code also works with negative coordinates, for the polygon as well as for the points to check.

Point in polygon - sampleThis will output:

point 1 (50 70): vertex
point 2 (70 40): inside
point 3 (-20 30): inside
point 4 (100 10): outside
point 5 (-10 -10): outside
point 6 (40 -20): inside
point 7 (110 -20): boundary

14 thoughts on “PHP point-in-polygon algorithm

  1. MrDog

    Very nice and saved me a lot of work. But one query – you say the last point in the polygon must be the same as the first one to close the loop which I understand – but the first one is (-50,30) and the last one is (-50,-30) which are not the same – which implies to me that the last value is not really needed?

    Reply
    1. Michael Post author

      Thanks for pointing that out!
      It was a mistake (now corrected) in the sample. The last coordinate does have to be the same as the first one.

      Regards

      Reply
  2. Rhijul

    Is there a way to optimise the code to run the algorithm for searching multiple points. Basically if we can get the status of all the points whether they are inside or outside. I know we can run the same algorithm over all the points, but then the we will be repeating a lot of our intersection calculations and the algorithm would not be that efficient.

    Reply
  3. Diego

    Thank you so much, the algorithm works like a charm with latitudes/longitudes… I will definitively buy you coffee :D

    Reply
  4. Nilesh Nayak

    Hi,

    I have a set of points as detailed below.

    $pointlocation = new pointLocation();

    $pts[] = “51.22881787980134 -1.157684326171875″;
    $pts[] = “51.138432319543924 -1.043701171875″;
    $pts[] = “51.11990293528057 -0.998382568359375 “;
    $pts[] = “51.12248887705868 -0.9455108642578125″;
    $pts[] = “51.19483648846099 -0.778656005859375″;
    $pts[] = “51.222367645529715 -0.701751708984375″;
    $pts[] = “51.269648373496736 -0.696258544921875″;
    $pts[] = “51.30443311306082 -0.71685791015625″;
    $pts[] = “51.336617716006664 -0.770416259765625″;
    $pts[] = “51.350771787631196 -0.9468841552734375″;

    $centerpoint = “51.263217 -0.9525255999999445″;
    echo $pointlocation->pointInPolygon($centerpoint, $pts);

    This seems to show that the point is outside when in effect, it is inside.

    If I am not wrong, the following statement causes the issue.

    if ($point['y'] > min($vertex1['y'], $vertex2['y']) and $point['y'] <= max($vertex1['y'], $vertex2['y']) and $point['x'] <= max($vertex1['x'], $vertex2['x']) and $vertex1['y'] != $vertex2['y']) {

    I am not sure what this statement is trying to do.

    Can you please shed light on this as we are using this in one of our projects.

    Reply
    1. Michael Post author

      Hi Nilesh,

      Don’t forget that, as mentionned in the article, the first and last point of the polygon array must be identical, to close it ;)
      So, just add $pts[] = “51.22881787980134 -1.157684326171875″; as the last point and the script will return inside, as it should.

      Kind regards,

      Michaël

      Reply
  5. Parth

    hey this how i am using this but it always show outside as result

    $pointLocation = new pointLocation();
    $points=array(“22.560019387 72.907277656″);
    $polygon = array(“72.9443135978394 22.5665187803137″,”72.9671445609741 22.5735726534814″,
    “72.9671445609741 22.5735726534814″,”72.9735818626099 22.5691343034821″,”72.9702344657593 22.5570470009717″,
    “72.9702344657593 22.5570470009717″,”72.9810920478516 22.5427786579349″,”72.9810920478516 22.5427786579349″,
    “72.9810920478516 22.5427786579349″,”72.9496780158692 22.5259718289148″,”72.9496780158692 22.5259718289148″,
    “72.9156890632324 22.5396077146951″,”72.9156890632324 22.5396077146951″,”72.9194656135254 22.5623576184676″,
    “72.9440990211182 22.5663998917189″,”72.9443135978394 22.5665187803137″);

    foreach($points as $key => $point) {
    echo “point ” . ($key+1) . ” ($point): ” . $pointLocation->pointInPolygon($point, $polygon) . “”;
    }

    please help

    parth

    Reply
    1. Michael Post author

      Hi Parth,

      It seems you switched the x and y values of the point, but even if it were x=72.907277656 and y=22.560019387, it would clearly be outside, as none of the polygon coordinates has x less than 7.91

      Regards,

      Michaël

      Reply
  6. Jonas

    Hey,

    great work!
    I have a question, is there a way to make a “hole” in the middle of a polygon (like in the sample picture; point 3).

    Kind Regards from Germany

    Jonas

    Reply
    1. Michael Post author

      Thanks Jonas,
      It is indeed currently not supported, but I’ll try to add it in a future version of the script. Anyway, the basic idea is to check the number of intersections with both boundaries (the external polygon and the “hole”) and add them up. If the total is an odd number (or 0), then the point is either outside or in the hole. I hope that helps.
      Michaël

      Reply

Leave a comment