- File:
-
- 1 edited
-
src/python/bin/gnuboat-log-converter (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/python/bin/gnuboat-log-converter
r26f77a6 r0c14082 34 34 35 35 regex_nmea_sentence = re.compile(r"^([^\$!]{0,})([\$!]{1}[\w]{2}[\w]{3}[^\*]+\*[\da-zA-Z]+)$") 36 regex_nmea_sentence_without_checksum = re.compile(r"^([^\$!]{0,})([\$!]{1}[\w]{2}[\w]{3}[^\*]+)\*[\da-zA-Z]+$") 37 regex_nmea_position_latitude = re.compile(r"^([0-9]{2})([0-9]{2}\.[0-9]+)$") 38 regex_nmea_position_longitude = re.compile(r"^([0-9]{3})([0-9]{2}\.[0-9]+)$") 36 regex_nmea_position = re.compile(r"^([0-9]+)([0-9]{2}\.[0-9]+)$") 39 37 40 38 def toFloat(string, current_row): … … 48 46 return 0 49 47 50 def nmea LatitudeToDecimalDegrees(nmea_value, current_row):51 global regex_nmea_position _latitude48 def nmeaToDecimalDegrees(nmea_value, current_row): 49 global regex_nmea_position 52 50 53 51 if toFloat(nmea_value, current_row) == 0: 54 52 return 0 55 53 56 matches = regex_nmea_position _latitude.match(nmea_value)54 matches = regex_nmea_position.match(nmea_value) 57 55 if matches is None: 58 print('INFO: Failed to convert latitudeposition value from line {} into decimal degrees: {}'.format(current_row, nmea_value))56 print('INFO: Failed to convert position value from line {} into decimal degrees: {}'.format(current_row, nmea_value)) 59 57 return 60 58 decimalDegrees = toFloat(matches.group(1), current_row) … … 62 60 return decimalDegrees + (decimalMinutes / 60.0) 63 61 64 def nmeaLongitudeToDecimalDegrees(nmea_value, current_row): 65 global regex_nmea_position_longitude 66 67 if toFloat(nmea_value, current_row) == 0: 68 return 0 69 70 matches = regex_nmea_position_longitude.match(nmea_value) 71 if matches is None: 72 print('INFO: Failed to convert longitude position value from line {} into decimal degrees: {}'.format(current_row, nmea_value)) 73 return 74 decimalDegrees = toFloat(matches.group(1), current_row) 75 decimalMinutes = toFloat(matches.group(2), current_row) 76 return decimalDegrees + (decimalMinutes / 60.0) 77 78 def convertToCsv(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time): 62 def convertToCsv(input_file_path, output_file_path, errors_ignore, strip_time): 79 63 global regex_nmea_sentence 80 global regex_nmea_sentence_without_checksum81 64 82 65 try: … … 104 87 for line in input_file: 105 88 line = line.strip() 106 if checksums_fix: 107 matches = regex_nmea_sentence_without_checksum.match(line) 108 pynmea_parse_check = False 109 else: 110 matches = regex_nmea_sentence.match(line) 111 pynmea_parse_check = True 89 matches = regex_nmea_sentence.match(line) 112 90 if matches is None: 113 91 print('INFO: Failed to parse nmea sentence ({}) at line {}\n'.format(line, current_row)) … … 117 95 sys.exit(4) 118 96 else: 119 nmea_sentence = pynmea2.parse(matches.group(2) , check=pynmea_parse_check)97 nmea_sentence = pynmea2.parse(matches.group(2)) 120 98 if nmea_sentence.sentence_type == 'RMC': 121 99 if last_row is not None: … … 123 101 last_row = {'datetime': '{} {}'.format(nmea_sentence.datestamp, nmea_sentence.timestamp), 124 102 'gps_status': nmea_sentence.status, 125 'latitude': nmea LatitudeToDecimalDegrees(nmea_sentence.lat, current_row),103 'latitude': nmeaToDecimalDegrees(nmea_sentence.lat, current_row), 126 104 'latitude_direction': nmea_sentence.lat_dir, 127 'longitude': nmea LongitudeToDecimalDegrees(nmea_sentence.lon, current_row),105 'longitude': nmeaToDecimalDegrees(nmea_sentence.lon, current_row), 128 106 'longitude_direction': nmea_sentence.lon_dir, 129 107 'speed_over_ground': toFloat(nmea_sentence.spd_over_grnd, current_row), … … 141 119 input_file.close() 142 120 143 def convertToNmea0183(input_file_path, output_file_path, errors_ignore, checksums_fix,strip_time):121 def convertToNmea0183(input_file_path, output_file_path, errors_ignore, strip_time): 144 122 global regex_nmea_sentence 145 global regex_nmea_sentence_without_checksum146 123 147 124 try: … … 161 138 for line in input_file: 162 139 line = line.strip() 163 if checksums_fix: 164 matches = regex_nmea_sentence_without_checksum.match(line) 165 pynmea_parse_check = False 166 else: 167 matches = regex_nmea_sentence.match(line) 168 pynmea_parse_check = True 140 matches = regex_nmea_sentence.match(line) 169 141 if matches is None: 170 142 print('INFO: Failed to parse nmea sentence ({}) at line {}\n'.format(line, current_row)) … … 174 146 sys.exit(4) 175 147 else: 176 nmea_sentence = pynmea2.parse(matches.group(2) , check=pynmea_parse_check)148 nmea_sentence = pynmea2.parse(matches.group(2)) 177 149 output_string = str(nmea_sentence) 178 150 if not strip_time and matches.group(1): … … 182 154 input_file.close() 183 155 184 def convertToGpx(input_file_path, output_file_path, errors_ignore, checksums_fix, strip_time): 156 def convertToGpx(input_file_path, output_file_path, errors_ignore, strip_time): 157 global regex_nmea_sentence 158 185 159 # make sure, nmea input file, does not have timestamps in front of the nmea 186 160 # sentences 187 161 [nmea_without_timestamps_handle, nmea_without_timestamp_file_path] = tempfile.mkstemp(text=True) 188 convertToNmea0183(input_file_path, nmea_without_timestamp_file_path, errors_ignore, checksums_fix,True)162 convertToNmea0183(input_file_path, nmea_without_timestamp_file_path, errors_ignore, True) 189 163 190 164 subprocess.call(['gpsbabel', '-i', 'nmea', '-f', nmea_without_timestamp_file_path, '-o', 'gpx', '-F', output_file_path]) … … 208 182 parser.add_argument('-i', '--ignore-errors', dest='errors_ignore', action='store_false', 209 183 help='Ignore any errors reading the log file') 210 parser.add_argument('-c', '--checksums-fix', dest='checksums_fix', action='store_true',211 help='Ignore all checksums (and replace them with a new correct one, if needed)')212 184 parser.add_argument('input_file', metavar='input_file_path', type=str, 213 185 help='File to be converted (input)') … … 231 203 errors_ignore = arguments.errors_ignore 232 204 233 if arguments.checksums_fix is not None:234 checksums_fix = arguments.checksums_fix235 236 205 if arguments.strip_time is not None: 237 206 strip_time = arguments.strip_time … … 240 209 print('ERROR: No input file specified') 241 210 sys.exit(2) 242 #elif not os.path.exists(arguments.input_file):211 elif not os.path.exists(arguments.input_file): 243 212 print('ERROR: Specified input file does not exists: {}'.format(arguments.input_file)) 244 213 sys.exit(3) … … 256 225 257 226 if output_format == Format.CSV.value: 258 convertToCsv(input_file_path, output_file_path, errors_ignore, checksums_fix,strip_time)227 convertToCsv(input_file_path, output_file_path, errors_ignore, strip_time) 259 228 elif output_format == Format.NMEA0183.value: 260 convertToNmea0183(input_file_path, output_file_path, errors_ignore, checksums_fix,strip_time)229 convertToNmea0183(input_file_path, output_file_path, errors_ignore, strip_time) 261 230 elif output_format == Format.GPX.value: 262 convertToGpx(input_file_path, output_file_path, errors_ignore, checksums_fix,strip_time)231 convertToGpx(input_file_path, output_file_path, errors_ignore, strip_time) 263 232 else: 264 233 print('ERROR: Undefined output format!\n')
Note:
See TracChangeset
for help on using the changeset viewer.
