CineAI commited on
Commit
a6b09ed
·
verified ·
1 Parent(s): 53802b2

Create src/geo_info.py

Browse files
Files changed (1) hide show
  1. src/geo_info.py +54 -0
src/geo_info.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from typing import Optional, Tuple, Dict
3
+
4
+ R = 6371e3
5
+ N = 1e3
6
+
7
+
8
+ class GeoInfo:
9
+ def __init__(self, response: Dict[str, str], url: Optional[str] = None, coordinates: Optional[str] = None):
10
+ self.response = response
11
+
12
+ if self.response['coordinates'] == "":
13
+ raise ValueError(f"The coordinates from the model are empty: {self.response['coordinates']}")
14
+
15
+ if url:
16
+ coords_string = url.split("@")[1]
17
+ lat_str, lon_str, _ = coords_string.split(',', 2)
18
+ else:
19
+ coords_string = coordinates
20
+ lat_str, lon_str = coords_string.split(",", 1)
21
+ self.lat_1 = float(lat_str.strip())
22
+ self.lon_1 = float(lon_str.strip())
23
+ if response['coordinates'].split(",")[0].endswith('N') or response['coordinates'].split(",")[0].endswith('S') or response['coordinates'].split(",")[1].endswith('E') or response['coordinates'].split(",")[0].endswith('W'):
24
+ self.lat_2 = float(response['coordinates'].split(",")[0].split(" ")[0].strip())
25
+ self.lon_2 = float(response['coordinates'].split(",")[1].split(" ")[1].strip())
26
+ else:
27
+ self.lat_2 = float(response['coordinates'].split(",")[0].strip())
28
+ self.lon_2 = float(response['coordinates'].split(",")[1].strip())
29
+
30
+ self.phi_1 = self.lat_1 * math.pi / 180
31
+ self.phi_2 = self.lat_2 * math.pi / 180
32
+ self.delta_gamma = (self.lon_2 - self.lon_1) * math.pi / 180
33
+
34
+ def calculate_error(self) -> Tuple[float, float, float, float, float]:
35
+ delta_phi = (self.lat_2 - self.lat_1) * math.pi / 180
36
+ a = math.sin(delta_phi / 2) * math.sin(delta_phi / 2) + math.cos(self.phi_1) * math.cos(self.phi_2) * math.sin(self.delta_gamma / 2) * math.sin(self.delta_gamma / 2)
37
+ c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
38
+ error = (R * c) / N
39
+ return error, self.lat_1, self.lon_1, self.lat_2, self.lon_2
40
+
41
+ def calculate_bearing(self) -> float:
42
+ y = math.sin(self.delta_gamma) * math.cos(self.phi_2)
43
+ x = math.cos(self.phi_1) * math.sin(self.phi_2) - math.sin(self.phi_1) * math.cos(self.phi_2) * math.cos(self.delta_gamma)
44
+
45
+ theta = math.atan2(y, x)
46
+ bearing = (theta * 180 / math.pi + 360) % 360
47
+ return bearing
48
+
49
+ def get_direction(self, angle: float) -> str:
50
+ directions = ['North', 'North-East', 'East', 'South-East', 'South', 'South-West', 'West', 'North-West'];
51
+ return directions[round(angle / 45) % 8]
52
+
53
+ def combine_info(self, error: float, direction: str) -> str:
54
+ return f"Point 1 is approximately {error} km away from Point 2 in the {direction} direction"