Move all business logic code under template folder
[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 from network_generation.model.python.point import Point
22
23
24 class IGeoLocationData:
25
26     def __init__(self, latitude: float, longitude: float, aboveMeanSeaLevel: float):
27         self.latitude = latitude
28         self.longitude = longitude
29         self.aboveMeanSeaLevel = aboveMeanSeaLevel
30
31 class IGeoLocation:
32
33     def __init__(self, latitude: float = 0, longitude: float = 0, aboveMeanSeaLevel: float = 0):
34         self.latitude = latitude
35         self.longitude = longitude
36         self.aboveMeanSeaLevel = aboveMeanSeaLevel
37
38     def __str__(self)-> str:
39         return f'lat : {self.latitude} : lon : {self.longitude} : amsl : {self.aboveMeanSeaLevel}'
40
41
42 class GeoLocation(IGeoLocation):
43     _equatorialRadius = 6378137  # meters
44     _polarRadius = 6356752  # meters
45
46     def __init__(self, geoLocation: IGeoLocationData = None):
47         super().__init__(
48             geoLocation['latitude'] if geoLocation else 0,
49             geoLocation['longitude'] if geoLocation else 0,
50             geoLocation['aboveMeanSeaLevel'] if geoLocation else 0
51         )
52
53     @property
54     def equatorialRadius(self) -> int:
55         return GeoLocation._equatorialRadius
56
57     @property
58     def polarRadius(self)-> int:
59         return GeoLocation._polarRadius
60
61     def set_latitude(self, value: float):
62         if not (-90 <= value <= 90):
63             raise ValueError('Invalid latitude. Latitude must be between -90 and 90.')
64         self.latitude = value
65
66     def set_longitude(self, value: float):
67         if not (-180 <= value <= 180):
68             raise ValueError('Invalid longitude. Longitude must be between -180 and 180.')
69         self.longitude = value
70
71     def json(self) -> dict[str, float]:
72         return {
73             "latitude": self.latitude, 
74             "longitude": self.longitude, 
75             "aboveMeanSeaLevel": self.aboveMeanSeaLevel,      
76         }
77     
78     def __str__(self) -> str:
79         return str(self.json())
80     
81     def point_to_geo_location(self, point:Point):
82         """
83         A static function which converts a point in pixels into a geographical location
84         when the self is represented as Point(0,0)
85         @param point : The point to be converted
86         returns The converted GeoLocation object.
87         """
88         equatorialRadius = 6378137  # meters
89         new_lat = self.latitude + (point.y / equatorialRadius) * (180 / math.pi)
90         new_lon = self.longitude + (point.x / equatorialRadius) * (180 / math.pi) / math.cos(self.latitude * math.pi / 180)
91
92         geo_location: IGeoLocationData = {
93             "longitude": new_lon,
94             "latitude": new_lat,
95             "aboveMeanSeaLevel": self.aboveMeanSeaLevel
96         }
97         return GeoLocation(geo_location)