1 # Copyright 2024 highstreet technologies
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 A collection of TypeDefinitions for a geographical location
21 from typing import Any, TypedDict, cast
23 from typing_extensions import Required
25 from network_generation.model.python.point import Point
28 class IGeoLocation(TypedDict):
29 latitude: Required[float]
30 longitude: Required[float]
31 aboveMeanSeaLevel: Required[float]
34 default_value: IGeoLocation = {
37 "aboveMeanSeaLevel": 0,
43 def default() -> dict[str, Any]:
44 return cast(dict[str, Any], default_value)
46 _equatorialRadius = 6378137 # meters
47 _polarRadius = 6356752 # meters
49 def __init__(self, data: IGeoLocation = default_value) -> None:
50 self.latitude = data["latitude"]
51 self.longitude = data["longitude"]
52 self.aboveMeanSeaLevel = data["aboveMeanSeaLevel"]
55 def equatorialRadius(self) -> int:
56 return GeoLocation._equatorialRadius
59 def polarRadius(self) -> int:
60 return GeoLocation._polarRadius
63 def latitude(self) -> float:
67 def latitude(self, value: float) -> None:
68 if not (-90 <= value <= 90):
69 msg: str = "Invalid latitude. Latitude must be between -90 and 90."
71 self._latitude = value
74 def longitude(self) -> float:
75 return self._longitude
78 def longitude(self, value: float) -> None:
79 if not (-180 <= value <= 180):
81 "Invalid longitude. Longitude must be between -180 and 180."
83 self._longitude = value
86 def aboveMeanSeaLevel(self) -> float:
87 return self._aboveMeanSeaLevel
89 @aboveMeanSeaLevel.setter
90 def aboveMeanSeaLevel(self, value: float) -> None:
91 if not (-180 <= value <= 180):
93 "Invalid longitude. Longitude must be between -180 and 180."
95 self._aboveMeanSeaLevel = value
97 def json(self) -> dict[str, float]:
99 "latitude": self.latitude,
100 "longitude": self.longitude,
101 "aboveMeanSeaLevel": self.aboveMeanSeaLevel,
104 def __str__(self) -> str:
105 return str(self.json())
107 def point_to_geo_location(self, point: Point) -> Any:
109 A static function which converts a point in pixels into a geographical
110 location when the self is represented as Point(0,0)
111 @param point : The point to be converted
112 returns The converted GeoLocation object.
114 new_lat = self.latitude + (point.y / self.equatorialRadius) * (
117 new_lon = self.longitude + (point.x / self.equatorialRadius) * (
119 ) / math.cos(self.latitude * math.pi / 180)
121 geo_location: IGeoLocation = {
122 "longitude": new_lon,
124 "aboveMeanSeaLevel": self.aboveMeanSeaLevel,
126 return GeoLocation(geo_location)