-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKMLtoOSMAndGPX.py
539 lines (513 loc) · 27 KB
/
KMLtoOSMAndGPX.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#!/usr/bin/python
#========================================================================================
# Convert a KML file that was exported from google my maps into a GPX file.
# This includes OSMAnd extensions and translation of google waypoint icons into a similar OSMAnd icon.
# Tracks and waypoints are the only objects converted. Folders/layers are used as described in readme.
#
# See readme.md for more details
#
# 4/23/2023: V2.1 Tom Musolf with a little amazing help from ChatGPT
# 4/25/2023: V2.2 If you export a single layer/folder from your google my map it is not
# put into a folder. Current code does not check for no folders and only processes
# tracks and waypoints contained in folders. So there needs to be special case added
# to handle the no folder case. If you have a map with a single layer and export the
# entire map that single layer will be contained in a folder - so no issues with this case.
# Also added display counts for waypoint, tracks and folders.
# Moved waypoint and track processing code into functions so it can be used again
# in no folder case.
# 4/26/2023: V2.3 Couldn't import KMLtoOSMAndGPX created files into google my maps. Turns
# out it was missing "creator" field in the <GPX> element. Added that and it worked. Created a
# couple more functions to eliminate duplicate code blocks.
#9/16/2023: V2.4 Couldn't directly import created files into Garmin basecamp. If I ran them through
# GPSVisualizer.com they would work. Several differences in GPX files so I tweaked some of the GPX
# data to more closely match GPSVisualizer output. Down to the <trkseg><extensions> section. The
# presence of this section prevents the GPX file from being loaded directly into basecamp.
# Updated code to put osmand: on <extensions> tags which is what OSMAnd does.
#========================================================================================
import argparse
import xml.etree.ElementTree as ET
from xml.dom import minidom
import ntpath
PROGRAM_VERSION = "2.4"
DEFAULT_TRACK_TRANSPARENCY = "80"
DEFAULT_TRACK_WIDTH = "14"
# Both of these should probably be command line arguments
DEFAULT_TRACK_COLOR = "3AE63A" # a kelley green color
DEFAULT_ICON_COLOR = "DB4436" # rusty red
DEFAULT_TRACK_SPLIT = "no_split"
KMLCOLOR = "KMLCOLOR"
# Probably should make this a command line arguent and by default all layers are processed.
# Check this link for approaches:
# https://stackoverflow.com/questions/43786174/how-to-pass-and-parse-a-list-of-strings-from-command-line-with-argparse-argument
# https://www.geeksforgeeks.org/pass-list-as-command-line-argument-in-python/
LAYERS_TO_IGNORE = ["Untitled layer"]
# list of fields to insert into the GPX element
# globals to keep track of some counts
countFolders = 0
countFolderWaypoints = 0
countFolderTracks = 0
countTotalWaypoints = 0
countTotalTracks = 0
#========================================================================================
#There are certain characters that can't be in HTML/XML name or description strings.
#This function converts them to the HTML escaped version
#========================================================================================
html_escape_table = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "<",
}
def html_escape(text):
"""Produce entities within text."""
return "".join(html_escape_table.get(c,c) for c in text)
#========================================================================================
#========================================================================================
class cWaypoint:
def __init__ (self,icon,color,background):
self.icon = icon
self.color = color
self.background = background
#========================================================================================
#========================================================================================
def setupParseCmdLine():
parser = argparse.ArgumentParser(
prog="KMLtoOSMAndGPX",
description="Convert google my maps KML files to OSMAnd style GPX files, including icon conversion.",
epilog="text at bottom of help")
parser.add_argument("kml_file",
help="the input KML file path/name")
parser.add_argument("gpx_file",
help="the output GPX file path/name or if the -l option is specifed this is the path and output file(s) name prefix")
parser.add_argument('-l', '--layers',
action='store_true',
default=False,
help='False (default): A single GPX file is created. True: The tracks & waypoints in each KML layer will be written to a separate GPX file.')
parser.add_argument('-t', '--transparency',
action='store',
default=DEFAULT_TRACK_TRANSPARENCY,
help='Transparency value to use for all tracks. Specified as a 2 digit hex value. 00 is fully transparent and FF is opaque.')
parser.add_argument('-s', '--split',
action='store',
default=DEFAULT_TRACK_SPLIT,
help='Display distance splits along tracks. Value is in miles. Between 0.0 and 100.0')
parser.add_argument('-w', '--width',
action='store',
default=DEFAULT_TRACK_WIDTH,
type=int,
help='Width value to use for all tracks. Integer value between 1-24')
return(parser.parse_args())
#========================================================================================
# iconDictionary describes the mapping between a KML icon number and an OSMAnd icon name.
# It also contains a default OSMAnd color and shape to use for each OSMAnd icon type.
#
# iconDictionary format:
# "KML icon number":["OSMAnd Icon name","HTML hex color code or flag to use KMLCOLOR","OSMAnd shape"]
#
# Color code is a standard 6 digit HTML hex color code. This is what OSMAnd uses
# Put the string "KMLCOLOR", without the double quotes, in for a color value if you want to use the color specified in the KML file
# for a particular icon.
# As of 8/2020 OSMAnd icons do not support transparent colors.
# As of 8/2020 OSMAnd supports 3 icon shapes: circle, octagon, square
#
# To add additional KML icons to the dictionary.
#
# For each icon you want to translate you need to add a new entry/line into the iconDictionary table.
# To determine what the KML and OSMAnd icons are you want go through the following steps:
#
# KML icon number
# 1) Create a google my maps test file with the icons you want to use.
# 2) Export this map as a KML file.
# 3) Open up the file in a text editor and look for your points. You can ignore all the <style> & <StyleMap> tags at the
# beginning of the KML file. The points/waypoints/Placemarks will look like this:
#
# <Placemark>
# <name>Mileage Marker dot</name>
# <styleUrl>#icon-1739-0288D1-nodesc</styleUrl>
# <Point>
# <coordinates>-120.8427259,38.8170119,0</coordinates>
# </Point>
# </Placemark>
# The <styleUrl> tag has the icon number. In the preceeding example it's "1739".
#
# OSMAnd Icon name
# 1) Create some favorites using the icons you want.
# 2) Goto .../Android/data/net.osmand.plus/files/favorites/favorites.gpx
# 3) Open the favorites file in a text editor and look for the waypoints.
# in the following example the icon name is: "special_trekking"
#
# <wpt lat="39.2906659" lon="-121.4965106">
# <name>hiker, pale yellow</name>
# <extensions>
# <color>#eeee10</color>
# <icon>special_trekking</icon>
# <background>circle</background>
# </extensions>
# </wpt>
#
# ???: would be nice to have a command line option to read in a user supplied dictionary from a file
#========================================================================================
def KMLToOSMAndIcon(KMLIconID):
iconDictionary ={
"unknown":["special_symbol_question_mark","e044bb","octagon"], #unknown KML icon code - this entry will be used if the KML icon is not found the iconDictionary.
"1765":["tourism_camp_site",KMLCOLOR,"circle"], #campsite
"1525":["leisure_marina","a71de1","octagon"], #river access
"1739":["special_number_0",KMLCOLOR,"circle"], #Mileage marker plus-KML dot on gmaps & Plus in OSMAnd. Did have this color:"1010a0"
"1596":["special_trekking",KMLCOLOR,"circle"], #hiking trailhead Color I use is 9E963A
"1369":["special_trekking",KMLCOLOR,"circle"], #hiking trailhead -old style icon
"1371":["special_trekking",KMLCOLOR,"circle"], #hiking trailhead -old style icon
"1723":["tourism_viewpoint",KMLCOLOR,"octagon"], #rapid "d90000"
"1602":["tourism_hotel",KMLCOLOR,"circle"], #hotel, lodge
"1528":["bridge_structure_suspension","10c0f0","circle"], #bridge
"1577":["restaurants",KMLCOLOR,"circle"], #retaurant, diner, dining
"1085":["restaurants",KMLCOLOR,"circle"], #retaurant, diner, dining, old style icon
"1650":["tourism_picnic_site","eecc22","circle"], #picnic site
"1644":["amenity_parking",KMLCOLOR,"circle"], #parking area
"1578":["shop_supermarket",KMLCOLOR,"circle"], #grocery store, supermarket #1 - light blue "10c0f0"
"1685":["shop_supermarket",KMLCOLOR,"circle"], #grocery store, supermarket #2 - light blue "10c0f0"
"1023":["shop_supermarket",KMLCOLOR,"circle"], #grocery store, supermarket #2 - light blue "10c0f0", old style grocery icon
"1504":["air_transport","10c0f0","circle"], #airport, airstrip
"1581":["fuel",KMLCOLOR,"circle"], #gas station
"1733":["amenity_toilets","10c0f0","circle"], #toilet, restroom
"1624":["amenity_doctors","d00d0d","circle"], #hospital, doctor, emergency room
"1608":["tourism_information","1010a0","circle"], #tourism information
"1203":["tourism_information","1010a0","circle"], #tourism information, old style icon - big "i"
"1535":["special_photo_camera",KMLCOLOR,"circle"], #POI #1, camera "eecc22"
"993": ["special_photo_camera",KMLCOLOR,"circle"], #POI #1, camera "eecc22" old icon style
"1574":["special_flag_start",KMLCOLOR,"circle"], #POI #2, flag "eecc22"
"1899":["special_marker",KMLCOLOR,"circle"], #POI #3, pin "eecc22"
"1502":["special_star",KMLCOLOR,"circle"], #POI #4, star "eecc22"
"1501":["special_symbol_plus",KMLCOLOR,"circle"], #POI #5, plus/diamond "eecc22"
"1500":["special_flag_start",KMLCOLOR,"circle"], #POI #6, square in google maps & square flag i OSMAnd
"1592":["special_heart",KMLCOLOR,"circle"], #POI #7, heart
"1729":["tourism_viewpoint",KMLCOLOR,"circle"], #Vista point / viewpoint
"503": ["special_marker",KMLCOLOR,"circle"], #Old school map point
"1603":["special_house","eecc22","circle"], #house
"1879":["amenity_biergarten",KMLCOLOR,"circle"], #brewery, brew pub
"1541":["special_symbol_exclamation_mark","ff0000","octagon"], #danger #1 GMaps: "!" OSMAnd: exclamation
"1898":["special_symbol_exclamation_mark",KMLCOLOR,"octagon"], #danger #1 GMaps: "X" OSMAnd: exclamation
"1564":["amenity_fire_station","ff0000","octagon"], #danger #2 GMaps: OSMAnd: fire/explosion
"1710":["special_arrow_up_and_down","10c0f0","circle"], #river gauge, up/down arrow or thermometer
"1655":["amenity_police","1010a0","circle"], #ranger/police station #1
"1657":["amenity_police","1010a0","circle"], #ranger/police station #2
"1720":["wood","eecc22","circle"], #Park/National Park - yellow
"1701":["sport_swimming","eecc22","circle"], #Lake/swimmer - yellow
"1395":["sport_swimming","eecc22","circle"], #Lake/swimmer - yellow, old style icon
"1811":["special_sun","eecc22","circle"], #hot spring/sun - yellow
"1716":["route_railway_ref",KMLCOLOR,"circle"], #train station - purple
"1532":["route_bus_ref",KMLCOLOR,"circle"], #bus station or stop
"1626":["route_monorail_ref",KMLCOLOR,"circle"], #Metro, subway stop
"1534":["amenity_cafe",KMLCOLOR,"circle"], #cafe/coffe - blue
"1607":["amenity_cafe",KMLCOLOR,"circle"], #cafe/coffe - blue, old style ice cream cone icon
"1892":["waterfall","eecc22","circle"], #waterfall - yellow
"1634":["building_type_pyramid","eecc22","circle"], #Mountain Peak - yellow
"1684":["shop_department_store","10c0f0","circle"], #Store/shopping - blue
"1095":["shop_department_store","10c0f0","circle"], #Store/shopping - blue old style shopping icon
"1517":["amenity_bar",KMLCOLOR,"circle"], #Bar/cocktails/lounge - blue, old style icon
"979": ["special_sail_boat","a71de1","circle"], #Passenger ferry - purple
"1537":["special_sail_boat",KMLCOLOR,"circle"], #Auto Ferry
"1498":["place_town","0244D1","circle"], #town/city/village - Google circle with small square in center
"1521":["leisure_beach_resort","eecc22","circle"], #beach - yellow
"1703":["amenity_drinking_water","00842b","circle"], #Water Faucet - green
"1781":["sanitary_dump_station","10c0f0","circle"], #RV Dump station - light blue
"1798":["Winery",KMLCOLOR,"circle"], #Winery - light blue
"1636":["Museum",KMLCOLOR,"circle"], #Museum - light blue
"1289":["Museum","10c0f0","circle"], #Museum - light blue, old style icon
"1741":["special_wagon","10c0f0","circle"], #car rental - light blue
"1590":["shop_car_repair","10c0f0","circle"], #car/tire repair - light blue
"1659":["amenity_post_box","10c0f0","circle"], #post office
"1512":["amenity_atm","10c0f0","circle"], #bank/atm
"1870":["sport_scuba_diving",KMLCOLOR,"octagon"], #scuba, dive, snorkel site, google maps - snorkel mask, OSMAnd scuba diver
"1882":["reef",KMLCOLOR,"octagon"], #reef, tide pool - google maps starfish icon, OSMAnd seahorse/coral
"1573":["reef",KMLCOLOR,"octagon"], #reef, tide pool, fishing spot - google maps fish icon, OSMAnd seahorse/coral
"1569":["special_sail_boat",KMLCOLOR,"circle"], #Passenger Ferry
"1741":["special_wagon",KMLCOLOR,"circle"], #Car Rental
"1538":["special_wagon",KMLCOLOR,"circle"], #Car Rental
"1709":["amenity_cinema",KMLCOLOR,"circle"], #Cinema, movie, theater
"1615":["sport_canoe",KMLCOLOR,"circle"], #Kayak, kayak rental
"1598":["historic_castle",KMLCOLOR,"circle"], #castle, ruins
"1670":["building_type_church",KMLCOLOR,"circle"], #church, mosque, temple
"1877":["special_arrow_up_arrow_down",KMLCOLOR,"circle"], #stairway, for OSMAnd it's up/down arrow icon
}
waypt = cWaypoint("unknown",KMLCOLOR,"circle")
if not KMLIconID in iconDictionary:
KMLIconID = "unknown"
waypt.icon = iconDictionary[KMLIconID][0]
if iconDictionary[KMLIconID][1] == KMLCOLOR:
#use the icon color from the KML file
waypt.color = KMLCOLOR
else:
#use the icon color from the dictionary table
waypt.color = iconDictionary[KMLIconID][1]
waypt.background = iconDictionary[KMLIconID][2]
#print("icon:", waypt.icon, "color:", waypt.color, "background:",waypt.background)
return(waypt)
#========================================================================================
# addFileExtensionsTags
#========================================================================================
def addFileExtensionsTags(gpx,args):
# add extensions that apply to all tracks
# seems like this should be at the track level, but it's not
extensions = ET.SubElement(gpx,"extensions")
if args.width:
ET.SubElement(extensions, "osmand:width").text = str(args.width)
else:
ET.SubElement(extensions, "osmand:width").text = DEFAULT_TRACK_WIDTH
ET.SubElement(extensions, "osmand:show_arrows").text = "false"
ET.SubElement(extensions, "osmand:show_start_finish").text = "false"
# When you have multiple tracks in a file the split amounts are cummulative across
# all the tracks. Each track's split values don't start at zero.
if args.split == DEFAULT_TRACK_SPLIT:
ET.SubElement(extensions, "osmand:split_type").text = DEFAULT_TRACK_SPLIT
else:
ET.SubElement(extensions, "osmand:split_type").text = "distance"
#split interval is in meters and args.interval is in miles, so convert miles to meters
ET.SubElement(extensions, "osmand:split_interval").text = str(int(float(args.split) * 1609.34))
return
#========================================================================================
# writeGPXFile
#========================================================================================
def writeGPXFile(gpx,outputFilename):
# Create the ElementTree object with pretty printing options
tree = ET.ElementTree(gpx)
tree_str = ET.tostring(gpx, encoding="utf-8", xml_declaration=True)
pretty_tree_str = minidom.parseString(tree_str).toprettyxml(indent=" ", encoding="utf-8").decode()
# Write the pretty-printed GPX XML to a file
with open(outputFilename, "w",encoding="utf-8") as f:
f.write(pretty_tree_str)
#========================================================================================
# addGPXElement
#========================================================================================
def addGPXElement():
namespaces = {
"xmlns": "http://www.topografix.com/GPX/1/1",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation": "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd",
"xmlns:osmand": "https://osmand.net",
}
gpx = ET.Element("gpx", namespaces)
gpx.set("version", "1.1")
gpx.set("creator", "KMLtoOSMAndGPX")
return(gpx)
#========================================================================================
# processWaypoint
#========================================================================================
def processWaypoint(placemark,gpx):
global countFolderWaypoints
global countTotalWaypoints
# Get the coordinates from the KML Point element
point = placemark.find(".//{http://www.opengis.net/kml/2.2}Point/{http://www.opengis.net/kml/2.2}coordinates")
if point is not None:
countFolderWaypoints += 1
countTotalWaypoints += 1
coordinates = point.text.strip().split(",")
longitude = coordinates[0]
latitude = coordinates[1]
elevation = f"{float(coordinates[2]):.1f}"
#print("long",longitude, "lat",latitude,"elev",elevation)
# Create the GPX Waypoint element
waypoint = ET.SubElement(gpx, "wpt", lat=latitude, lon=longitude)
# Add the name and description from the KML Placemark element, if available
# add elevation
ET.SubElement(waypoint,"ele").text = elevation
name = placemark.find(".//{http://www.opengis.net/kml/2.2}name")
if name is not None:
#name = html_escape(name.text.strip())
name = name.text.strip()
print(" WayPt:",name)
ET.SubElement(waypoint, "name").text = name
description = placemark.find(".//{http://www.opengis.net/kml/2.2}description")
if description is not None:
#description = html_escape(description.text.strip())
description = description.text.strip()
ET.SubElement(waypoint, "desc").text = description
# add extensions
extensions = ET.SubElement(waypoint,"extensions")
# Use styleURL tag value to extract color and icon ID
# New icons appear to be of this style with an icon ID and a color
# <styleUrl>#icon-1577-DB4436-labelson</styleUrl>
# Old style icons come in two flavors, neither of which has color info
# <styleUrl>#icon-1369</styleUrl>
# <styleUrl>#icon-1085-labelson</styleUrl>
#
# if there is no color field (get an exception on trying to access the field)
# then we will use the DEFAULT_ICON_COLOR value. If the second field contains
# the string "labelson" we'll also use the DEFAULT_ICON_COLOR value.
style_url = placemark.findtext(".//{http://www.opengis.net/kml/2.2}styleUrl")
if style_url:
style = style_url.split("-")
waypt = KMLToOSMAndIcon(style[1])
else:
waypt = KMLToOSMAndIcon("unknown")
#print("KMLToOSMAndIcon: icon:",waypt.icon,"color:",waypt.color,"background:",waypt.background)
ET.SubElement(extensions,"osmand:icon").text = waypt.icon
ET.SubElement(extensions,"osmand:background").text = waypt.background
if waypt.color == KMLCOLOR: # we use value from KML file
try:
if style[2] == "labelson": # there is no color value in styleURL string
waypt.color = DEFAULT_ICON_COLOR
else:
waypt.color=style[2]
except IndexError:
waypt.color=DEFAULT_ICON_COLOR
ET.SubElement(extensions, "osmand:color").text = "#" + waypt.color
#========================================================================================
# processTrack
#========================================================================================
def processTrack(placemark,gpx,args):
global countFolderTracks
global countTotalTracks
# Get the coordinates from the KML LineString element
linestring = placemark.find(".//{http://www.opengis.net/kml/2.2}LineString/{http://www.opengis.net/kml/2.2}coordinates")
if linestring is not None:
countFolderTracks += 1
countTotalTracks += 1
# Create the GPX Track element with the trackpoints
track = ET.Element("trk")
# Add the name and description from the KML Placemark element, if available
name = placemark.find(".//{http://www.opengis.net/kml/2.2}name")
if name is not None:
#name = html_escape(name.text.strip())
name = name.text.strip()
print(" Track:",name)
ET.SubElement(track, "name").text = name
description = placemark.find(".//{http://www.opengis.net/kml/2.2}description")
if description is not None:
#description = html_escape(description.text.strip())
description = description.text.strip()
ET.SubElement(track, "desc").text = description
coordinates = linestring.text.strip().split()
trackpoints = []
trkseg = ET.SubElement(track, "trkseg")
# Iterate over the coordinates and create GPX trackpoints
for coordinate in coordinates:
longitude, latitude, altitude = coordinate.split(",")
trackpoint = ET.Element("trkpt", lat=latitude, lon=longitude)
if altitude is not None:
ET.SubElement(trackpoint, "ele").text = f"{float(altitude):.1f}"
trackpoints.append(trackpoint)
for trackpoint in trackpoints:
trkseg.append(trackpoint)
extensions = ET.SubElement(track,"extensions")
# [0] [1] [2]
# color width
# <styleUrl>#line-0F9D58-1000</styleUrl>
#Color is standard RGB color with no transparency
#Line width is 1000-32000. This maps to 1.0-24.0 for OSMAnd line width
style_url = placemark.findtext(".//{http://www.opengis.net/kml/2.2}styleUrl")
if style_url:
style = style_url.split("-")
color = style[1]
#print("track color:",color)
else:
color = DEFAULT_TRACK_COLOR
if args.transparency:
transparency = args.transparency
else:
transparency = DEFAULT_TRACK_TRANSPARENCY
ET.SubElement(extensions, "osmand:color").text = "#" + transparency + color
#Seems like the <extensions> for track width, show_arrows and split
#should be associated with each track, but OSMAnd doesn't do it that way, it's file based
#If track level was supported the code to write those extensions tags would be moved here.
#
# To scale the width range of 1000-32000 from the KML file to a range of 1-24
# for OSMAnd in the gpx file, you can use the following formula:
# y = ((x - 1000) / 31000) * 23 + 1
# Where:
# x is the value in the original KML range of 1000-32000
# y is the scaled value in the GPX range of 1-24
# Add the GPX Track element to the GPX file
gpx.append(track)
#========================================================================================
#
#========================================================================================
def processFolder(element,gpx,args):
global countFolderWaypoints
global countFolderTracks
for placemark in element.findall(".//{http://www.opengis.net/kml/2.2}Placemark"):
processWaypoint(placemark,gpx)
for placemark in element.findall(".//{http://www.opengis.net/kml/2.2}Placemark"):
processTrack(placemark,gpx,args)
print(" Waypoint count:", countFolderWaypoints)
print(" Track count: ", countFolderTracks)
countFolderTracks = 0
countFolderWaypoints = 0
#========================================================================================
# Main
#========================================================================================
def main():
global countFolders
global countFolderTracks
global countFolderWaypoints
global countTotalTracks
global countTotalWaypoints
# Parse the command line arguments
args = setupParseCmdLine()
print("")
print("KML to OSMAnd GPX file conversion")
print(" Version:" + PROGRAM_VERSION)
print(" Input file: ", args.kml_file)
print(" Output file: ", args.gpx_file)
print(" Output file path: ", ntpath.dirname(args.gpx_file))
print(" Output file name: ", ntpath.basename(args.gpx_file))
print(" Layer flag: ", args.layers)
print(" Transparency value: 0x", args.transparency)
print(" Track width: ", args.width)
print(" Track split: ", args.split)
print("")
print("Starting conversion...")
# Parse the KML file
tree = ET.parse(args.kml_file)
root = tree.getroot()
# process the KML file a folder at a time. If the -l flag is specified each folder's data
# will get written to a separate GPX file. If the -l flag is not specifgied than all
# the data is saved up and written to a single file.
#Note: the iter() function returns an iterator. There appears to be no clean way to
#check if the iterator is empty without using the first element. The countFolders value
#is used at the end of the folder for loop to see if any folders were processed. If not,
#then waypoints and tracks are processed at the root level instead of <folders> level.
folders = root.iter('{http://www.opengis.net/kml/2.2}Folder')
for folder in folders:
#Add the GPX element - just once if all folders are put into one GPX file,
#else if args.layers argument is specified add a GPX element for each folder
if args.layers or (countFolders == 0):
gpx = addGPXElement()
# Extract the folder name from the KML file
folder_name = folder.find('{http://www.opengis.net/kml/2.2}name').text
print("")
if folder_name in LAYERS_TO_IGNORE:
print("Skipping layer:", folder_name)
continue
countFolders += 1
print("Processing layer#:",countFolders, "layer:",folder_name)
processFolder(folder,gpx,args)
#If the layers command line switch was specified then we write out each folder
#as a separate GPX file.
if args.layers:
outputFilename = ntpath.join(ntpath.dirname(args.gpx_file),ntpath.basename(args.gpx_file) + "-" + folder_name + ".gpx")
print("Writing GPX output file for layer:",folder_name,"to file:",outputFilename)
addFileExtensionsTags(gpx,args)
writeGPXFile(gpx,outputFilename)
#processed all folders, but if it was a single layer export from google maps there will be
#no folders, just waypoint and tracks at the <document> level.
if countFolders == 0:
print("")
print("No folders found")
gpx = addGPXElement()
processFolder(root,gpx,args)
print("")
print(" Total waypoint count:", countTotalWaypoints)
print(" Total track count: ", countTotalTracks)
print(" Total folder count: ", countFolders)
#Done processing the file and if we are not writing individual folder/layer
#files then write out the one and one gpx file.
#There's a corner case here if there are no folders and the -l (layers) flag
#was specified. Could either not write out any data because there are no layers
#or write a single file - which is what we now do here.
if (countFolders == 0) or (not args.layers):
print("Writing single GPX output file:",args.gpx_file)
addFileExtensionsTags(gpx,args)
writeGPXFile(gpx,args.gpx_file)
if __name__ == "__main__":
main()