GoogleEarth Dynamic KML

From OpenStreetMap Wiki
Jump to: navigation, search
Available languages — GoogleEarth Dynamic KML
Afrikaans Alemannisch aragonés asturianu azərbaycanca Bahasa Indonesia Bahasa Melayu Bân-lâm-gú Basa Jawa Baso Minangkabau bosanski brezhoneg català čeština dansk Deutsch eesti English español Esperanto estremeñu euskara français Frysk Gaeilge Gàidhlig galego Hausa hrvatski Igbo interlingua Interlingue isiXhosa isiZulu íslenska italiano Kiswahili Kreyòl ayisyen kréyòl gwadloupéyen kurdî latviešu Lëtzebuergesch lietuvių magyar Malagasy Malti Nederlands Nedersaksies norsk norsk nynorsk occitan Oromoo oʻzbekcha/ўзбекча Plattdüütsch polski português română shqip slovenčina slovenščina Soomaaliga suomi svenska Tiếng Việt Türkçe Vahcuengh vèneto Wolof Yorùbá Zazaki српски / srpski беларуская български қазақша македонски монгол русский тоҷикӣ українська Ελληνικά Հայերեն ქართული नेपाली मराठी हिन्दी অসমীয়া বাংলা ਪੰਜਾਬੀ ગુજરાતી ଓଡ଼ିଆ தமிழ் తెలుగు ಕನ್ನಡ മലയാളം සිංහල ไทย မြန်မာဘာသာ ລາວ ភាសាខ្មែរ ⵜⴰⵎⴰⵣⵉⵖⵜ አማርኛ 한국어 日本語 中文(简体)‎ 吴语 粵語 中文(繁體)‎ ייִדיש עברית اردو العربية پښتو سنڌي فارسی ދިވެހިބަސް


Google Earth with OSM fuel stations

Wouldn't it be cool to show OpenStreetMap data/poi's into GoogleEarth ? And even cooler would be to download only data that's visible in the current view. And super cool would be to retrieve just what you want by adjusting an url parameter. Well, good news.. it all can be done, and it's even not that hard to make.

Important legal note

Do not use use data from Google Earth to add or change anything in Open Street Map, ie. don't look at a satellite image from Google and add or change streets of other things. Somebody has a copyright on the data shown in Google Earth and using any data from it will compromise the OSM database. See the Legal FAQ for more information.



  • A server with
    • Some kind of database with openstreetmap data (i'm using the postgres mapnik database of the dutch tileserver)
    • PHP with a script to deliver the dynamic KML data
  • GoogleEarth

Server Side

First create a table with poi's into your database. If you have already a mapnik setup, then you can use the planet_osm_point table like in the code below. Change the login data and database related stuff for your system. Maybe you also have to correct the transform (4326) for your projection ! Put the data.php onto your server.


	// Creates the KML/XML Document.
	$dom = new DOMDocument('1.0', 'UTF-8');

	// Creates the root KML element and appends it to the root document.
	$node = $dom->createElementNS('', 'kml');
	$parNode = $dom->appendChild($node);

	// Creates a KML Document element and append it to the KML element.
	$dnode = $dom->createElement('Document');
	$docNode = $parNode->appendChild($dnode);

	// database stuff
	$pgsql['user'] = 'mapnik'; // username
	$pgsql['db'] = 'osm';      // database
	// currently we only support amenity in the url
	$what = $_GET['amenity'];
	if ($what == '') {
		$what = 'restaurant';
	$connect_string = ' user=' . $pgsql['user'] . ' dbname=' . $pgsql['db'];
	$pgcon = pg_connect($connect_string); 
	if ($pgcon) { // connected!	
		$bbox = $_GET['BBOX']; // get the bbox param from google earth
		list($bbox_south, $bbox_west, $bbox_east, $bbox_north) = split(",", $bbox); // west, south, east, north
                // Get the data from the Database Table (planet_osm_point)
		$sql = "SELECT osm_id, name, x(transform(way,4326)) as lon, y(transform(way, 4326)) as lat FROM planet_osm_point WHERE (amenity='" . $what . "') AND (box(point(" . $bbox_south . "," . $bbox_west . "),point(" . $bbox_east . "," . $bbox_north . ")) ~ transform(way,4326)) LIMIT 100";
		// perform query
		$query = pg_query($pgcon, $sql);
		if ($query) {
			if (pg_num_rows($query) > 0) { // found something

				// Iterates through the results, creating one Placemark for each row.
				while ($row = pg_fetch_array($query))
					 // Creates a Placemark and append it to the Document.
					  $node = $dom->createElement('Placemark');
					  $placeNode = $docNode->appendChild($node);

					  // Creates an id attribute and assign it the value of id column.
					  $placeNode->setAttribute('id', 'placemark' . $row['osm_id']);

					  // Create name, and description elements and assigns them the values of the name and address columns from the results.
					  $nameNode = $dom->createElement('name',htmlentities($row['name']));

					  // Creates a Point element.
					  $pointNode = $dom->createElement('Point');

					  // Creates a coordinates element and gives it the value of the lng and lat columns from the results.
					  $coorStr = $row['lon'] . ','  . $row['lat'];
					  $coorNode = $dom->createElement('coordinates', $coorStr);

			} else { // nothing found
	} else {
		// no valid database connection

	$kmlOutput = $dom->saveXML();
	header('Content-type: application/');
	echo $kmlOutput;

Client Side / Google Earth

Create a network link in GoogleEarth

  • goto My places, right click, add, Network Link
  • give it a name
  • insert the link to your data.php like :
  • Open tab refresh and select View based refresh, after camera stops, 4 secs
  • hit Ok and the network link icon should indicate it's loading data when you are done zooming into a area

Background info

When you pan/zoom into a new area, after 4 seconds GoogleEarth will download data from our network link url, with some parameters, like amenity and the boundingbox of the current view. The php script will use this information to query the database and build the kml output.

Future stuff

  • Remove fixed amenity implementation, so you can query every possible field
  • add polygon support, so you can show ways / borders / buildings / area's