@@ -23,7 +23,7 @@ def __init__(self, root):
2323 self .status_label = tk .Label (root , text = "" , width = 30 )
2424 self .status_label .grid (row = 4 , column = 1 , columnspan = 3 , sticky = "ew" , padx = 25 , pady = 5 )
2525
26- self .button1 = tk .Button (root , text = "GPX -> CNX" , command = lambda :[self .status_label .config (text = "" ), self .select_files ()])
26+ self .button1 = tk .Button (root , text = "GPX -> CNX" , command = lambda : [self .status_label .config (text = "" ), self .select_files ()])
2727 self .button1 .grid (row = 1 , column = 1 , columnspan = 1 , sticky = "ew" , padx = 25 , pady = 5 )
2828
2929 self .button2 = tk .Button (root , text = "POI TYPES EDITOR" , command = self .poi_editor )
@@ -59,7 +59,7 @@ def log_window(self):
5959 textinfo = ""
6060 flogout = 0
6161 try :
62- with open ("log.txt" , "r" ) as logtxt :
62+ with open ("log.txt" , "r" , encoding = "utf-8" ) as logtxt :
6363 textinfo = logtxt .read ()
6464 flogout = 1
6565 except FileNotFoundError :
@@ -103,11 +103,10 @@ def calc_distance(self):
103103
104104 # Converts a list of GPX files to CNX xml format
105105 def convert_gpx2cnx (self , gpx_files ):
106- output_dir = os .path .join (os .path .normpath (os .path .split (gpx_files [0 ])[0 ]),'cnx_routes' )
106+ output_dir = os .path .join (os .path .normpath (os .path .split (gpx_files [0 ])[0 ]), 'cnx_routes' )
107107 if not os .path .exists (output_dir ):
108108 os .makedirs (output_dir )
109109
110- log = "gpx2cnx.py log"
111110 self .results = []
112111 for gpx_file in gpx_files :
113112 try :
@@ -171,10 +170,10 @@ def convert_gpx2cnx(self, gpx_files):
171170 first_lon = track_points [0 ]['lon' ]
172171 first_ele = (track_points [0 ]['ele' ] * 100 ).quantize (Decimal ('1' ), decimal .ROUND_HALF_UP )
173172
174- relative_points = [f"{ first_lat } ,{ first_lon } ,{ first_ele } " ] # First Point - Absolute Coordinates
173+ relative_points = [f"{ first_lat } ,{ first_lon } ,{ first_ele } " ] # First Point - Absolute Coordinates
175174
176175 first_diffs = []
177- for i in range (1 , len (track_points )): # Calc first diffs
176+ for i in range (1 , len (track_points )): # Calc first diffs
178177 lat1 = track_points [i - 1 ]['lat' ]
179178 lon1 = track_points [i - 1 ]['lon' ]
180179 lat2 = track_points [i ]['lat' ]
@@ -186,14 +185,14 @@ def convert_gpx2cnx(self, gpx_files):
186185
187186 first_diffs .append ((lat_diff , lon_diff , ele_diff ))
188187
189- if i == 1 : # Second Point
188+ if i == 1 : # Second Point
190189 lat_diff = lat_diff .quantize (Decimal ('1' ), decimal .ROUND_HALF_UP )
191190 lon_diff = lon_diff .quantize (Decimal ('1' ), decimal .ROUND_HALF_UP )
192191 ele_diff = ele_diff .quantize (Decimal ('1' ), decimal .ROUND_HALF_UP )
193192
194193 relative_points .append (f"{ lat_diff } ,{ lon_diff } ,{ ele_diff } " )
195194
196- for i in range (1 , len (first_diffs )): # Calc second diffs
195+ for i in range (1 , len (first_diffs )): # Calc second diffs
197196 lat_diff = (first_diffs [i ][0 ] - first_diffs [i - 1 ][0 ]).quantize (Decimal ('1' ), decimal .ROUND_HALF_UP )
198197 lon_diff = (first_diffs [i ][1 ] - first_diffs [i - 1 ][1 ]).quantize (Decimal ('1' ), decimal .ROUND_HALF_UP )
199198 ele_diff = (first_diffs [i ][2 ]).quantize (Decimal ('1' ), decimal .ROUND_HALF_UP )
@@ -216,7 +215,8 @@ def convert_gpx2cnx(self, gpx_files):
216215
217216 if relative_points :
218217 tracks_str = ';' .join (relative_points )
219- ET .SubElement (route , 'Tracks' ).text = tracks_str
218+ format_tracks_str = tracks_str + ';' # Line-ending delimiter as in native CNX
219+ ET .SubElement (route , 'Tracks' ).text = format_tracks_str
220220 else :
221221 ET .SubElement (route , 'Tracks' ).text = ""
222222
@@ -228,16 +228,17 @@ def convert_gpx2cnx(self, gpx_files):
228228 point = ET .SubElement (points , 'Point' )
229229 ET .SubElement (point , 'Lat' ).text = str (wpt ['lat' ])
230230 ET .SubElement (point , 'Lng' ).text = str (wpt ['lon' ])
231- ET .SubElement (point , 'Type' ).text = '0' # Type 0 - point marker without differentiation by purpose
231+ ET .SubElement (point , 'Type' ).text = '0' # Type 0 - point marker without differentiation by purpose
232232 ET .SubElement (point , 'Descr' ).text = wpt ['name' ]
233233
234234 filename = os .path .splitext (os .path .basename (gpx_file ))[0 ]
235235 trim_filename = filename [:18 ]
236236 output_filename = os .path .join (output_dir , f"route_{ trim_filename } .cnx" )
237237 xml_string = self .prettify (route )
238+ format_xml_string = xml_string .replace ("<Navs/>" , "<Navs />" ) # Format the XML tag like in native CNX
238239 with codecs .open (output_filename , "w" , encoding = 'utf-8-sig' ) as f :
239240 f .write ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n ' )
240- f .write (xml_string [ xml_string .find ('<Route' ):])
241+ f .write (format_xml_string [ format_xml_string .find ('<Route' ):])
241242
242243 self .results .append (f"-- { gpx_file } -> SUCCESS" )
243244 self .rstat = 1
0 commit comments