Newer
Older
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# imports
# standard
from math import isclose, inf
# local, external
import networkx as nx
import osmnx as ox
from shapely.geometry import LineString
from numpy import nan
# local, internal
import src.topupopt.data.gis.calculate as gis_calc
import src.topupopt.data.gis.osm as osm
# *****************************************************************************
# *****************************************************************************
class TestGisCalculate:
# *************************************************************************
# *************************************************************************
def validate_edge_distances(self, G: nx.MultiDiGraph, abs_tol: float = 5):
# get the true edge lengths
true_lengths = {
edge_key: (
G.edges[edge_key][osm.KEY_OSMNX_LENGTH]
if osm.KEY_OSMNX_LENGTH in G.edges[edge_key]
else None
)
for edge_key in G.edges(keys=True)
}
# get the edge lengths calculated independently
calculated_lengths = gis_calc.edge_lengths(G)
# for each edge on the graph
for edge_key in true_lengths.keys():
# validate
assert isclose(
calculated_lengths[edge_key], true_lengths[edge_key], abs_tol=abs_tol
)
# *************************************************************************
# *************************************************************************
def test_distances(self):
# get a graph
G = ox.graph_from_point(
(55.71654, 9.11728),
network_type="drive",
custom_filter='["highway"~"residential|tertiary|unclassified|service"]',
truncate_by_edge=True,
)
# count occurrences
self.example_count_occurrences(G)
# validate without projected coordinates
self.validate_edge_distances(G=G)
# project the graph
projected_G = ox.project_graph(G=G)
# validate with projected coordinates
self.validate_edge_distances(G=projected_G)
# calculate node path lenths
self.example_node_path_lengths()
# calculate edge path lengths
self.example_edge_path_lengths()
# *************************************************************************
# *************************************************************************
def example_node_path_lengths(self):
# identify path between two nodes
G = nx.MultiDiGraph()
G.add_edge(0, 1, length=10)
G.add_edge(1, 2, length=10)
G.add_edge(1, 2, length=5)
G.add_edge(2, 3, length=5)
# calculate length
path = [0, 1, 2, 3]
this_length = gis_calc.node_path_length(network=G, path=path)
assert this_length == 20
# calculate all possible lengths
these_lengths = gis_calc.node_path_length(
network=G, path=path, return_minimum_length_only=False
)
true_lengths = [25, 20]
assert len(these_lengths) == len(true_lengths)
for _length in these_lengths:
assert _length in true_lengths
# *********************************************************************
# *********************************************************************
# no path
path = [0, 3]
this_length = gis_calc.node_path_length(network=G, path=path)
assert this_length == inf
these_lengths = gis_calc.node_path_length(
network=G, path=path, return_minimum_length_only=False
)
assert these_lengths[0] == inf
# empty path
path = []
this_length = gis_calc.node_path_length(network=G, path=path)
assert this_length == inf
# *********************************************************************
# *********************************************************************
# *************************************************************************
# *************************************************************************
def example_edge_path_lengths(self):
# identify path between two nodes
G = nx.MultiDiGraph()
G.add_edge(0, 1, length=10)
G.add_edge(1, 2, length=10)
G.add_edge(1, 2, length=5)
G.add_edge(2, 3, length=5)
# calculate length
path = [(0, 1, 0), (1, 2, 0), (2, 3, 0)]
this_length = gis_calc.edge_path_length(network=G, path=path)
assert this_length == 25
# alternative path
path = [(0, 1, 0), (1, 2, 1), (2, 3, 0)]
this_length = gis_calc.edge_path_length(network=G, path=path)
assert this_length == 20
# *********************************************************************
# *********************************************************************
# no path
path = [(0, 1, 0), (1, 3, 0)]
this_length = gis_calc.edge_path_length(network=G, path=path)
assert this_length == inf
path = []
this_length = gis_calc.edge_path_length(network=G, path=path)
assert this_length == inf
# *********************************************************************
# *********************************************************************
# *************************************************************************
# *************************************************************************
def example_count_occurrences(self, G: nx.MultiDiGraph):
# get a gdf from the graph via osmnx
gdf = ox.utils_graph.graph_to_gdfs(G, nodes=False)
# define the column (street)
column = "name"
# count the ocurrences
count_dict = gis_calc.count_ocurrences(gdf, column=column)
# expected result
true_count_dict = {
nan: 249,
"Havremarken": 8,
"Kornmarken": 34,
"Kløvervej": 38,
"Kærvej": 52,
"Plougslundvej": 24,
"Egevænget": 16,
"Fyrrevænget": 6,
"Kærhusvej": 12,
"Kløvermarken": 52,
"Grønningen": 38,
"Bygager": 10,
"Fælleden": 6,
"Flintemarken": 52,
"Stendyssen": 8,
"Markskellet": 54,
"Engdraget": 20,
"Vestervang": 36,
"Tingstedet": 87,
"Tuen": 10,
"Lillevang": 96,
"Grenevej": 24,
"Hedegårdsvej": 16,
"Gravhøjen": 28,
"Lysningen": 37,
"Ved Søen": 20,
"Bopladsen": 10,
"Koldingvej": 14,
"Bakkelien": 38,
}
# test
for key, value in count_dict.items():
assert value == true_count_dict[key]
# *********************************************************************
# *********************************************************************
# count only a few column entries
count_dict = gis_calc.count_ocurrences(
gdf, column=column, column_entries=["Kløvermarken", "Grenevej", "Bopladsen"]
)
# test
for key, value in count_dict.items():
assert value == true_count_dict[key]
# *********************************************************************
# *********************************************************************
# count the nans
count_dict = gis_calc.count_ocurrences(gdf, column=column, column_entries=[nan])
# test
for key, value in count_dict.items():
assert value == true_count_dict[key]
# *********************************************************************
# *********************************************************************
# *************************************************************************
# *************************************************************************
def test_compute_great_circle_distance_linestring(self):
# Source: https://keisan.casio.com/exec/system/1224587128
length_tolerance = 1 # meters
# dict_A = {'x': 12.2430512, 'y': 55.6822924}
# dict_B = {'x': 12.242863, 'y': 55.682155}
# dict_C = {'x': 12.252233, 'y': 55.68085}
list_1_points = [(12, 56)]
# 1º longitude at a latitude of 56º
list_2_points_a = [(12, 56), (13, 56)]
# 1º latituide at a longitude of 13º
list_2_points_b = [(13, 56), (13, 57)]
# 1º longitude at a latitude of 56º + 1º latituide at a longitude of 13º
list_3_points = [(12, 56), (13, 56), (13, 57)]
# radius = 6363.478 km at sea level
# edge = radius*(pi/180)*angle in degrees
true_length_2_points_a = 62.178959 * 1e3 # 62.178959 km with r=6371.009 km
true_length_2_points_b = 111.195084 * 1e3 # 111.195084 km with r=6371.009 km
true_length_3_points = true_length_2_points_a + true_length_2_points_b
# make sure the function fails with a single point (sanity check)
Pedro L. Magalhães
committed
error_raised = False
try:
line = LineString(list_1_points)
except Exception:
Pedro L. Magalhães
committed
error_raised = True
assert error_raised
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# make sure it works with 2 points
line = LineString(list_2_points_a)
_length = gis_calc.great_circle_distance_along_path(line)
assert isclose(_length, true_length_2_points_a, abs_tol=length_tolerance)
line = LineString(list_2_points_b)
_length = gis_calc.great_circle_distance_along_path(line)
assert isclose(_length, true_length_2_points_b, abs_tol=length_tolerance)
# make sure it works with 3 points
line = LineString(list_3_points)
_length = gis_calc.great_circle_distance_along_path(line)
assert isclose(_length, true_length_3_points, abs_tol=length_tolerance)
# *************************************************************************
# *************************************************************************
# *****************************************************************************
# *****************************************************************************