Last active
July 19, 2019 09:26
-
-
Save hossain-khan/5351153 to your computer and use it in GitHub Desktop.
Snippet for parsing KML file and then converting to polygon shapes of Android Google Maps V2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Dirty way to parse the KML file and create equivalent polygons for android maps V2 | |
* | |
* What it does: | |
* - Reads local KML file and looks for "Polygon" data, and converts them | |
* to equivalent simple polygon data for Android Maps V2 | |
* | |
* What it does NOT do / Limitations: | |
* - Consider all possible DOM tree structure | |
* (manually need to update selector for placemark, | |
* eg. "$kml->Document->Placemark as $pm") | |
* - use altitude values | |
* - Create complex Polygon with hole | |
* - Style polygon based on KML style document | |
* - and many more | |
* | |
* Why create this? | |
* - I needed quick way to generate tons of polygon data from KML. | |
* Since, android maps v2 does not support KML data, needed something like this script | |
* | |
* References: | |
* - KML Reference: https://developers.google.com/kml/documentation/kmlreference | |
* - POLYGON on KML: https://developers.google.com/kml/documentation/kmlreference#polygon | |
*/ | |
// enable error reporting - helpful when working on this script | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
echo 'Begin Script <br/><hr/>'; | |
$polygonDataArray = array(); | |
$kml = simplexml_load_file('data.kml'); | |
//print_r($kml->Document); // debug the whole document data | |
/* You MUST update this selector to math your KML document tree */ | |
foreach($kml->Document->Placemark as $pm){ | |
if(isset($pm->Polygon)){ | |
// Process polygon datas | |
// Get coordinates for 'outerBoundaryIs', other possible data not considered is 'innerBoundaryIs' | |
$coordinates = $pm->Polygon->outerBoundaryIs->LinearRing->coordinates; | |
$cordsData = trim(((string) $coordinates)); | |
// check if coordinate data is available | |
if(isset($cordsData) && !empty($cordsData)){ | |
$explodedData = explode("\n", $cordsData); | |
$explodedData = array_map('trim', $explodedData); | |
// next for each of the points build the polygon data | |
$points = ""; | |
foreach ($explodedData as $index => $coordinateString) { | |
$coordinateSet = array_map('trim', explode(',', $coordinateString)); | |
if(count($coordinateSet) == 3){ | |
// lon,lat[,alt] | Index 0 = lon, index 1 = lat, index 2 = alt [optional] | |
$points .= " new LatLng({$coordinateSet[1]}, {$coordinateSet[0]}),"; | |
} else { | |
echo '<br/>Unhandled case for data set : ' . print_r($coordinateSet, true); | |
} | |
} | |
// at the end, remove the last ',' | |
$polygonDataArray[] = "new PolygonOptions().add(" . substr($points, 0, -1) . ")"; | |
} | |
} else { | |
echo '<br/>Not a polygon - skipping'; | |
} | |
} | |
$androidMapJava = " | |
private static List<PolygonOptions> polygonOptionsList = new ArrayList<PolygonOptions>(); | |
static { | |
"; | |
foreach ($polygonDataArray as $polyDataSet) { | |
// add polygon to list - add a | |
$androidMapJava.= "\tpolygonOptionsList.add({$polyDataSet}.strokeColor(Color.BLACK));\n"; | |
} | |
$androidMapJava .= "}"; | |
echo " | |
<pre> | |
{$androidMapJava} | |
</pre> | |
"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment