CSV to KML

by jediknight304 on July 21st, 2010

This isn’t release code, looking for bugs and ways to improve.

Syntax: PHP
Show lines - Hide lines - Show in textbox - Download
<?php
function display_all($arr){ 
	echo "<table cellpadding='5' border><tr><th>Column Name</th><th>Column Index</th>"; 
	echo "<th>Column Data -></th></tr>";
	foreach($arr as $spot){ 
		echo "<tr>";
		foreach($spot as $spotdeux){ 
			echo "<td align='center'>"; 
			echo $spotdeux; 
			echo "</td>";
		}
		echo "</tr>";
	}
	echo "</tr> </table>";
}
 
/*
$columnNames = array('timestamp', 'infection', 'port', 'city');
#Hold column name in 0, column number in 1, and data in 2+
for($i = 0; $i < count ( $columnNames ) ; $i++){
	$displayData[$i][0]  = $columnNames[$i];
}
*/
#case insensitive
$displayData = array(array('timestamp'),array('infection'),array('port'),array('city'));
 
//open and parse csv
$filename = "http://127.0.0.1/kyle/csv.csv";
$file=fopen($filename,"r") or die("<br />Can't open $filename");
 
#prep
$data = fgetcsv($file);
for ($i = 0; $i < count($data); $i++){#cycle column names in file
	for ($j = 0; $j < count ($displayData); $j++){#cycle wanted columns
		if(strcmp($data[$i],$displayData[$j][0])==0){ #case insensitive
			$displayData[$j][1] = $i;#set the next row to the column index
		}
	}
}
 
$row = -1;
while (($data = fgetcsv($file)) !== FALSE) {
	$row++;
	for ($j=0; $j < count ($data); $j++) {
		for ($i = 0; $i < count($displayData); $i++){
			if($displayData[$i][1] == $j){
				$displayData[$i][] = $data[$j];
				#print "$c  --  $data[$c] <br />";
			}
		}
	}
}
fclose($file);
 
display_all($displayData);
 
//print all cities --- foreach ($cities as $c){print $c . "<br />";}
 
//setup kml framework
$kmlWhole=
	'<?xml version="1.0" encoding="UTF-8"?>
	<kml xmlns="http://www.opengis.net/kml/2.2">
	<Placemark>
	<name>NAMEregex</name>
	<description>DESCRIPTIONregex</description>
	<Point>
	<coordinates>EASTregex,NORTHregex,0</coordinates>
	</Point>
	</Placemark>
	</kml>';
//String Beginning for KML
$kmlStart=
	'<?xml version="1.0" encoding="UTF-8"?>
	<kml xmlns="http://www.opengis.net/kml/2.2">';
//Middle section for longitutde lattitude
$kmlMid=
'<Placemark>
	<name>NAMEregex</name>
	<description>DESCRIPTIONregex</description>
	<Point>
	<coordinates>EASTregex,NORTHregex,0</coordinates>
	</Point>
	</Placemark>';
//String End for KML
$kmlEnd=
	'</kml>';
 
//switch < and > with compatable alternatives for output in html
//  throughout the entire kml framework
$kmlWhole= preg_replace(array('/</','/>/'),array('<','><br />'),$kmlWhole);
$kmlStart= preg_replace(array('/</','/>/'),array('<','><br />'),$kmlStart);
$kmlMid= preg_replace(array('/</','/>/'),array('<','><br />'),$kmlMid);
$kmlEnd= preg_replace(array('/</','/>/'),array('<','><br />'),$kmlEnd);
//print $kmlStart;print $kmlMid;print $kmlEnd;
 
 
//Google example code
 
$MAPS_HOST = "maps.google.com";
 
$base_url = "http://" . $MAPS_HOST . "/maps/geo?output=xml";
 
print count($displayData[0])."<br />";
 
//This is killing it .... WHY!
for($i = 0; $i< count ($displayData); $i++){
	if(strcmp($displayData[$i][0],"city") == 0){
		$cityIndex = $i;
	}
}
 
for($i = 2; $i< count ($displayData[$cityIndex]); $i++){
  print $displayData[$cityIndex][$i].", ";
}
die("Stop here");
 
for($i = 2; $i< count ($displayData[0]); $i++){
 
	$address = $displayData[$cityIndex][$i]. ", WV";
	$request_url = $base_url . "&q=" . urlencode($address);
	$xml = simplexml_load_file($request_url) or die("url not loading");
 
	$status = $xml->Response->Status->code;
	if (strcmp($status, "200") == 0) {
		// Successful geocode
		$geocode_pending = false;
		$coordinates = $xml->Response->Placemark->Point->coordinates;
		$coordinatesSplit = split(",", $coordinates);
		// Format: Longitude, Latitude, Altitude
		$lat = $coordinatesSplit[1];
		$lng = $coordinatesSplit[0];
		//echo "<br />$city is at Lattitude: $lat Longitude: $lng";
		$replaceThis = array('/DESCRIPTIONregex/','/NAMEregex/','/EASTregex/','/NORTHregex/');
		$withThis =    array('My Decription', "My Name $i", $lng, $lat);
		$mid .= preg_replace($replaceThis, $withThis, $kmlMid);
 
	} else {// failure to geocode
		echo "Address " . $address . " failed to geocoded. ";
		echo "Received status " . $status;
	}
 
}
 
print $kmlStart;
print $mid;
print $kmlEnd;
?>

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS