Skip to content

Commit

Permalink
Raise an error if non-integer keys are used, for example those with a…
Browse files Browse the repository at this point in the history
…n underscore
  • Loading branch information
geographika committed May 17, 2024
1 parent 14b5f97 commit c665ad9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
69 changes: 52 additions & 17 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,39 @@ def test_pickling():
assert len(net2.edges()) == 1


def test_load_network_with_valid_ints():

rec1 = {
"geometry": {"type": "LineString", "coordinates": [(0, 0), (0, 1)]},
"properties": {"EDGE_ID": "1"},
}
rec2 = {
"geometry": {"type": "LineString", "coordinates": [(0, 1), (0, 2)]},
"properties": {"EDGE_ID": "21"},
}
recs = [rec1, rec2]

net = loader.load_network_from_geometries(recs, use_integer_keys=True)
# print(list(net.graph["keys"].keys()))
assert list(net.graph["keys"].keys()) == [1, 21]


def test_load_network_with_invalid_ints():

rec1 = {
"geometry": {"type": "LineString", "coordinates": [(0, 0), (0, 1)]},
"properties": {"EDGE_ID": "1"},
}
rec2 = {
"geometry": {"type": "LineString", "coordinates": [(0, 1), (0, 2)]},
"properties": {"EDGE_ID": "2_1"},
}
recs = [rec1, rec2]

with pytest.raises(ValueError):
loader.load_network_from_geometries(recs, use_integer_keys=True)


def test_doctest():
import doctest

Expand All @@ -224,21 +257,23 @@ def test_doctest():

if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
test_doctest()
test_add_edge()
test_add_edge_missing_key()
test_add_edge_with_reverse_lookup()
test_create_graph()
test_create_multidigraph()
test_create_graph_no_lookup()
test_load_network_from_records()
test_load_network_from_records_missing_key()
test_load_network_from_geometries()
test_load_network_from_geometries_calculated_length()
test_load_network_from_invalid_geometries()
test_load_network_from_invalid_geometries_skip()
test_load_network_from_invalid_geometries_missing_key()
test_uniquedict()
test_uniquedict_error()
test_pickling()
# test_doctest()
# test_add_edge()
# test_add_edge_missing_key()
# test_add_edge_with_reverse_lookup()
# test_create_graph()
# test_create_multidigraph()
# test_create_graph_no_lookup()
# test_load_network_from_records()
# test_load_network_from_records_missing_key()
# test_load_network_from_geometries()
# test_load_network_from_geometries_calculated_length()
# test_load_network_from_invalid_geometries()
# test_load_network_from_invalid_geometries_skip()
# test_load_network_from_invalid_geometries_missing_key()
# test_uniquedict()
# test_uniquedict_error()
# test_pickling()
test_load_network_with_invalid_ints()
test_load_network_with_valid_ints()
print("Done!")
2 changes: 1 addition & 1 deletion wayfarer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import NamedTuple


__version__ = "0.12.0"
__version__ = "0.12.1"

LENGTH_FIELD = "LEN_"
EDGE_ID_FIELD = "EDGE_ID"
Expand Down
5 changes: 4 additions & 1 deletion wayfarer/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,10 @@ def load_network_from_geometries(

# keys as integers are faster than strings, so convert if possible
if use_integer_keys:
key = int(key)
if key.isdigit():
key = int(key)
else:
raise ValueError(f"Input string '{key}' is not a valid integer")

# if we simply take the coordinates then often these differ due to rounding issues as they
# are floats e.g. (-9.564484483347517, 52.421103202488965) and (-9.552925853749544, 52.41969110706263)
Expand Down

0 comments on commit c665ad9

Please sign in to comment.