Convert from Google KML to GPS Exchange Format GPX with PHP

Here is a quick post on converting from KML files used in Google Earth/Maps to GPX Exchange Format (GPX) with PHP.

Code is quite explanatory, change $u with the location of the KML file, code will output GPX XML.

Alternatively, download the code here.

	//enter location of KML file here
	$u = "";
	function utcdate() {
		return gmdate("Y-m-d\Th:i:s\Z");

	$u_parts = pathinfo($u); //array of url parts
	$u_ext = strtoupper($u_parts['extension']);
	if ($u_ext== "KML") {

		$dom_kml = new DOMDocument();

		$dom_gpx = new DOMDocument('1.0', 'UTF-8');
		$dom_gpx->formatOutput = true;
		//root node
		$gpx = $dom_gpx->createElement('gpx');
		$gpx = $dom_gpx->appendChild($gpx);
		$gpx_version = $dom_gpx->createAttribute('version');
		$gpx_version_text = $dom_gpx->createTextNode('1.0');
		$gpx_creator = $dom_gpx->createAttribute('creator');
		$gpx_creator_text = $dom_gpx->createTextNode('');
		$gpx_xmlns_xsi = $dom_gpx->createAttribute('xmlns:xsi');
		$gpx_xmlns_xsi_text = $dom_gpx->createTextNode('');
		$gpx_xmlns = $dom_gpx->createAttribute('xmlns');
		$gpx_xmlns_text = $dom_gpx->createTextNode('');
		$gpx_xsi_schemaLocation = $dom_gpx->createAttribute('xsi:schemaLocation');
		$gpx_xsi_schemaLocation_text = $dom_gpx->createTextNode('');
		$gpx_url = $dom_gpx->createElement('url');
		$gpx_url = $gpx->appendChild($gpx_url);
		$gpx_url_text = $dom_gpx->createTextNode($u_parts['dirname']);
		$gpx_time = $dom_gpx->createElement('time');
		$gpx_time = $gpx->appendChild($gpx_time);
		$gpx_time_text = $dom_gpx->createTextNode(utcdate());
		// placemarks
		$names = array();
		foreach ($dom_kml->getElementsByTagName('Placemark') as $placemark) {
			foreach ($placemark->getElementsByTagName('name') as $name) {
				$name  = $name->nodeValue;
				//check if the key exists
				if (array_key_exists($name, $names)) {
					//increment the value
					$name = $name." ({$names[$name]})";
				} else {
					$names[$name] = 0;
			foreach ($placemark->getElementsByTagName('description') as $description) {
				$description  = $description->nodeValue;
			foreach ($placemark->getElementsByTagName('Point') as $point) {
				foreach ($point->getElementsByTagName('coordinates') as $coordinates) {
					//add the marker
					$coordinate = $coordinates->nodeValue;
					$coordinate = str_replace(" ", "", $coordinate);//trim white space
					$latlng = explode(",", $coordinate);
					if (($lat = $latlng[1]) && ($lng = $latlng[0])) {
						$gpx_wpt = $dom_gpx->createElement('wpt');
						$gpx_wpt = $gpx->appendChild($gpx_wpt);

						$gpx_wpt_lat = $dom_gpx->createAttribute('lat');
						$gpx_wpt_lat_text = $dom_gpx->createTextNode($lat);
						$gpx_wpt_lon = $dom_gpx->createAttribute('lon');
						$gpx_wpt_lon_text = $dom_gpx->createTextNode($lng);
						$gpx_time = $dom_gpx->createElement('time');
						$gpx_time = $gpx_wpt->appendChild($gpx_time);
						$gpx_time_text = $dom_gpx->createTextNode(utcdate());
						$gpx_name = $dom_gpx->createElement('name');
						$gpx_name = $gpx_wpt->appendChild($gpx_name);
						$gpx_name_text = $dom_gpx->createTextNode($name);
						$gpx_desc = $dom_gpx->createElement('desc');
						$gpx_desc = $gpx_wpt->appendChild($gpx_desc);
						$gpx_desc_text = $dom_gpx->createTextNode($description);
						//$gpx_url = $dom_gpx->createElement('url');
						//$gpx_url = $gpx_wpt->appendChild($gpx_url);
						//$gpx_url_text = $dom_gpx->createTextNode($ref);
						$gpx_sym = $dom_gpx->createElement('sym');
						$gpx_sym = $gpx_wpt->appendChild($gpx_sym);
						$gpx_sym_text = $dom_gpx->createTextNode('Waypoint');
			foreach ($placemark->getElementsByTagName('LineString') as $lineString) {
				foreach ($lineString->getElementsByTagName('coordinates') as $coordinates) {
					//add the new track
					$gpx_trk = $dom_gpx->createElement('trk');
					$gpx_trk = $gpx->appendChild($gpx_trk);
					$gpx_name = $dom_gpx->createElement('name');
					$gpx_name = $gpx_trk->appendChild($gpx_name);
					$gpx_name_text = $dom_gpx->createTextNode($name);
					$gpx_trkseg = $dom_gpx->createElement('trkseg');
					$gpx_trkseg = $gpx_trk->appendChild($gpx_trkseg);
					$coordinates = $coordinates->nodeValue;
					$coordinates = preg_split("/[\s\r\n]+/", $coordinates); //split the coords by new line
					foreach ($coordinates as $coordinate) {
						$latlng = explode(",", $coordinate);
						if (($lat = $latlng[1]) && ($lng = $latlng[0])) {
							$gpx_trkpt = $dom_gpx->createElement('trkpt');
							$gpx_trkpt = $gpx_trkseg->appendChild($gpx_trkpt);

							$gpx_trkpt_lat = $dom_gpx->createAttribute('lat');
							$gpx_trkpt_lat_text = $dom_gpx->createTextNode($lat);
							$gpx_trkpt_lon = $dom_gpx->createAttribute('lon');
							$gpx_trkpt_lon_text = $dom_gpx->createTextNode($lng);
							$gpx_time = $dom_gpx->createElement('time');
							$gpx_time = $gpx_trkpt->appendChild($gpx_time);
							$gpx_time_text = $dom_gpx->createTextNode(utcdate());
		header("Content-Type: text/xml");
		echo $dom_gpx->saveXML();

Let me know if you find any issues.

How to Unlock Garmin maps

For information only, How to Unlock Garmin maps.

First get your hands on the desired Garmin City Navigator Downloadable Update for your country/city (MapSource A).
In the compressed file should be the folders IMG, support and Windows plus a readme.txt.

You will need a version of Garmin MapSource 6.13.1 or older which does NOT support Unlocking via the internet (MapSource B).
This can be found in older version of the downloaded map update files (perhaps a 2007/2008 version).

Run the CN.msi installation file to install the old version of MapSource (B).

Run MapSource (B) and the UnlockWizard.

Now get your hands on Garmin Unlock Generator V1.5,
Enter your 10 digit Unit ID, and select map product <custom mapset>, enter the FID (MapID) of the newest Downloadable Update (the one you want to Unlock).
Click generate, copy the 25 digit map unlock code and paste this into the UnlockWizard.

At this stage you will have the older version of MapSource (B) with some old map.

Run the CNMENT2010Update_ENU.msi file in IMG folder (MapSource A).

Run MapSource (A). That’s it. The map should now be Unlocked.