如何使用 PHP 对 KML 文件中的坐标进行重新排序和重新组织


How Can You Reorder And Reorganize Coordinates From A KML File With PHP?

根据 http://assemblysys.com/php-point-in-polygon-algorithm/,我可以采用一组形成多边形的点,并确定一个点是位于多边形内部还是外部。我还创建了一个 KML 文件,当 JavaScript 使用来确定哪些点在里面时,但是我的最终目标是让每个标记在 MySQL 表内包含一个额外的数据集,该数据集将存储此数据以供以后使用,从而加快地图加载时间。

浏览 KML 文件,每个多边形都有一组由",0.0"分隔的坐标,这意味着意义不大,因为根据上述文档,我只需要一组纬度和经度。此外,纬度和经度位于不同的地方。下面是一个连接示例:

-86.1459875,39.8622513,0.0 -86.1459875,39.8555639,0.0 -86.1398077,39.8556628,0.0 -86.1398077,39.862218399999996,0.0

-86.1459875,39.8622513,0.0

相反,我希望该集如下所示:

39.8622513 -86.1459875, 39.8555639 -86.1459875, 39.8556628 -86.1398077, 39.862218399999996 -

86.1398077, 39.8622513 -86.1459875

下面是我的代码示例,但是我不确定是否有更简单的方法可以使用PHP操作数组,而无需使用多个for循环或foreach循环。

$kml = 'document.xml'; 
//get the total number polygons 
$numPoly = count( $kml->Document[0]->Folder);
$newArray = array(); 
$a = 1; 
$explode;
$explode['coordinates']; 
for( $i=0; $i <= $numPoly; $i++)
{
    $numPlace = count( $kml->Document[0]->Folder[$i]->Placemark); //count   the number of placemarks there are 
for($z = 0; $z <= $numPlace; $z++) 
{
    $regionName = $kml->Document[0]->Folder[$i]->Placemark[$z]->name."</br>"; //grab each placemarks name 
    //get the cordinates inside of each placemark 
    $regionCords = $kml->Document[0]->Folder[$i]->Placemark[$z]->Polygon->outerBoundaryIs->LinearRing->coordinates;
    //print_r($regionCords); print "<br/><br/>";
    foreach($regionCords as $num2 => $region)
    {
        print $a;
        $explodeCords = array_splice( explode('|', str_replace(',0.0',' |', $regionCords) ), 0, -1 ) ;
        foreach ($explodeCords as $exploded)
        {
            $exploded = explode(',', $exploded);
            $exploded1 = $exploded[0];
            $exploded2 = $exploded[1];
            $explodedString = $exploded2.$exploded1.",";
            //echo $explodedString; echo "<br/><br/>";
        }
    }
    $numCordinates =  count($explodeCords['coordinates'] ); //returns the number of [lat,long] cords in each cordinate set
    if ( !empty( $regionCords ) )
    {
        $a++;
    }
}
}

如果我走在正确的道路上,或者是否有另一种方法可以让您使用大多数数组函数和 PHP 轻松切换这两个数字,

请告诉我。

解决方案:在每个 foreach 循环之后,最佳做法是将新数组从循环中删除,因为如果要添加另一个 foreach 循环,可能会导致不需要的效果,例如以倍数将值附加到新数组。

从 foreach 循环中删除值后,始终可以使用array_map来切换坐标。有关更完整的示例,请参阅以下 Github 存储库:https://github.com/jdmagic21/GMapParse