Check if Google Map Point is in polygon from PHP

2019-02-22 07:34发布

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.

3条回答
三岁会撩人
2楼-- · 2019-02-22 07:51

You could use the PHP V8 PECL extension to execute javascript within PHP. Alternatively, you could call a node.js script using 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

查看更多
再贱就再见
3楼-- · 2019-02-22 07:55

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.

查看更多
ら.Afraid
4楼-- · 2019-02-22 07:59

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.

查看更多
登录 后发表回答