Check if Google Map Point is in a polygon with PHP

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

All answers related to this question are solved using javascript, but I demand to do this on the server side; this is because the result should not be displayed to the user as a web client, it must be saved and then used as a parameter to select a group of users (using the system) inside this area (polygon).

I was looking for the Google Maps API for PHP, but it looks like it doesn't exist at all. I found this one , but it is not related to Google, and also focuses on the interface.

I also looked for a REST API; it would be relatively easy to upload the content to my php and parse it, but it looks like Google has made every effort to the JS API.

Is there any workaround for this?

Change 1 . At the request of @Spacedman, the file format is KML

Clarification 1 . I expected Google to provide a tool for this (as it exists with JS); parsing the file for checking by the algorithm is likely, and I will need to check if it works correctly.

+6
source share
3 answers

Have you tried finding "php point in polygon" in your favorite search engine? Top hit:

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

It uses the scanline algorithm, and there are some examples. All you have to do is read your polygon file in the desired format (you neglected to say which format you have) and call the function.

+6
source

You can try something like this (in php it should look like):

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 a pair, the point is outside, even inside

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

The idea is that you have to create a ray from your point and check how many intersections there are between this ray and Polygons

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

+1
source

You can use the PHP V8 PECL extension to execute javascript in PHP. Alternatively, you can invoke the node.js script with a shell command (which essentially does the same thing).

http://php.net/manual/en/book.v8js.php

http://php.net/manual/en/function.shell-exec.php

0
source

Source: https://habr.com/ru/post/949915/


All Articles