This repository was archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_distance.py
132 lines (102 loc) · 3.76 KB
/
calculate_distance.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
"""
Earth radius is defined in meters, thus all results will be in meters.
"""
import math
EARTHRADIUS = 6371000
def calc_latlong_distance(coordinates1, coordinates2):
"""
#### Input:
- Start Coordinate pairs [Latitude, Longitude]
- End Coordinate pairs [Latitude, Longitude]
#### Output:
- Distance in meters
#### Desc:
[Found the equation in the link below. Ported from Java to Python.](https://stackoverflow.com/questions/837872/calculate-distance-in-meters-when-you-know-longitude-and-latitude-in-java)
"""
lat1 = coordinates1[0]
lng1 = coordinates1[1]
lat2 = coordinates2[0]
lng2 = coordinates2[1]
dLat = math.radians(lat2 - lat1)
dLng = math.radians(lng2 - lng1)
area = (
math.sin(dLat / 2) * math.sin(dLat / 2) +
math.cos(math.radians(lat1)) *
math.cos(math.radians(lat2)) *
math.sin(dLng / 2) * math.sin(dLng / 2)
)
circumf = 2 * math.atan2(math.sqrt(area), math.sqrt(1 - area))
distance = EARTHRADIUS * circumf
return distance
# Definately spent way too much time on trying to work the equation backwards.
# Decided to just look up a solution and work it into the project.
def _findCoor_by_distAndBearing(anchor_lat, anchor_long, distance, degree_idx):
"""
#### Input:
- Starting Latitude Coordinate
- Starting Longitude Coordinate
- Distance in meters
- Degree Index (degrees of a circle)
#### Output:
- [Latitude, Longitude] of end point.
#### Desc:
[Found the equation on StackOverflow](https://stackoverflow.com/questions/7222382/get-lat-long-given-current-point-distance-and-bearing)
Was relearning how spherical circumference calculations worked, spent way
too much time (a few hours) trying to find missing variables using
the function above. Decided to search for an implementation instead.
Degrees bearing used to determine the midpoints of all sides of a square.
This helps set the initial search zone for nearby rentals.
If we implement an octagon, should be a small zone to search. Would do it
here.
"""
degree_bearing = {
0: 0,
90: 1.57,
180: 3.14,
240: 4.18
}
bearing = degree_bearing[degree_idx]
# convert to radians
radians_lat = math.radians(anchor_lat)
radians_long = math.radians(anchor_long)
dist_div_earthrad = distance / EARTHRADIUS
result_lat = (
math.asin(
math.sin(radians_lat) * math.cos(dist_div_earthrad) +
math.cos(radians_lat) * math.sin(dist_div_earthrad) *
math.cos(bearing)))
result_long = (
radians_long + math.atan2(
(math.sin(bearing) * math.sin(dist_div_earthrad) *
math.cos(radians_lat)),
(math.cos(dist_div_earthrad) - math.sin(radians_lat) *
math.sin(result_lat))
)
)
result_lat = math.degrees(result_lat)
result_long = math.degrees(result_long)
return result_lat, result_long
"""
Hardcoded functions to search for endpoint. to calculate the search range.
Bearings denote certain results:
- 0 as max latitude
- 90 as max longitude
- 180 as min latitude
- 240 as min longitude
"""
def find_max_latitude(anchor_lat, anchor_long, distance):
return _findCoor_by_distAndBearing(
anchor_lat, anchor_long, distance, 0
)[0]
def find_max_longitude(anchor_lat, anchor_long, distance):
return _findCoor_by_distAndBearing(
anchor_lat, anchor_long, distance, 90
)[1]
def find_min_latitude(anchor_lat, anchor_long, distance):
return _findCoor_by_distAndBearing(
anchor_lat, anchor_long, distance, 180
)[0]
def find_min_longitude(anchor_lat, anchor_long, distance):
return _findCoor_by_distAndBearing(
anchor_lat, anchor_long, distance, 240
)[1]