-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspatialfunclib.py
287 lines (228 loc) · 9.79 KB
/
spatialfunclib.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#
# Spatial function library.
# Author: James P. Biagioni (jbiagi1@uic.edu)
# Company: University of Illinois at Chicago
# Created: 3/3/10
#
# Author: Mahmuda Ahmed
# Company: The University of Texas at San Antonio
# Modified: 2013
#
# Author: Jorren Hendriks (j.a.m.hendriks@student.tue.nl)
# Institute: Eindhoven University of Technology (TU/e)
# Modified: 30/4/2020
#
# Author: Erfan Hosseini Sereshgi (shosseinisereshgi@tulane.edu)
# Company: Tulane University
# Modified: 6/10/2021
#
# Author: Jordi Aguilar Larruy (jordi.aguilar.larruy@estudiantat.upc.edu)
# Company: Universitat Politècnica de Catalunya
# Modified: 5/31/2021
#
import math
#
# Global constants.
#
METERS_PER_DEGREE_LATITUDE = 111070.34306591158
METERS_PER_DEGREE_LONGITUDE = 83044.98918812413
EARTH_RADIUS = 6371000.0 # meters
#
# Returns the distance in meters between two points specified in degrees, using the default (Haversine formula) method.
#
def distance(a_lat, a_lon, b_lat, b_lon):
return haversine_distance(a_lat, a_lon, b_lat, b_lon)
#
# Returns the euclidean distance in meters between two points.
#
def euclideandistance(a_x, a_y, b_x, b_y):
return math.sqrt(math.pow(float(a_x)-float(b_x),2)+math.pow(float(a_y)-float(b_y),2))
#
# Returns the great-circle difference in bearing.
#
def bearing_difference(a_bearing, b_bearing):
max_bearing = max(a_bearing, b_bearing)
min_bearing = min(a_bearing, b_bearing)
return min(abs(max_bearing - min_bearing), abs((360.0 - max_bearing) + min_bearing), abs(180.0 - (max_bearing - min_bearing)))
#
# Returns whether the coordinates for two points A and B are the same.
#
def same_coords(a_lat, a_lon, b_lat, b_lon):
if (a_lat == b_lat and a_lon == b_lon):
return True
else:
return False
#
# Returns the distance in meters between two points specified in degrees, using the Haversine formula. Formula adapted from: http://www.movable-type.co.uk/scripts/latlong.html
#
def haversine_distance(a_lat, a_lon, b_lat, b_lon):
if (same_coords(a_lat, a_lon, b_lat, b_lon)):
return 0.0
dLat = math.radians(b_lat - a_lat)
dLon = math.radians(b_lon - a_lon)
a = math.sin(dLat/2.0) * math.sin(dLat/2.0) + math.cos(math.radians(a_lat)) * math.cos(math.radians(b_lat)) * math.sin(dLon/2.0) * math.sin(dLon/2.0)
c = 2.0 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
d = EARTH_RADIUS * c
return d
#
# Returns the distance in meters between two points specified in degrees, using the Spherical Law of Cosines. Formula adapted from: http://www.movable-type.co.uk/scripts/latlong.html
#
def slc_distance(a_lat, a_lon, b_lat, b_lon):
if (same_coords(a_lat, a_lon, b_lat, b_lon)):
return 0.0
a_lat = math.radians(a_lat)
a_lon = math.radians(a_lon)
b_lat = math.radians(b_lat)
b_lon = math.radians(b_lon)
a = math.sin(a_lat) * math.sin(b_lat) + math.cos(a_lat) * math.cos(b_lat) * math.cos(b_lon - a_lon)
d = math.acos(a) * EARTH_RADIUS
return d
#
# Returns the distance in meters between two points specified in degrees, using an approximation method.
#
def fast_distance(a_lat, a_lon, b_lat, b_lon):
if (same_coords(a_lat, a_lon, b_lat, b_lon)):
return 0.0
y_dist = METERS_PER_DEGREE_LATITUDE * (a_lat - b_lat)
x_dist = METERS_PER_DEGREE_LONGITUDE * (a_lon - b_lon)
return math.sqrt((y_dist * y_dist) + (x_dist * x_dist))
#
# Returns the path bearing between two points specified in degrees. Formula adapted from: http://www.movable-type.co.uk/scripts/latlong.html
#
def path_bearing(a_lat, a_lon, b_lat, b_lon):
a_lat = math.radians(a_lat)
a_lon = math.radians(a_lon)
b_lat = math.radians(b_lat)
b_lon = math.radians(b_lon)
y = math.sin(b_lon-a_lon) * math.cos(b_lat)
x = math.cos(a_lat) * math.sin(b_lat) - math.sin(a_lat) * math.cos(b_lat) * math.cos(b_lon-a_lon)
bearing = math.atan2(y, x)
return math.fmod(math.degrees(bearing) + 360.0, 360.0)
#
# Returns the path bearing between two points specified in meters.
#
def path_bearing_meters(a_x, a_y, b_x, b_y):
ydiff = float(b_y)-float(a_y)
xdiff = float(b_x)-float(a_x)
## hypotenuse = math.sqrt(math.pow(ydiff,2)+math.pow(xdiff,2))
## if (hypotenuse != 0):
## bearing = math.acos(xdiff/hypotenuse)
## else:
## bearing = 0
bearing = math.atan2(ydiff, xdiff)
return math.fmod(math.degrees(bearing) + 360.0, 360.0)
#
# Returns the destination point given distance and bearing from a start point. Formula adapted from: http://www.movable-type.co.uk/scripts/latlong.html
#
def destination_point(a_lat, a_lon, bearing, distance):
angular_distance = (distance/6371000.0) # meters
bearing = math.radians(bearing)
a_lat = math.radians(a_lat)
a_lon = math.radians(a_lon)
b_lat = math.asin(math.sin(a_lat) * math.cos(angular_distance) + math.cos(a_lat) * math.sin(angular_distance) * math.cos(bearing))
b_lon = a_lon + math.atan2(math.sin(bearing) * math.sin(angular_distance) * math.cos(a_lat), math.cos(angular_distance) - math.sin(a_lat) * math.sin(b_lat))
b_lon = math.fmod((b_lon + (3 * math.pi)), (2 * math.pi)) - math.pi
return (math.degrees(b_lat), math.degrees(b_lon))
#
# Returns the intersection point of two paths given start points and bearings. Formula adapted from: http://www.movable-type.co.uk/scripts/latlong.html
#
def intersection_point(a_lat, a_lon, a_bearing, b_lat, b_lon, b_bearing, debug=False):
a_lat = math.radians(a_lat)
a_lon = math.radians(a_lon)
b_lat = math.radians(b_lat)
b_lon = math.radians(b_lon)
brng13 = math.radians(a_bearing)
brng23 = math.radians(b_bearing)
dLat = (b_lat - a_lat)
dLon = (b_lon - a_lon)
dist12 = 2.0 * math.asin(math.sqrt(math.sin(dLat/2.0) * math.sin(dLat/2.0) + math.cos(a_lat) * math.cos(b_lat) * math.sin(dLon/2.0) * math.sin(dLon/2.0)))
if (dist12 == 0.0):
if (debug):
print("Intersection ERROR: points are too close!")
return None
# initial/final bearings between points
try:
brngA = math.acos((math.sin(b_lat) - math.sin(a_lat) * math.cos(dist12)) / (math.sin(dist12) * math.cos(a_lat)))
except Exception as inst:
if (debug):
print("Intersection EXCEPTION: " + str(inst))
return None
# protect against rounding
if (math.isnan(brngA)):
brngA = 0.0
try:
brngB = math.acos((math.sin(a_lat) - math.sin(b_lat) * math.cos(dist12)) / (math.sin(dist12) * math.cos(b_lat)))
except Exception as inst:
if (debug):
print("Intersection EXCEPTION: " + str(inst))
return None
if (math.sin(b_lon - a_lon) > 0):
brng12 = brngA
brng21 = 2.0 * math.pi - brngB
else:
brng12 = 2.0 * math.pi - brngA
brng21 = brngB
alpha1 = (brng13 - brng12 + math.pi) % (2.0 * math.pi) - math.pi
alpha2 = (brng21 - brng23 + math.pi) % (2.0 * math.pi) - math.pi
# infinite intersections
if ((math.sin(alpha1) == 0.0) and (math.sin(alpha2) == 0.0)):
if (debug):
print("Intersection ERROR: infinite intersections!")
return None
# ambiguous intersection
if ((math.sin(alpha1) * math.sin(alpha2)) < 0.0):
if (debug):
print("Intersection ERROR: ambiguous intersection!")
return None
alpha3 = math.acos (-1.0 * math.cos(alpha1) * math.cos(alpha2) + math.sin(alpha1) * math.sin(alpha2) * math.cos(dist12))
dist13 = math.atan2(math.sin(dist12) * math.sin(alpha1) * math.sin(alpha2), math.cos(alpha2) + math.cos(alpha1) * math.cos(alpha3))
c_lat = math.asin(math.sin(a_lat) * math.cos(dist13) + math.cos(a_lat) * math.sin(dist13) * math.cos(brng13))
dLon13 = math.atan2(math.sin(brng13) * math.sin(dist13) * math.cos(a_lat), math.cos(dist13) - math.sin(a_lat) * math.sin(c_lat))
c_lon = a_lon + dLon13
c_lon = math.fmod((c_lon + math.pi), (2.0 * math.pi)) - math.pi
return (math.degrees(c_lat), math.degrees(c_lon))
#
# Returns the coordinates of a point some "fraction_along" the line AB.
#
def point_along_line(a_lat, a_lon, b_lat, b_lon, fraction_along):
c_lon = a_lon + (fraction_along * (b_lon - a_lon))
c_lat = a_lat + (fraction_along * (b_lat - a_lat))
return (c_lat, c_lon)
#
# Returns the orthogonal projection of point C onto line AB, and its fraction along line AB.
#
def projection_onto_line(a_lat, a_lon, b_lat, b_lon, c_lat, c_lon, debug=False):
ab_angle = path_bearing_meters(a_lat, a_lon, b_lat, b_lon)
if (debug):
print("ab_angle: " + str(ab_angle) + " degrees")
ac_angle = path_bearing_meters(a_lat, a_lon, c_lat, c_lon)
if (debug):
print("ac_angle: " + str(ac_angle) + " degrees")
ab_length = euclideandistance(a_lat, a_lon, b_lat, b_lon)
if (debug):
print("ab_length: " + str(ab_length) + " meters")
ac_length = euclideandistance(a_lat, a_lon, c_lat, c_lon)
if (debug):
print("ac_length: " + str(ac_length) + " meters")
angle_diff = (ac_angle - ab_angle)
if (debug):
print("angle_diff: " + str(angle_diff) + " degrees")
meters_along = (ac_length * math.cos(math.radians(angle_diff)))
if (debug):
print("meters_along: " + str(meters_along) + " meters")
if (ab_length == 0.0):
fraction_along = 0.0
else:
fraction_along = (meters_along / ab_length)
if (debug):
print("fraction_along: " + str(fraction_along))
projected_point = point_along_line(a_lat, a_lon, b_lat, b_lon, fraction_along)
if (debug):
print("projected_point: " + str(projected_point))
cproj_length = euclideandistance(c_lat, c_lon, projected_point[0], projected_point[1])
if (debug):
print("cproj_length: " + str(cproj_length) + " meters")
cproj_angle = path_bearing_meters(c_lat, c_lon, projected_point[0], projected_point[1])
if (debug):
print("cproj_angle: " + str(cproj_angle) + " degrees")
return (projected_point, fraction_along, cproj_length)