-
Notifications
You must be signed in to change notification settings - Fork 7
/
localize_event.py
103 lines (82 loc) · 3.78 KB
/
localize_event.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
# Copyright (c) 2023 Kim Hendrikse
import datetime
import math
import sys
from opensoundscape.localization import localize
if len(sys.argv) > 1:
if sys.argv[1] == "--help":
print("Usage: python3 localize_event.py [temperature_in_celsius]")
print("Temperature is used to compute the speed of sound. If not provided, defaults to 343 m/s.")
sys.exit()
try:
temp_in_celsius = float(sys.argv[1])
speed_of_sound = 333.1 + 0.6 * temp_in_celsius
except ValueError:
print("Invalid temperature value. Please provide a valid number or use --help for usage information.")
sys.exit()
else:
speed_of_sound = 343
print("Enter GPS coordinates and timestamps. Press enter twice to finish.")
def gps_to_cartesian_2d(lat, lon, ref_lat=None, ref_lon=None):
R = 6371000
if ref_lat is None or ref_lon is None:
ref_lat, ref_lon = lat, lon
lat_rad, lon_rad = math.radians(lat), math.radians(lon)
ref_lat_rad, ref_lon_rad = math.radians(ref_lat), math.radians(ref_lon)
x = R * (lon_rad - ref_lon_rad) * math.cos(ref_lat_rad)
y = R * (lat_rad - ref_lat_rad)
return x, y
def cartesian_2d_to_gps(x, y, ref_lat, ref_lon):
R = 6371000
ref_lat_rad, ref_lon_rad = math.radians(ref_lat), math.radians(ref_lon)
delta_lat_rad = y / R
delta_lon_rad = x / (R * math.cos(ref_lat_rad))
lat_rad = ref_lat_rad + delta_lat_rad
lon_rad = ref_lon_rad + delta_lon_rad
lat, lon = math.degrees(lat_rad), math.degrees(lon_rad)
return lat, lon
receiver_gps = []
timestamps = []
while True:
try:
line = input().strip()
if not line:
break
lat_lon, timestamp = line.split(maxsplit=1)
lat, lon = lat_lon.split(',')
receiver_gps.append((float(lat), float(lon)))
timestamps.append(timestamp)
except EOFError:
break
except ValueError as e:
print(f"Error reading input: {e}")
# Convert GPS to Cartesian 2D
ref_point = receiver_gps[0]
receiver_locations = [gps_to_cartesian_2d(lat, lon, ref_point[0], ref_point[1]) for lat, lon in receiver_gps]
# Convert timestamps to datetime objects
datetimes = [datetime.datetime.strptime(ts, "%Y-%m-%d_%H-%M-%S.%f") for ts in timestamps]
# Convert datetime objects to relative arrival times in seconds
earliest_time = min(datetimes)
arrival_times = [(dt - earliest_time).total_seconds() for dt in datetimes]
estimated_location_cartesian = localize(receiver_locations, arrival_times, 'soundfinder', speed_of_sound)
# Convert the estimated location back to GPS
estimated_location = cartesian_2d_to_gps(estimated_location_cartesian[0], estimated_location_cartesian[1], ref_point[0], ref_point[1])
# ... [Previous input handling and calculations]
if isinstance(estimated_location_cartesian, list) or not all(
isinstance(coord, (float, int)) for coord in estimated_location_cartesian):
print("Error in sound source localization.")
else:
# Convert the estimated location back to GPS
estimated_location = cartesian_2d_to_gps(estimated_location_cartesian[0], estimated_location_cartesian[1],
ref_point[0], ref_point[1])
osm_link = f"https://www.openstreetmap.org/?mlat={estimated_location[0]}&mlon={estimated_location[1]}#map=15/{estimated_location[0]}/{estimated_location[1]}"
# google_maps_link = f"https://www.google.com/maps/@?api=1&map_action=map¢er={estimated_location[0]},{estimated_location[1]}&basemap=satellite&zoom=15"
google_maps_link = f"https://www.google.com/maps?q={estimated_location[0]},{estimated_location[1]}&t=h&z=15"
print(f"Location: {estimated_location[0]},{estimated_location[1]}")
print()
print("Web links:")
print()
print(f'OpenStreetMap: {osm_link}')
print()
print(f'Google Maps: {google_maps_link}')
print()