在多边形中查找点 PHP
我有一个关于 mysql 几何数据类型多边形的典型问题。
我有多边形数据,以纬度和经度数组的形式,例如:
[["x":37.628134, "y":-77.458334],
["x":37.629867, "y":-77.449021],
["x":37.62324, "y":-77.445416],
["x":37.622424, "y":-77.457819]]
我有一个带有纬度和经度坐标的点(顶点),例如:
$location = new vertex($_GET["longitude"], $_GET["latitude"]);
现在我想知道这个顶点(点)是否在多边形。 我怎样才能在 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是我从另一种语言转换为 PHP 的函数:
附加:
对于更多功能,我建议您使用 Polygon.php 类此处提供。
使用您的顶点创建类,并使用您的测试点作为输入调用函数
isInside
,让另一个函数解决您的问题。This is a function i converted from another language into PHP:
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.上面的热门答案包含错别字。在其他地方,此代码已被清理。更正后的代码如下:
The popular answer above contains typos. Elsewhere, this code has been cleaned up. The corrected code is as follows:
上面的解决方案没有按我的预期工作,您可以选择下面的解决方案
使用 PHP
, 而不是使用上面的解决方案
使用 MySql
Above solution is not working as i expect, instead of using the above solution you can prefer below solutions
With PHP
With MySql
如果您的多边形是自闭合的,也就是说它的最终顶点是最后一个点和第一个点之间的线,那么您需要在循环中添加一个变量和一个条件来处理最终顶点。您还需要将顶点数传递为等于点数。
这是为处理自闭合多边形而修改的已接受答案:
谢谢!我发现这个页面和它被接受的答案非常有帮助,我很自豪能够提供这个变体。
If your polygons are self-closing, that is to say that it's final vertex is the line between it's last point and it's first point then you need to add a variable and a condition to your loop to deal with the final vertex. You also need to pass the number of vertices as being equal to the number of points.
Here is the accepted answer modified to deal with self-closing polygons:
Thank you! I found this page and it's accepted answer very helpful and I am proud to offer this variation.
我将泰国多边形放入MySQL。并将接受的答案函数与 MySQL 8 中的内置函数进行比较。
这是上面有点的多边形 - RED 是第一个,BLUE - 最后一个:
I使用 https://www.gpsvisualizer.com/draw/ 并制作屏幕以可视化所有点。
我给了点作为 PHP 函数的坐标 + 使用查询与 MySQL 函数比较结果:
结果:
我尝试更改多边形,但 php 函数总是在这些点上出错,这意味着某个地方存在我找不到的错误。
更新 1
找到解决方案 assemblysys.com/php-point-in-polygon-algorithm - 该算法与 MySQL 算法相同!
更新 2
比较了 PHP 与 MySQL 的速度(我认为 PHP 应该更快),但事实并非如此。比较 47k 点。
I put Thailand polygon into MySQL. And compared accepted answer function with built-in function in MySQL 8.
Here is polygon with dots above - RED is 1st, BLUE - last:
I draw some dots outside and inside Thailand Polygon on the map using https://www.gpsvisualizer.com/draw/ and made screen to visualize all the dots.
I gave dots as coordinates for PHP function + compared results with MySQL function using query:
The result:
I tried to change polygon, but php function always making mistakes about those dots, means somewhere there is bug which I could not find.
Update 1
Found solution assemblysys.com/php-point-in-polygon-algorithm - this algo works same as MySQL algo!
Update 2
Compared PHP speed vs MySQL (I was thinking that PHP should be much more faster), but no. Compared 47k dots.
这是一个可能的算法。
编写代码留作练习。 :)
Here's a possible algorithm.
Writing the code is left as an exercise. :)
更新了代码,这样我将更容易使用谷歌地图:
它接受如下数组:
因此与谷歌地图一起使用会更容易:
Updated code so i will be easier to use with google maps:
It accept array like:
So it will be easier to use with google maps:
我在 php codeigniter 中创建了代码,在我的控制器中我创建了两个函数,如下所示
另一个用于检查 lat-lng 的函数如下
为了您的测试目的,我通过了下面的内容
我的多边形
I have created code in php codeigniter, in my controller i have create two functions like below
Another function for check the lat-lng is below
For your testing purpose i passed below things
My Polygon