-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeographic_calculations.py
214 lines (140 loc) · 6.16 KB
/
geographic_calculations.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
#! /usr/bin/env python
'''#######################################################################################
# geographic_calculations.py
#
# This file contains functions that implement calculations on geographic information.
#
# NOTE: Any plotting is set up for output, not viewing on screen.
# So, it will likely be ugly on screen. The saved PDFs should look
# better.
#
# Created: 04/23/14
# - Joshua Vaughan
# - http://www.ucs.louisiana.edu/~jev9637
#
# Modified:
# *
#
#######################################################################################'''
import numpy as np
def calculate_distance(position1, position2):
''' Calculate the distance between two lat/long coordinates using a unit sphere
Copied from: John Cook at http://www.johndcook.com/python_longitude_latitude.html
Input arguments:
position1 = lat/long pair in decimal degrees DD.dddddd
position2 = lat/long pair in decimal degrees DD.dddddd
Returns:
distance = distance from position 1 to position 2 in meters
Modified:
*Joshua Vaughan - [email protected] - 04/23/14
- Additional commenting
- Modified to match "theme" of CRAWLAB
- Inputs change to long/lat array slices
'''
lat1, long1 = position1
lat2, long2 = position2
R = 6373000 # Radius of the earth in m
# Convert latitude and longitude to spherical coordinates in radians.
degrees_to_radians = np.pi/180.0
# phi = 90 - latitude
phi1 = np.deg2rad(90.0 - lat1)
phi2 = np.deg2rad(90.0 - lat2)
# theta = longitude
theta1 = np.deg2rad(long1)
theta2 = np.deg2rad(long2)
# Compute spherical distance from spherical coordinates.
# For two locations in spherical coordinates
# (1, theta, phi) and (1, theta, phi)
# cosine( arc length ) =
# sin phi sin phi' cos(theta-theta') + cos phi cos phi'
# distance = rho * arc length
cos = (np.sin(phi1) * np.sin(phi2) * np.cos(theta1 - theta2) +
np.cos(phi1) * np.cos(phi2))
arc = np.arccos( cos )
# Multiply arc by the radius of the earth
distance = arc * R
return distance
def calculate_simple_distance(position1, position2):
''' Calculate the distance between two lat/long coords using simple cartesian math
Equation from: http://www.movable-type.co.uk/scripts/latlong.html
Input arguments:
position1 = lat/long pair in decimal degrees DD.dddddd
position2 = lat/long pair in decimal degrees DD.dddddd
Returns:
distance = distance from position 1 to position 2 in meters
Created: Joshua Vaughan - [email protected] - 04/24/14
Modified:
*
'''
R = 6373000 # Radius of the earth in m
lat1, long1 = np.deg2rad(position1)
lat2, long2 = np.deg2rad(position2)
dLat = lat2 - lat1
dLon = long2 - long1
x = dLon * np.cos((lat1+lat2)/2)
distance = np.sqrt(x**2 + dLat**2) * R
return distance
def calculate_bearing(position1, position2):
''' Calculate the bearing between two GPS coordinates
Equations from: http://www.movable-type.co.uk/scripts/latlong.html
Input arguments:
position1 = lat/long pair in decimal degrees DD.dddddd
position2 = lat/long pair in decimal degrees DD.dddddd
Returns:
bearing = initial bearing from position 1 to position 2 in degrees
Created: Joshua Vaughan - [email protected] - 04/23/14
Modified:
*
'''
lat1, long1 = np.deg2rad(position1)
lat2, long2 = np.deg2rad(position2)
dLon = long2 - long1
y = np.sin(dLon) * np.cos(lat2)
x = np.cos(lat1)*np.sin(lat2) - np.sin(lat1)*np.cos(lat2)*np.cos(dLon)
#bearing = np.rad2deg(np.arctan2(y, x))
bearing = (np.rad2deg(np.arctan2(y, x)) + 360) % 360
return bearing
def calculate_midpoint(position1, position2):
''' Calculate the midpoint between two GPS coordinates
Equations from: http://www.movable-type.co.uk/scripts/latlong.html
Input arguments:
position1 = lat/long pair in decimal degrees DD.dddddd
position2 = lat/long pair in decimal degrees DD.dddddd
Returns:
midpoint = lat/long pair in decimal degrees DD.dddddd
Created: Joshua Vaughan - [email protected] - 04/23/14
Modified:
*
'''
lat1, long1 = np.deg2rad(position1)
lat2, long2 = np.deg2rad(position2)
dLat = lat2 - lat1
dLon = long2 - long1
Bx = np.cos(lat2) * np.cos(dLon)
By = np.cos(lat2) * np.sin(dLon)
midpoint_lat = np.arctan2(np.sin(lat1) + np.sin(lat2),
np.sqrt( (np.cos(lat1) + Bx) * (np.cos(lat1) +Bx ) + By*By ) )
midpoint_long = long1 + np.arctan2(By, np.cos(lat1) + Bx)
return np.rad2deg([midpoint_lat, midpoint_long])
def define_destination(start_position, bearing, distance):
''' Calculate the endpoint given a GPS coordinate, heading, and desired distance
Equations from: http://www.movable-type.co.uk/scripts/latlong.html
Inputs arguments:
start_position = GPS lat/long pair in decimal degrees DD.ddddddd
bearing = start bearing (deg)
distance = how far to go (m)
Returns:
end_position = GPS lat/long pair of endpoint in decimal degrees DD.ddddddd
Created: Joshua Vaughan - [email protected] - 04/23/14
Modified:
*
'''
R = 6373000.0 # Radius of the earth in m
start_lat, start_long = np.deg2rad(start_position)
bearing = np.deg2rad(bearing)
end_lat = np.arcsin(np.sin(start_lat)*np.cos(distance/R) +
np.cos(start_lat)*np.sin(distance/R)*np.cos(bearing))
end_long = start_long + np.arctan2(np.sin(bearing)*np.sin(distance/R)*np.cos(start_lat),
np.cos(distance/R)-np.sin(start_lat)*np.sin(end_lat))
return np.rad2deg([end_lat, end_long])