简体   繁体   中英

PHP How to extract extended data from kml file

I use the folowing PHP to extract the coordinates from a kml file:

$xml = simplexml_load_file('data.kml');
$data = $xml->Document->Folder->Placemark;

foreach ($data as $record) {
    $coordinates = $record->MultiGeometry->Polygon->outerBoundaryIs->LinearRing->coordinates[0];
}

This works fine for the coordinates. But how can I Extact the ExtendedData?

The kml file I am using:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>pc4_single_vlak</name>
    <visibility>1</visibility>
    <Schema name="pc4_single_vlak" id="kml_schema_ft_pc4_single_vlak">
      <SimpleField type="xsd:string" name="pc4">
        <displayName>pc4</displayName>
      </SimpleField>
      <SimpleField type="xsd:string" name="woonplaats">
        <displayName>woonplaats</displayName>
      </SimpleField>
      <SimpleField type="xsd:string" name="wplnaam_uniek">
        <displayName>wplnaam_uniek</displayName>
      </SimpleField>
      <SimpleField type="xsd:string" name="gemeente">
        <displayName>gemeente</displayName>
      </SimpleField>
      <SimpleField type="xsd:string" name="provincie">
        <displayName>provincie</displayName>
      </SimpleField>
    </Schema>
    <Folder id="kml_ft_pc4_single_vlak">
      <name>pc4_single_vlak</name>
      <Placemark id="kml_1">
        <name>kml_1</name>
        <snippet></snippet>
        <ExtendedData>
          <SchemaData schemaUrl="#kml_schema_ft_pc4_single_vlak">
            <SimpleData name="pc4">4001</SimpleData>
            <SimpleData name="woonplaats">Tiel</SimpleData>
            <SimpleData name="wplnaam_uniek">Tiel</SimpleData>
            <SimpleData name="gemeente">Tiel</SimpleData>
            <SimpleData name="provincie">Gelderland</SimpleData>
          </SchemaData>
        </ExtendedData>
        <MultiGeometry>
          <Polygon>
            <outerBoundaryIs>
              <LinearRing>
                <coordinates>...</coordinates>
              </LinearRing>
            </outerBoundaryIs>
          </Polygon>
        </MultiGeometry>
      </Placemark>
    </Folder>
  </Document>
</kml>

I tried all kinds of things, but I cant get it to work.

I'm not sure where the hiccup is, but hopefully this provides some clarity.

Let's get the ExtendedData parent:

echo $record->ExtendedData;

No attributes though, only children, let's get the schemaUrl from the first SchemaData child:

echo $record->ExtendedData->SchemaData[0]['schemaUrl'];

When getting attrributes, you access them as an index of the element.

Here's a working example

I found the solution to my own question :). I gues this isnt the best way to do it. But I used the folowing code to get to the different SimpleData fields:

echo $record->ExtendedData->SchemaData->SimpleData[0];
echo $record->ExtendedData->SchemaData->SimpleData[1];
echo $record->ExtendedData->SchemaData->SimpleData[2];

enz..

Again. I am sure there is a better solution to this. If someone knows, please comment!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM