-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
150 lines (125 loc) · 4.64 KB
/
helper.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
import math
import heapq
import numpy as np
from math import pi
import igraph as ig
from global_land_mask import globe
def shortest_path(g, src, target):
"""
Implementation of Dijkstra's Algorithm
"""
q = [(0, src, ())]
visited, dist = set(), {src: 0.0}
while q:
cost, v, path = heapq.heappop(q)
print(v, target)
print("distance left: ", math.sqrt((v[0] - target[0])*(v[0] - target[0]) + (v[1] - target[1])*(v[1] - target[1])))
if v not in visited:
visited.add(v)
path += (v,)
if v == target:
return (cost, path)
for cost2, v2 in g.get(v, ()):
if v2 in visited:
continue
if cost + cost2 < dist.get(v2, float('inf')):
dist[v2] = cost + cost2
heapq.heappush(q, (cost + cost2, v2, path))
return (float('inf'), ())
def create_graph(x, y):
"""
Creates Graph with Ocean (lon, lat) as nodes and edges with neighbouring ocean nodes
"""
edges = []
for i in range(len(x)):
for j in range(len(y)):
if globe.is_land(y[j], x[i]):
continue
center = get_node_index(i, j, len(y))
if not globe.is_land(y[j], x[(i - 1 + len(x))%len(x)]):
top = get_node_index((i-1+len(x)) % len(x), j, len(y))
edges.append((center, top))
if not globe.is_land(y[j], x[(i+1)%len(x)]):
bottom = get_node_index((i+1)%len(x), j, len(y))
edges.append((center, bottom))
if not globe.is_land(y[(j+1)%len(y)], x[i]):
right = get_node_index(i, (j+1)%len(y), len(y))
edges.append((center, right))
if not globe.is_land(y[(j-1+len(y))%len(y)], x[i]):
left = get_node_index(i, (j-1+len(y))%len(y), len(y))
edges.append((center, left))
if not globe.is_land(y[(j+1)%len(y)], x[(i-1+len(x)) % len(x)]):
top_right = get_node_index((i-1+len(x)) % len(x), (j+1)%len(y), len(y))
edges.append((center, top_right))
if not globe.is_land(y[(j-1+len(y))%len(y)], x[(i-1+len(x)) % len(x)]):
top_left = get_node_index((i-1+len(x)) % len(x), (j-1+len(y))%len(y), len(y))
edges.append((center, top_left))
if not globe.is_land(y[(j+1)%len(y)], x[(i+1) % len(x)]):
bottom_right = get_node_index((i+1) % len(x), (j+1)%len(y), len(y))
edges.append((center, bottom_right))
if not globe.is_land(y[(j-1+len(y))%len(y)], x[(i+1) % len(x)]):
bottom_left = get_node_index((i+1) % len(x), (j-1+len(y))%len(y), len(y))
edges.append((center, bottom_left))
G = ig.Graph(len(x) * len(y), edges)
return G
def calculate_cost(X, Y, U, V, v1, v2, s0):
"""
Calculates time taken by vessel to travel a distance considering ocean currents
"""
j1, i1 = v1
j2, i2 = v2
u = (U[j1,i1] + U[j2,i2])/2.
v = (V[j1,i1] + V[j2,i2])/2.
ds = distance(Y[v1], X[v1], Y[v2], X[v2])
a = bearing(Y[v1], X[v1], Y[v2], X[v2])
# Velocity along track
s = s0 + u*np.cos(a) + v*np.sin(a)
if s < 0:
return np.inf
else:
return ds/s
def distance(lat1, lon1, lat2, lon2):
"""
Calculate the Great-circle distance
"""
# http://www.movable-type.co.uk/scripts/latlong.html
R = 6.371e6
lat1 *= pi/180.
lon1 *= pi/180.
lat2 *= pi/180.
lon2 *= pi/180.
return R*np.arccos(
np.sin(lat1)*np.sin(lat2) +
np.cos(lat1)*np.cos(lat2)*np.cos(lon2-lon1))
def bearing(lat1, lon1, lat2, lon2):
"""
Calculates Bearing (angle)
"""
lat1 *= pi/180.
lon1 *= pi/180.
lat2 *= pi/180.
lon2 *= pi/180.
y = np.sin(lon2-lon1)*np.cos(lat2)
x = np.cos(lat1)*np.sin(lat2) - np.sin(lat1)*np.cos(lat2)*np.cos(lon2-lon1)
return (pi/2) - np.arctan2(y, x)
def get_node_index(i, j, len_y):
"""
2D Index -> 1D Index
"""
return i * len_y + j
def get_coord(index, len_y):
"""
1D Index -> 2D Index
"""
return (index // len_y, index % len_y)
def get_index_from_lat_long(x, y, coord):
"""
coord: list
(latitude, longitude)
"""
return (np.absolute(y - coord[0]).argmin(), np.absolute(x - coord[1]).argmin())
def get_distance(v1, v2):
"""
Calculate the Great-circle distance
"""
return (np.linalg.norm(v1-v2))