Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #By IndexCase 2023
- import re
- import json
- import os
- import time
- def dms_to_dd(d, m, s=0):
- """Convert degrees, minutes, and seconds to decimal degrees."""
- return d + m / 60 + s / 3600
- def parse_waypoint(waypoint_raw):
- lines = waypoint_raw.strip().split("\n")
- # Look for the WP: line
- wp_name = None
- if lines[0].startswith("WP:"):
- wp_name = lines[0].split(":", 1)[1].strip()
- # Remove the WP: line to ensure the following code works as expected
- lines = lines[1:]
- # Look for DMS or DMM format
- match_lat = re.search(r"N (\d+)°(\d+)'([\d.]+)?", lines[0])
- match_long = re.search(r"E (\d+)°(\d+)'([\d.]+)?", lines[0])
- # Try DMM if DMS didn't match
- if not match_lat or not match_long:
- match_lat = re.search(r"N (\d+)°([\d.]+)'", lines[0])
- match_long = re.search(r"E (\d+)°([\d.]+)'", lines[0])
- if match_lat and match_long:
- lat = dms_to_dd(float(match_lat.group(1)), float(match_lat.group(2)))
- long = dms_to_dd(float(match_long.group(1)), float(match_long.group(2)))
- else:
- raise ValueError(f"Failed to parse lat-long from: \n{lines[0]}")
- else:
- lat = dms_to_dd(float(match_lat.group(1)), float(match_lat.group(2)), float(match_lat.group(3) or 0))
- long = dms_to_dd(float(match_long.group(1)), float(match_long.group(2)), float(match_long.group(3) or 0))
- # Extract elevation in feet
- match = next((re.search(r"(\d+)m", line) for line in lines if re.search(r"(\d+)m", line)), None)
- if not match:
- raise ValueError(f"Failed to parse elevation from waypoint data:\n{waypoint_raw}")
- elevation = float(match.group(1))
- return {
- "name": wp_name, # It could be None here
- "lat": lat,
- "long": long,
- "elev": elevation
- }
- def process_waypoints():
- with open(input_file_path, 'r', encoding='utf-8') as file:
- data = file.read()
- waypoints_raw = [chunk for chunk in data.strip().split("\n\n") if len(chunk.split("\n")) > 1]
- print(f"Waypoints found: {waypoints_raw}")
- waypoints = []
- for idx, waypoint_raw in enumerate(waypoints_raw):
- try:
- waypoint = parse_waypoint(waypoint_raw)
- if not waypoint['name']: # if no name is provided, use a default one
- waypoint['name'] = "WAYPOINT" + str(idx)
- waypoints.append(waypoint)
- except ValueError as e:
- print(f"Skipped waypoint due to error: {e}")
- continue
- json_output = []
- for idx, waypoint in enumerate(waypoints):
- waypoint_name = waypoint['name']
- json_output.append({
- "id": idx + 2,
- "name": waypoint_name,
- "lat": round(waypoint["lat"], 9),
- "long": round(waypoint["long"], 9),
- "elev": round(waypoint["elev"], 2) # Rounded to two decimal places for feet
- })
- output_file_path = os.path.join(script_directory, 'scratchpad.tw')
- with open(output_file_path, 'w') as output_file:
- output_file.write(json.dumps(json_output, separators=(',', ':')))
- script_directory = os.path.dirname(os.path.abspath(__file__))
- input_file_path = os.path.join(script_directory, '0000.txt')
- # Process waypoints once on start
- process_waypoints()
- last_mod_time = os.path.getmtime(input_file_path)
- while True:
- try:
- current_mod_time = os.path.getmtime(input_file_path)
- if current_mod_time != last_mod_time:
- process_waypoints()
- last_mod_time = current_mod_time
- time.sleep(10) # Check for changes every 10 seconds
- except KeyboardInterrupt:
- print("Stopping script.")
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement