-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
box.py
59 lines (45 loc) · 1.87 KB
/
box.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from typing import NamedTuple, Self
from latlon import LatLon
from utils import meters_to_lat, meters_to_lon
class Box(NamedTuple):
point: LatLon
size: LatLon
def __str__(self) -> str:
return f'{self.point},{self.point + self.size}'
def __contains__(self, point: LatLon) -> bool:
return (
self.point.lat <= point.lat <= self.point.lat + self.size.lat and
self.point.lon <= point.lon <= self.point.lon + self.size.lon
)
def extend(self, *, meters: float = None, scale: float = None) -> Self:
if meters is not None:
extend_lat = meters_to_lat(meters)
extend_lon = meters_to_lon(meters, self.point.lat)
elif scale is not None:
extend_lat = self.size.lat * scale
extend_lon = self.size.lon * scale
else:
raise ValueError('No extend unit specified')
return Box(
point=LatLon(self.point.lat - extend_lat, self.point.lon - extend_lon),
size=LatLon(self.size.lat + extend_lat * 2, self.size.lon + extend_lon * 2))
def squarify(self) -> Self:
# calculate the center of the bounding box
center_lat = self.point.lat + self.size.lat / 2
center_lon = self.point.lon + self.size.lon / 2
density_y = meters_to_lat(0.1)
density_x = meters_to_lon(0.1, center_lat)
d_lat = self.size.lat
d_lon = self.size.lon
height = d_lat / density_y
width = d_lon / density_x
ratio = width / height
if ratio > 1:
d_lat = d_lat * ratio
else:
d_lon = d_lon / ratio
return Box(
point=LatLon(center_lat - d_lat / 2, center_lon - d_lon / 2),
size=LatLon(d_lat, d_lon))
def center(self) -> LatLon:
return LatLon(self.point.lat + self.size.lat / 2, self.point.lon + self.size.lon / 2)