-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.py
93 lines (79 loc) · 2.9 KB
/
point.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import geopy
import math
from geopy.distance import great_circle
import uuid
class Point(geopy.point.Point):
@classmethod
def from_json(cls, d, self=None):
if self == None:
self = cls.__new__(cls)
self = Point(
longitude = d["lon"],
latitude = d["lat"],
altitude = d["alt"]
)
return self
def to_json(self):
return {
"lon": self.longitude,
"lat": self.latitude,
"alt": self.altitude
}
def distance_to(self, p2):
return great_circle(self, p2).meters
def perp(self, p2, dist, duuid):
b = self.bearing_to_point(p2)
h = 1.0 / float(
int.from_bytes(
b"\xff" * 16,
byteorder='little'
) / int.from_bytes(
uuid.UUID(duuid).bytes_le,
byteorder='little'
)
)
b += 180 + ((h * 180) - 90)
b %= 360
return self.point_at_vector(100 - (dist * 10), b)
def bearing_to_point(self, target_location):
# algorithm from https://gist.github.com/jeromer/2005586
lat1 = math.radians(self.latitude)
lat2 = math.radians(target_location.latitude)
diffLong = math.radians(target_location.longitude - self.longitude)
x = math.sin(diffLong) * math.cos(lat2)
y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1) * math.cos(lat2) * math.cos(diffLong))
initial_bearing = math.atan2(x, y)
initial_bearing = math.degrees(initial_bearing)
bearing = (initial_bearing + 360) % 360
return bearing
def point_at_vector(self, distance, bearing):
if distance == 0:
return Point(self)
else:
point = Point(great_circle(meters=distance).destination(self, bearing))
point.altitude = self.altitude
return point
def point_at_xy_distance(self, x_dist, y_dist):
lat = great_circle(meters=y_dist).destination(self, 0).latitude if y_dist > 0 else self.latitude
lon = great_circle(meters=x_dist).destination(self, 90).longitude if x_dist > 0 else self.longitude
return Point(latitude = lat, longitude = lon, altitude = self.altitude)
def simple_string(self):
return str(self.latitude) + " " + str(self.longitude) + " " + str(self.altitude)
class Space:
def __init__(self, bottom_left, top_right):
self.bottom_left = bottom_left
self.top_right = top_right
def to_json(self):
return {
"bottom_left": self.bottom_left.to_json(),
"top_right": self.top_right.to_json()
}
@classmethod
def from_json(cls, d, self=None):
if self is None:
self = cls.__new__(cls)
self = Space(
bottom_left = Point.from_json(d["bottom_left"]),
top_right = Point.from_json(d["top_right"])
)
return self