protected function featureFromXML() { $features = []; $properties = []; $id = ""; $geom_types = geoPHP::geometryList(); $placemark_elements = $this->xmlobj->getElementsByTagName('placemark'); if ($placemark_elements->length) { foreach ($placemark_elements as $placemark) { $properties = []; foreach ($placemark->childNodes as $child) { // Node names are all the same, except for MultiGeometry, which maps to GeometryCollection $node_name = $child->nodeName == 'multiGeometry' ? 'geometrycollection' : $child->nodeName; if (array_key_exists($node_name, $geom_types)) { $adapter = new KML(); $geometry = $adapter->read($child->ownerDocument->saveXML($child)); } elseif ($node_name == 'extendeddata') { foreach ($child->childNodes as $data) { if ($data->nodeName != '#text') { if ($data->nodeName == 'data') { $value = $data->getElementsByTagName('value')[0]; $properties[$data->getAttribute('name')] = preg_replace('/\\n\\s+/', ' ', trim($value->textContent)); } elseif ($data->nodeName == 'schemadata') { foreach ($data->childNodes as $schemadata) { if ($schemadata->nodeName != '#text') { $properties[$schemadata->getAttribute('name')] = preg_replace('/\\n\\s+/', ' ', trim($schemadata->textContent)); } } } } } } elseif (!in_array($node_name, ['#text', 'lookat', 'style', 'styleurl'])) { $properties[$child->nodeName] = preg_replace('/\\n\\s+/', ' ', trim($child->textContent)); } } $feature = new Feature($geometry, $properties, $id); $features[] = $feature; } } else { throw new Exception("Cannot Read Feature From KML"); } $id = ""; $properties = []; $collection = new FeatureCollection($features, $properties, $id); return $collection; }
<div id="tags"></div> <p id="shortdesc"> Demonstrate the operation of the KML parser with this KML file : <a href="http://code.google.com/apis/kml/documentation/KML_Samples.kml">code.google.com/apis/kml/documentation/KML_Samples.kml</a> </p> <div id="output"> <?php $url = '<script type="text/javascript">document.write(document.getElementById(\'file_url\'))</script>'; ?> <?php require_once 'lib/Format/KML.class.php'; $options = array("extractStyles" => true); $test = new KML($options); /** read **/ $features = $test->read("http://code.google.com/apis/kml/documentation/KML_Samples.kml"); $html = ""; foreach ($features as $feature) { $html .= "<h2>Geometry : <b>" . get_class($feature->geometry) . "</b></h2>"; if (count($feature->attributes) > 0) { $html .= "<h3>attributes : </h3>"; $html .= "<ul>"; foreach ($feature->attributes as $key => $value) { $html .= "<li>" . $key . " : " . $value . "</li>"; } $html .= "</ul>"; } if (count($feature->style) > 0) { $html .= "<h3>style : </h3>"; foreach ($feature->style as $style) { $html .= "<ul>";
function test_Format_KML_extendedData() { $f = new KML(); $features = $f->read(self::ext_data); $this->assertEqual($features[0]->attributes["all_bridges"]["value"], "3030"); $this->assertEqual($features[0]->attributes["all_bridges"]["displayName"], "all bridges"); }
<?php require_once 'lib/Format/KML.class.php'; $options = array("extractStyles" => true); $test = new KML($options); /** read **/ $test->read("KML_Samples.kml"); //Util::dump($test->features, "features"); /** write **/ $kml = $test->write($test->features); Util::dump($kml, "kml !");