Kml.php in farmOS 2.x
File
modules/core/kml/src/Encoder/Kml.php
View source
<?php
namespace Drupal\farm_kml\Encoder;
use Drupal\serialization\Encoder\XmlEncoder;
class Kml extends XmlEncoder {
protected static $format = [
'geometry_kml',
];
public function encode($data, $format, array $context = []) {
$xml = [
'@xmlns' => 'http://earth.google.com/kml/2.1',
'Document' => [
'Placemark' => $data,
],
];
$xml_context = [
'xml_version' => '1.0',
'xml_encoding' => 'UTF-8',
'xml_format_output' => TRUE,
'xml_root_node_name' => 'kml',
] + $context;
return $this
->getBaseEncoder()
->encode($xml, 'xml', $xml_context);
}
public function decode($data, $format, array $context = []) {
$decoded_placemarks = [];
$xml = simplexml_load_string($data);
if (empty($xml)) {
return $decoded_placemarks;
}
$root = $xml;
if (isset($xml->Document)) {
$root = $xml->Document;
}
$placemarks = [];
if (isset($root->Folder)) {
foreach ($root->Folder as $folder) {
if (isset($folder->Placemark)) {
foreach ($folder->Placemark as $placemark) {
$placemarks[] = $placemark;
}
}
}
}
if (isset($root->Placemark)) {
foreach ($root->Placemark as $placemark) {
$placemarks[] = $placemark;
}
}
foreach ($placemarks as $placemark) {
$geometry = (array) $placemark;
$geometry['xml'] = $placemark
->asXML();
$decoded_placemarks[] = $geometry;
}
return $decoded_placemarks;
}
}
Classes
Name |
Description |
Kml |
Provides a KML encoder that extends from the XML encoder. |