Newer
Older
# 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:
# *************************************************************************
# *************************************************************************
Pedro L. Magalhães
committed
def validate_edge_distances(self, G: nx.MultiDiGraph, abs_tol: float = 5):
# get the true edge lengths
edge_key: (
G.edges[edge_key][osm.KEY_OSMNX_LENGTH]
if osm.KEY_OSMNX_LENGTH in G.edges[edge_key]
else None
)
Pedro L. Magalhães
committed
for edge_key in G.edges(keys=True)
Pedro L. Magalhães
committed
# get the edge lengths calculated independently
calculated_lengths = gis_calc.edge_lengths(G)
Pedro L. Magalhães
committed
# for each edge on the graph
calculated_lengths[edge_key], true_lengths[edge_key], abs_tol=abs_tol
)
# *************************************************************************
# *************************************************************************
custom_filter='["highway"~"residential|tertiary|unclassified|service"]',
Pedro L. Magalhães
committed
self.validate_edge_distances(G=G)
Pedro L. Magalhães
committed
self.validate_edge_distances(G=projected_G)
# *************************************************************************
# *************************************************************************
def example_node_path_lengths(self):
# identify path between two nodes
this_length = gis_calc.node_path_length(network=G, path=path)
network=G, path=path, return_minimum_length_only=False
)
true_lengths = [25, 20]
for _length in these_lengths:
assert _length in true_lengths
# *********************************************************************
# *********************************************************************
this_length = gis_calc.node_path_length(network=G, path=path)
network=G, path=path, return_minimum_length_only=False
)
this_length = gis_calc.node_path_length(network=G, path=path)
# *********************************************************************
# *********************************************************************
# *************************************************************************
# *************************************************************************
def example_edge_path_lengths(self):
# identify path between two nodes
this_length = gis_calc.edge_path_length(network=G, path=path)
this_length = gis_calc.edge_path_length(network=G, path=path)
# *********************************************************************
# *********************************************************************
this_length = gis_calc.edge_path_length(network=G, path=path)
this_length = gis_calc.edge_path_length(network=G, path=path)
# *********************************************************************
# *********************************************************************
# *************************************************************************
# *************************************************************************
def example_count_occurrences(self, G: nx.MultiDiGraph):
# get a gdf from the graph via osmnx
column = "name"
# count the ocurrences
count_dict = gis_calc.count_ocurrences(gdf, column=column)
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
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,
}
for key, value in count_dict.items():
assert value == true_count_dict[key]
# *********************************************************************
# *********************************************************************
gdf, column=column, column_entries=["Kløvermarken", "Grenevej", "Bopladsen"]
)
for key, value in count_dict.items():
assert value == true_count_dict[key]
# *********************************************************************
# *********************************************************************
count_dict = gis_calc.count_ocurrences(gdf, column=column, column_entries=[nan])
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
# 1º longitude at a latitude of 56º + 1º latituide at a longitude of 13º
list_3_points = [(12, 56), (13, 56), (13, 57)]
Pedro L. Magalhães
committed
# 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)
error_triggered = False
try:
except Exception:
error_triggered = True
assert error_triggered
_length = gis_calc.great_circle_distance_along_path(line)
assert isclose(_length, true_length_2_points_a, abs_tol=length_tolerance)
_length = gis_calc.great_circle_distance_along_path(line)
assert isclose(_length, true_length_2_points_b, abs_tol=length_tolerance)
_length = gis_calc.great_circle_distance_along_path(line)
assert isclose(_length, true_length_3_points, abs_tol=length_tolerance)
# *************************************************************************
# *************************************************************************
# *****************************************************************************
# *****************************************************************************