Check if Google Map Point is in polygon from PHP

11,409

Solution 1

Did you try searching for "php point in polygon" in your favourite search engine? Top hit:

http://assemblysys.com/php-point-in-polygon-algorithm/

It uses a scanline algorithm, and there's some examples. All you need to do is read your polygon file into the right format (you neglected to say what format you have) and call the function.

Solution 2

You can try somthing like this (in php should be similar):

int iCheck=0;    
for (i = 0, v = HowManyVecotrsHasThePolygon - 1; i < HowManyVecotrsHasThePolygon; v = i++)
                    {
                        if (((vectorPointLatitud[i] > ptoLatitud) != (vectorPointLatitud[v] > ptoLatitud)) && (ptoLongitud < (vectorPointLongitud[v] - vectorPointLongitud[i]) * (ptoLatitud - vectorPointLatitud[i]) / (vectorPointLatitud[v] - vectorPointLatitud[i]) + vectorPointLongitud[i]))
                            iCheck++;
                    }

if iCheck is pair the point is outside, even inside

Checkout Polygons Eric Haines. I got the idea from him.

The idea is you've to create a Ray from your point, and check how many intersections between this ray and the Polygons vectors

The algorithm is just a bit of algebra, that you can check in any book.

Share:
11,409
Absulit
Author by

Absulit

Code and Art

Updated on June 04, 2022

Comments

  • Absulit
    Absulit almost 2 years

    I've been looking for a way to check if a point is part of a polygon; this polygon is loaded from a file.

    All the answers related to this question are solved with javascript, but I require to do this on server-side; this because the result does not need to be shown to the user as a webclient, it needs to be stored and later be used as a parameter to select a group of users (that use the system) inside that area (polygon).

    I looked for a Google Maps API for PHP but it looks like it does not exists at all. I found this one, but it is not related to Google and also focuses on the front end.

    I also looked for a REST API; it would have been relatively easy to load the content to my php and parse it, but looks like Google put all its efforts on the JS API.

    Is there any workaround for this?

    Edit 1: As @Spacedman requested, the file format is a KML

    Clarification 1: I expected that Google provide a tool for this (as it exists with JS); parsing the file to check via an algorithm is a posibility and I'll have to check if it works properly.

  • Absulit
    Absulit over 10 years
    I've accepted this answer because in the end it is a solution, but not the one I expected, because I thought that Google could provide a simple tool (function, class) to parse the file. In the end my solution took a while; as you said, parsing the file and using point in polygon algorithms.
  • Coded Container
    Coded Container over 9 years
    This would only be applicable if the page was fully loaded in a browser, correct? You couldn't automatically figure out the points using a cron job right?