public function Kml::decode in farmOS 2.x
Overrides XmlEncoder::decode
File
- modules/
core/ kml/ src/ Encoder/ Kml.php, line 52
Class
- Kml
- Provides a KML encoder that extends from the XML encoder.
Namespace
Drupal\farm_kml\EncoderCode
public function decode($data, $format, array $context = []) {
// Start an array of decoded placemark data.
$decoded_placemarks = [];
// Build an XML object.
$xml = simplexml_load_string($data);
// If empty, or failed to parse, bail.
if (empty($xml)) {
return $decoded_placemarks;
}
// Determine the root. Sometimes it is "Document".
$root = $xml;
if (isset($xml->Document)) {
$root = $xml->Document;
}
// Start an array of placemarks to decode.
$placemarks = [];
// If the KML is organized into folders, iterate through them.
if (isset($root->Folder)) {
foreach ($root->Folder as $folder) {
if (isset($folder->Placemark)) {
foreach ($folder->Placemark as $placemark) {
$placemarks[] = $placemark;
}
}
}
}
// Also check the root for any placemarks.
if (isset($root->Placemark)) {
foreach ($root->Placemark as $placemark) {
$placemarks[] = $placemark;
}
}
// Decode each placemark into an array.
// Include the individual placemark as an XML string.
foreach ($placemarks as $placemark) {
$geometry = (array) $placemark;
$geometry['xml'] = $placemark
->asXML();
$decoded_placemarks[] = $geometry;
}
return $decoded_placemarks;
}