Skip to content

Instantly share code, notes, and snippets.

@woodbri
Created November 1, 2017 21:23
Show Gist options
  • Save woodbri/9c72d83440609a5f7bcd148098797b0a to your computer and use it in GitHub Desktop.
Save woodbri/9c72d83440609a5f7bcd148098797b0a to your computer and use it in GitHub Desktop.
Script to pasrse a kml file
<?php
$xml = simplexml_load_file('Peoples-Ranch-Map.kml-2.xml');
$folders = $xml->Document->Folder;
foreach ($folders as $folder) {
print "Name: " . $folder->name . "\n";
print "Description: " . $folder->description . "\n";
$children = $folder->children();
foreach ($children as $child) {
print ' Name: ' . $child->name . "\n";
print ' Description: ' . $child->description . "\n";
$type = 'NONE';
if (isset($child->Point)) {
$type = 'POINT';
$coordinates = $child->Point->coordinates;
$geom = preg_split('/\s+/', trim($coordinates));
}
elseif (isset($child->LineString)) {
$type = 'LINESTRING';
$coordinates = $child->LineString->coordinates;
$geom = preg_split('/\s+/', trim($coordinates));
if (count($geom) and $geom[0] == $geom[count($geom)-1]) {
$type = 'POLYGON';
}
}
$coords = array();
foreach ($geom as $g) {
$tmp = explode(',', $g);
$coords[] = "$tmp[0] $tmp[1]";
}
if ($type == 'POLYGON') {
$wkt = $type . '((' . implode(',', $coords) . '))';
}
else {
$wkt = $type . '(' . implode(',', $coords) . ')';
}
print ' ' . $wkt . "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment