【问题标题】:How to check if a lat/long coordinate exists or doesn't exist inside a polygon - PHP如何检查多边形内是否存在纬度/经度坐标 - PHP
【发布时间】:2015-02-18 14:28:30
【问题描述】:

我想用 PHP 编写一些代码来检查单个纬度/经度坐标与多边形的坐标。并返回真或假是否存在于多边形内。

例如我在这里创建了一个多边形.. http://www.geocodezip.com/polygonTool.asp

0: 53.34545,-6.255083 1:53.340121,-6.239033 2:53.338788,-6.238947 3:53.337405,-6.240492 4:53.334227,-6.245642 5:53.332074,-6.252937 6:53.330024,-6.264353 7:53.333766,-6.264868 8:53.33761,-6.265812 9:53.338583,-6.266155 10: 53.341607,-6.265383 11: 53.342683,-6.264439 12: 53.344067,-6.264696 13: 53.344733,-6.259632

我有一个用户位置... 53.338839, -6.249386(在这个多边形中确实存在)。

有没有一种简单的方法来验证它是在多边形内部还是外部?

【问题讨论】:

  • 循环遍历你的多边形点数组,确保你传入的值在任何一个方向上都不会超过它们
  • 为什么这个标签是 google-maps-api-3?客户端是javascript,服务器是php。

标签: php google-maps geolocation geocoding


【解决方案1】:

确定一个点是否在多边形中的一种方法是计算从该点(在任何方向)绘制的线与多边形边界相交的次数。如果它们相交的次数为偶数次,则该点在外部。

以下 PHP 代码需要 2 个数组 $polyX = 多边形的经度点数组。重复第一个点以闭合多边形。 $polyy = 多边形的纬度点数组,

$polySides  = 13; //how many corners the polygon has
$polyX = array(53.34545,53.340121,53.338788,53.337405,53.334227,53.332074,53.330024,53.333766,53.33761,53.338583,53.341607,53.342683,53.344067,53.344733,53.34545);
$polyY = array(6.255083,-6.239033,-6.238947,-6.240492,-6.245642,-6.252937,-6.264353,-6.264868,-6.265812,-6.266155,-6.265383,-6.264439,-6.264696,-6.259632,6.255083);
$x =53.338839;//your coordinates
$y =-6.249386;


function pointInPolygon($polySides,$polyX,$polyY,$x,$y) {
  $j = $polySides-1 ;
  $oddNodes = 0;
  for ($i=0; $i<$polySides; $i++) {
    if ($polyY[$i]<$y && $polyY[$j]>=$y 
 ||  $polyY[$j]<$y && $polyY[$i]>=$y) {
    if ($polyX[$i]+($y-$polyY[$i])/($polyY[$j]-$polyY[$i])*($polyX[$j]-$polyX[$i])<$x)    {
    $oddNodes=!$oddNodes; }}
   $j=$i; }

  return $oddNodes; }


 if (pointInPolygon($polySides,$polyX,$polyY,$x,$y)){
  echo "Is in polygon!";
}
else echo "Is not in polygon";

请参阅 this 了解 JavaScript 实现。

【讨论】:

    【解决方案2】:

    来自我的Geodetic classes,基本功能是:

    function isInRegion(Geodetic_LatLong $position)
    {
        $latitude = $position->getLatitude()->getValue();
        $longitude = $position->getLongitude()->getValue();
        $perimeterNodeCount = count($this->_nodePoints);
        $jIndex = $perimeterNodeCount - 1 ;
        $oddNodes = FALSE;
        for ($iIndex = 0; $iIndex < $perimeterNodeCount; ++$iIndex) {
            $iLatitude = $this->_nodePoints[$iIndex]->getLatitude()->getValue();
            $jLatitude = $this->_nodePoints[$jIndex]->getLatitude()->getValue();
            if (($iLatitude < $latitude && $jLatitude >= $latitude) ||
                ($jLatitude < $latitude && $iLatitude >= $latitude)) {
                $iLongitude = $this->_nodePoints[$iIndex]->getLongitude()->getValue();
                $jLongitude = $this->_nodePoints[$jIndex]->getLongitude()->getValue();
                if ($iLongitude +
                    ($latitude - $iLatitude) /
                    ($jLatitude - $iLatitude) * ($jLongitude - $iLongitude) < $longitude) {
                    $oddNodes = !$oddNodes;
                }
            }
            $jIndex = $iIndex;
        }
        return $oddNodes;
    }
    

    【讨论】:

    • 如果您考虑正在执行的实际算法,它与大卫斯特拉坎的回答中描述的基本相同:计算从点(任何方向)绘制的线与多边形边界相交的次数,如果它们相交偶数次,则该点在多边形之外
    猜你喜欢
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 2017-08-08
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多