1 # Copyright 2023 highstreet technologies GmbH
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 model.python.point import Point
24 class IGeoLocationData:
26 def __init__(self, latitude: float, longitude: float, aboveMeanSeaLevel: float):
27 self.latitude = latitude
28 self.longitude = longitude
29 self.aboveMeanSeaLevel = aboveMeanSeaLevel
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
38 def __str__(self)-> str:
39 return f'lat : {self.latitude} : lon : {self.longitude} : amsl : {self.aboveMeanSeaLevel}'
42 class GeoLocation(IGeoLocation):
43 _equatorialRadius = 6378137 # meters
44 _polarRadius = 6356752 # meters
46 def __init__(self, geoLocation: IGeoLocationData = None):
48 geoLocation['latitude'] if geoLocation else 0,
49 geoLocation['longitude'] if geoLocation else 0,
50 geoLocation['aboveMeanSeaLevel'] if geoLocation else 0
54 def equatorialRadius(self) -> int:
55 return GeoLocation._equatorialRadius
58 def polarRadius(self)-> int:
59 return GeoLocation._polarRadius
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.')
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
71 def json(self) -> dict[str, float]:
73 "latitude": self.latitude,
74 "longitude": self.longitude,
75 "aboveMeanSeaLevel": self.aboveMeanSeaLevel,
78 def __str__(self) -> str:
79 return str(self.json())
81 def point_to_geo_location(self, point:Point):
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.
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)
92 geo_location: IGeoLocationData = {
95 "aboveMeanSeaLevel": self.aboveMeanSeaLevel
97 return GeoLocation(geo_location)