Advertisement
comicidiot

Maps: MySQL to KML

Mar 10th, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.04 KB | None | 0 0
  1. <?php
  2.     // MYSQL INFO
  3.         $un     = "";
  4.         $pw     = "";
  5.         $db     = "";
  6.         $svr        = "";
  7.         $tbl        = "";
  8.  
  9.    
  10.     // MYSQL CONNECT INFO
  11.         $con    = mysqli_connect($svr, $un, $pw, $db);
  12.         ## If the credentials are wrong
  13.         if(!$con) {
  14.             die("Could not connect to server: " . mysqli_error($con) . "<br>");
  15.         }
  16.    
  17.         #$database  =   mysqli_select_db($db, $svr);
  18.         ## If the database can't be reached.
  19.         ## Likely misspelled, doesn't exist or the MYSQL user doesn't have access
  20.         #if(!$database) {
  21.         #   die("Could not connect to database: " . mysqli_error($con) . "<br>");
  22.         #}
  23.        
  24.     // Get the rows for the KML creation
  25.         $query = "SELECT * FROM $tbl";
  26.         $result = mysqli_query($con, $query);
  27.        
  28.         ## If the query is invalid it'll error out
  29.         if(!$result) {
  30.             die("There was an error with the query: " . mysqli_error($con) . "<br>");
  31.         }
  32.  
  33.     // CREATE THE DOM DOCUMENT
  34.         $dom = new DOMDocument('1.0', 'UTF-8');
  35.        
  36.         ## I'll admit that most of the stuff below this I copy and pasted
  37.         ## because I'm not familiar with object oriented programming.
  38.         ## https://developers.google.com/kml/articles/phpmysqlkml#outputkml
  39.        
  40.         ## Creates the root KML element and appends it to the root document.
  41.         $node = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml');
  42.         $parNode = $dom->appendChild($node);
  43.        
  44.         ## Creates a KML Document element and append it to the KML element.
  45.         $dnode = $dom->createElement('Document');
  46.         $docNode = $parNode->appendChild($dnode);
  47.        
  48.         ## Create a Folder element and append it to the KML element
  49.         $fnode = $dom->createElement('Folder');
  50.         $folderNode = $parNode->appendChild($fnode);
  51.        
  52.         ## Iterate through the MySQL results
  53.         $row = @mysqli_fetch_assoc($result);
  54.        
  55.         ## Create a Placemark and append it to the document
  56.         $node = $dom->createElement('Placemark');
  57.         $placeNode = $folderNode->appendChild($node);
  58.        
  59.         ## Create an id attribute and assign it the value of id column
  60.         $placeNode->setAttribute('id','linestring1');
  61.        
  62.         ## Create name, description, and address elements and assign them the values of
  63.         ## the name, type, and address columns from the results
  64.        
  65.         $nameNode = $dom->createElement('name','My path');
  66.         $placeNode->appendChild($nameNode);
  67.         $descNode= $dom->createElement('description', 'Where I hiked');
  68.         $placeNode->appendChild($descNode);
  69.        
  70.         ## Create a LineString element
  71.         $lineNode = $dom->createElement('LineString');
  72.         $placeNode->appendChild($lineNode);
  73.         $exnode = $dom->createElement('extrude', '1');
  74.         $lineNode->appendChild($exnode);
  75.         $almodenode =$dom->createElement(altitudeMode,'relativeToGround');
  76.         $lineNode->appendChild($almodenode);
  77.        
  78.         ## Create a coordinates element and give it the value of the lng and lat columns from the results
  79.        
  80.         ## I added this next line;
  81.         $mycoords       =   $row['latitude'] . " " . $row['longitude'];
  82.        
  83.         $coorNode = $dom->createElement('coordinates',$mycoords);
  84.         $lineNode->appendChild($coorNode);
  85.         $kmlOutput = $dom->saveXML();
  86.        
  87.     // Output the KML
  88.         header('Content-type: application/vnd.google-earth.kml+xml');
  89.         echo $kmlOutput;
  90.  
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement