Skip to content

Instantly share code, notes, and snippets.

@ericjames
Created October 23, 2017 02:07
Show Gist options
  • Save ericjames/2af933a473956ee26ff9a7a48b36df4a to your computer and use it in GitHub Desktop.
Save ericjames/2af933a473956ee26ff9a7a48b36df4a to your computer and use it in GitHub Desktop.
PHP KML Generator - Generates a KML file from database stored KML geometry
<?php
$routenum = $_REQUEST['routenum'];
$style = $_REQUEST['style'];
$tableRoutes = "";
$key = "&key=";
$callback = "&callback=jsonp";
$select = "geometry";
$query = "http://www.google.com/fusiontables/api/query?sql=SELECT+{$select}+FROM+{$tableRoutes}+WHERE+route='{$routenum}'{$key}{$callback}";
$getfile = file_get_contents($query);
$geometry = substr($getfile, 10, -2);
$kml = array('<?xml version="1.0" encoding="UTF-8"?>');
$kml[] = '<kml xmlns="http://www.opengis.net/kml/2.2">';
$kml[] = ' <Document>';
$kml[] = ' <name>Route Line</name>';
$kml[] = ' <Style id="redline">';
$kml[] = ' <LineStyle>';
$kml[] = ' <color>501400FA</color>';
$kml[] = ' <width>5</width>';
$kml[] = ' </LineStyle>';
$kml[] = ' <PolyStyle><fill>0</fill></PolyStyle>';
$kml[] = ' </Style>';
$kml[] = ' <Style id="blueline">';
$kml[] = ' <LineStyle>';
$kml[] = ' <color>50F03214</color>';
$kml[] = ' <width>5</width>';
$kml[] = ' </LineStyle>';
$kml[] = ' <PolyStyle><fill>0</fill></PolyStyle>';
$kml[] = ' </Style>';
$kml[] = ' <Style id="greenline">';
$kml[] = ' <LineStyle>';
$kml[] = ' <color>5014B414</color>';
$kml[] = ' <width>5</width>';
$kml[] = ' </LineStyle>';
$kml[] = ' <PolyStyle><fill>0</fill></PolyStyle>';
$kml[] = ' </Style>';
$kml[] = ' <Style id="busline">';
$kml[] = ' <LineStyle>';
$kml[] = ' <color>50F0AA14</color>';
$kml[] = ' <width>4</width>';
$kml[] = ' </LineStyle>';
$kml[] = ' <PolyStyle><fill>0</fill></PolyStyle>';
$kml[] = ' </Style>';
$kml[] = ' <Placemark>';
$kml[] = ' <name>Metro Transit - Route '. $routenum .' </name>';
$kml[] = ' <description> </description>';
$kml[] = ' <styleUrl>#' . $style . '</styleUrl>';
$kml[] = ' ' . $geometry;
$kml[] = ' </Placemark>';
$kml[] = ' </Document>';
$kml[] = '</kml>';
$kmlOutput = join("\n", $kml);
header('Content-type: application/vnd.google-earth.kml+xml');
echo $kmlOutput;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment