Created
June 8, 2013 14:06
-
-
Save jcchurch/5735280 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import xml.etree.ElementTree as etree | |
def main(): | |
assert len(sys.argv) >= 2 | |
filename = sys.argv[1] | |
# This translates the Google Maps symbol codes | |
# to Garmin symbol codes. I have determined that | |
# Garmin's codes are more readable. | |
iconmaps = { | |
'#icon-1417': 'Airport', | |
'#icon-1385': 'Tall Tower', | |
'#icon-1461': 'Car', # metro | |
'#icon-1037': 'Church', # shrine | |
'#icon-1107': 'Museum', | |
'#icon-1445': 'Museum', | |
'#icon-1363': 'Marina', | |
'#icon-1363': 'Marina', | |
'#icon-979': 'Bar', | |
'#icon-1193': 'Park', | |
'#icon-1021': 'Park', | |
'#icon-1095': 'Shopping Center', | |
'#icon-991': 'Restaurant', | |
'#icon-1075': 'Restaurant', | |
'#icon-1085': 'Restaurant', | |
'#icon-1459': 'Car', # train station | |
'#icon-1113': 'Geocache', # interesting place | |
'#icon-1053': 'Geocache', # interesting place | |
'#icon-1405': 'Geocache', # interesting place | |
'#icon-1035': 'Residence', | |
'#icon-960-DB4436': 'Residence', | |
'#icon-960-62AF44': 'Green Diamond', | |
} | |
waypoints = [] | |
alllat = [] | |
alllon = [] | |
tree = etree.parse(filename) | |
root = tree.getroot() | |
placemark_nodes = root.find('{http://www.opengis.net/kml/2.2}Document').findall('{http://www.opengis.net/kml/2.2}Placemark') | |
# For each placemark in Google Maps, | |
# we will map the name, lat, lon, and symbol to something | |
# that will work on my Garmin GPS device | |
for pm in placemark_nodes: | |
point = pm.find("{http://www.opengis.net/kml/2.2}Point") | |
if point is not None: | |
coordinates = point.find("{http://www.opengis.net/kml/2.2}coordinates").text | |
[lon, lat, elev] = coordinates.split(",") | |
thiswp = {} | |
name = pm.find("{http://www.opengis.net/kml/2.2}name").text.encode('ascii', 'ignore') | |
name = name.strip() | |
thiswp['name'] = name | |
thiswp['lat'] = float(lat) | |
thiswp['lon'] = float(lon) | |
original_icon = pm.find("{http://www.opengis.net/kml/2.2}styleUrl").text | |
if original_icon in iconmaps: | |
thiswp['symbol'] = iconmaps[original_icon] | |
else: | |
print "Find icon for ",thiswp['name'], original_icon | |
waypoints.append(thiswp) | |
alllat.append( float(lat) ) | |
alllon.append( float(lon) ) | |
# Now that we have our waypoints in a workable form, write this to the screen. | |
print """<?xml version="1.0" encoding="UTF-8"?> | |
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="EasyGPS 4.92" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gpxtrx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.topografix.com/GPX/gpx_style/0/2 http://www.topografix.com/GPX/gpx_style/0/2/gpx_style.xsd http://www.topografix.com/GPX/gpx_overlay/0/3 http://www.topografix.com/GPX/gpx_overlay/0/3/gpx_overlay.xsd http://www.topografix.com/GPX/gpx_modified/0/1 http://www.topografix.com/GPX/gpx_modified/0/1/gpx_modified.xsd http://www.topografix.com/GPX/Private/TopoGrafix/0/4 http://www.topografix.com/GPX/Private/TopoGrafix/0/4/topografix.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd"> | |
<metadata> | |
<bounds minlat="%s" minlon="%s" maxlat="%s" maxlon="%s"/> | |
<extensions> | |
<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2013-06-02T02:57:10.274Z</time> | |
</extensions> | |
</metadata> | |
""" % (min(alllat), min(alllon), max(alllat), max(alllon)) | |
gpxtemplate = """<wpt lat="%s" lon="%s"> | |
<ele>0</ele> | |
<name>%s</name> | |
<sym>%s</sym> | |
<type>%s</type> | |
<type>Locale</type> | |
<extensions> | |
<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3"> | |
<label_text>%s</label_text> | |
</label> | |
</extensions> | |
</wpt> | |
""" | |
for wp in waypoints: | |
print gpxtemplate % (wp['lat'], wp['lon'], wp['name'], wp['symbol'], wp['symbol'], wp['name']) | |
print "</gpx>" | |
return 0 | |
if __name__=='__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment