将KML导入MySQL

问题描述 投票:0回答:1
php mysql xml kml
1个回答
0
投票

坐标值似乎是用空格而不是换行符分隔的。 Xpath可以简化代码,并且可以标准化坐标值。

$document = new DOMDocument();
$document->loadXML(getKMLString());
$xpath = new DOMXpath($document);
// register prefix for namespace
$xpath->registerNamespace('kml', 'http://www.opengis.net/kml/2.2');

$records = [];
// iterate placemarks with polygon descendants
foreach ($xpath->evaluate('//kml:Folder/kml:Placemark[count(.//kml:Polygon) > 0]') as $placemark) {
    // fetch the first polygon inside $placemark
    foreach ($xpath->evaluate('(//kml:Polygon)[1]', $placemark) as $polygon) {
        // fetch coordinates as string with all whitespaces normalized to single space characters
        $coordinates = parseCoordinates($xpath->evaluate('normalize-space(.//kml:coordinates)', $polygon));
        $records[] = [
            // fetch $placemakr name as string
            'name' => trim($xpath->evaluate('string(kml:name)', $placemark)),
            // compile $coordinates array to Polygon((...))
            'polygon' => coordinatesToPolygon($coordinates),
            'coordinates' => $coordinates,
        ];
        
    }
    
}
var_dump($records);

// parse coordinates into a nested array
function parseCoordinates(string $coordinates) {
    return array_map(
        function($coordinate) {
            [$latitude, $longitude] = explode(',', $coordinate);
            return [$latitude, $longitude];
        },
        explode(' ', $coordinates)
    );
}

function coordinatesToPolygon(array $coordinates) {
    return 'Polygon(('.implode(
        ', ', array_map(fn ($coordinate) => implode(' ', $coordinate), $coordinates)
    ).'))';
}

function getKMLString() {
    return <<<'XML'
<?xml version="1.0" encoding="utf-8" ?>
<kml
xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="secc_cpv_e_20111101_01_r_ine" id="secc_cpv_e_20111101_01_r_ine">
<SimpleField name="Name" type="string"></SimpleField>
</Schema>
<Folder>
<name>secc_cpv_e_20111101_01_r_ine</name>
<Placemark>
<name>5200101001</name>
<Style>
<LineStyle>
<color>ff0000ff</color>
</LineStyle>
<PolyStyle>
<fill>0</fill>
</PolyStyle>
</Style>
<MultiGeometry>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>-2.93698273596522,35.2979346176443,0 -2.93697084790041,35.2979326186938,0 -2.93695877204193,35.297933090301,0 -2.93694953435202,35.2979385903615,0 -2.93694137509274,35.2979447898008,0 -2.93692885164904,35.297946595687,0 -2.93691871628878,35.2979409773216,0 -2.93691413103252,35.2979323321398,0 -2.93690399979109,35.2979270789636,0 -2.93689431492369,35.2979220514443,0 -2.93688289797324,35.2979224151838,0 -2.93687516563971,35.2979298005764,0 </coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</MultiGeometry>
</Placemark>
</Folder>
</Document>
</kml>
XML;
}

输出:

array(1) {
  [0]=>
  array(3) {
    ["name"]=>
    string(10) "5200101001"
    ["polygon"]=>
    string(439) "Polygon((-2.93698273596522 35.2979346176443, -2.93697084790041 35.2979326186938, -2.93695877204193 35.297933090301, -2.93694953435202 35.2979385903615, -2.93694137509274 35.2979447898008, -2.93692885164904 35.297946595687, -2.93691871628878 35.2979409773216, -2.93691413103252 35.2979323321398, -2.93690399979109 35.2979270789636, -2.93689431492369 35.2979220514443, -2.93688289797324 35.2979224151838, -2.93687516563971 35.2979298005764))"
    ["coordinates"]=>
    array(12) {
      [0]=>
      array(2) {
        [0]=>
        string(17) "-2.93698273596522"
        [1]=>
        string(16) "35.2979346176443"
      }
      [1]=>
      array(2) {
        [0]=>
        string(17) "-2.93697084790041"
        [1]=>
        string(16) "35.2979326186938"
      }
      [2]=>
      array(2) {
        [0]=>
        string(17) "-2.93695877204193"
        [1]=>
        string(15) "35.297933090301"
      }
      [3]=>
      array(2) {
        [0]=>
        string(17) "-2.93694953435202"
        [1]=>
        string(16) "35.2979385903615"
      }
      [4]=>
      array(2) {
        [0]=>
        string(17) "-2.93694137509274"
        [1]=>
        string(16) "35.2979447898008"
      }
      [5]=>
      array(2) {
        [0]=>
        string(17) "-2.93692885164904"
        [1]=>
        string(15) "35.297946595687"
      }
      [6]=>
      array(2) {
        [0]=>
        string(17) "-2.93691871628878"
        [1]=>
        string(16) "35.2979409773216"
      }
      [7]=>
      array(2) {
        [0]=>
        string(17) "-2.93691413103252"
        [1]=>
        string(16) "35.2979323321398"
      }
      [8]=>
      array(2) {
        [0]=>
        string(17) "-2.93690399979109"
        [1]=>
        string(16) "35.2979270789636"
      }
      [9]=>
      array(2) {
        [0]=>
        string(17) "-2.93689431492369"
        [1]=>
        string(16) "35.2979220514443"
      }
      [10]=>
      array(2) {
        [0]=>
        string(17) "-2.93688289797324"
        [1]=>
        string(16) "35.2979224151838"
      }
      [11]=>
      array(2) {
        [0]=>
        string(17) "-2.93687516563971"
        [1]=>
        string(16) "35.2979298005764"
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.