3b6793a93b774ebe1f4eeab775f19b72567d11a2
[oam.git] / code / network-generator / network_generation / model / python / geo_location.py
1 # Copyright 2023 highstreet technologies GmbH
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 #!/usr/bin/python
16
17 """
18 A collection of TypeDefinitions for a geographical location
19 """
20 import math
21
22 from network_generation.model.python.point import Point
23
24
25 class IGeoLocationData:
26     def __init__(
27         self, latitude: float, longitude: float, aboveMeanSeaLevel: float
28     ):
29         self.latitude = latitude
30         self.longitude = longitude
31         self.aboveMeanSeaLevel = aboveMeanSeaLevel
32
33
34 class IGeoLocation:
35     def __init__(
36         self,
37         latitude: float = 0,
38         longitude: float = 0,
39         aboveMeanSeaLevel: float = 0,
40     ):
41         self.latitude = latitude
42         self.longitude = longitude
43         self.aboveMeanSeaLevel = aboveMeanSeaLevel
44
45     def __str__(self) -> str:
46         return f"lat : {self.latitude} : lon : {self.longitude} : amsl : {self.aboveMeanSeaLevel}"
47
48
49 class GeoLocation(IGeoLocation):
50     _equatorialRadius = 6378137  # meters
51     _polarRadius = 6356752  # meters
52
53     def __init__(self, geoLocation: IGeoLocationData = None):
54         super().__init__(
55             geoLocation["latitude"] if geoLocation else 0,
56             geoLocation["longitude"] if geoLocation else 0,
57             geoLocation["aboveMeanSeaLevel"] if geoLocation else 0,
58         )
59
60     @property
61     def equatorialRadius(self) -> int:
62         return GeoLocation._equatorialRadius
63
64     @property
65     def polarRadius(self) -> int:
66         return GeoLocation._polarRadius
67
68     def set_latitude(self, value: float):
69         if not (-90 <= value <= 90):
70             raise ValueError(
71                 "Invalid latitude. Latitude must be between -90 and 90."
72             )
73         self.latitude = value
74
75     def set_longitude(self, value: float):
76         if not (-180 <= value <= 180):
77             raise ValueError(
78                 "Invalid longitude. Longitude must be between -180 and 180."
79             )
80         self.longitude = value
81
82     def json(self) -> dict[str, float]:
83         return {
84             "latitude": self.latitude,
85             "longitude": self.longitude,
86             "aboveMeanSeaLevel": self.aboveMeanSeaLevel,
87         }
88
89     def __str__(self) -> str:
90         return str(self.json())
91
92     def point_to_geo_location(self, point: Point):
93         """
94         A static function which converts a point in pixels into a geographical location
95         when the self is represented as Point(0,0)
96         @param point : The point to be converted
97         returns The converted GeoLocation object.
98         """
99         equatorialRadius = 6378137  # meters
100         new_lat = self.latitude + (point.y / equatorialRadius) * (
101             180 / math.pi
102         )
103         new_lon = self.longitude + (point.x / equatorialRadius) * (
104             180 / math.pi
105         ) / math.cos(self.latitude * math.pi / 180)
106
107         geo_location: IGeoLocationData = {
108             "longitude": new_lon,
109             "latitude": new_lat,
110             "aboveMeanSeaLevel": self.aboveMeanSeaLevel,
111         }
112         return GeoLocation(geo_location)