Skip to content

Commit 21c362b

Browse files
Thedore-ChatziioannouThedore-Chatziioannou
authored andcommitted
comments
1 parent fd3e3e3 commit 21c362b

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
### Fixed
2525

2626
### Added
27-
* Facility link snapping.
27+
* Facility link snapping (#276).
2828

2929
### Changed
3030

src/pam/operations/snap.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
""" Methods for snapping elements to the network or facilities. """
22

3+
from pathlib import Path
4+
35
import geopandas as gp
46

57
from pam.core import Population
@@ -13,7 +15,7 @@ def snap_facilities_to_network(
1315
"""Snaps activity facilities to a network geometry (in-place).
1416
1517
Args:
16-
population (Population): A PAM popualtion.
18+
population (Population): A PAM population.
1719
network (gp.GeoDataFrame): A network geometry shapefile.
1820
link_id_field (str, optional): The link ID field to use in the network shapefile. Defaults to "id".
1921
"""
@@ -39,7 +41,7 @@ def run_facility_link_snapping(
3941
link_id_field (str, optional): The link ID field to use in the network shapefile. Defaults to "id".
4042
"""
4143
population = read_matsim(path_population_in)
42-
if path_network_geometry.endswith(".parquet"):
44+
if ".parquet" in Path(path_network_geometry).suffixes:
4345
network = gp.read_parquet(path_network_geometry)
4446
else:
4547
network = gp.read_file(path_network_geometry)

tests/test_29_snap.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import os
2-
from pathlib import Path
32

43
import geopandas as gp
4+
import pytest
55
from pam.operations.snap import run_facility_link_snapping, snap_facilities_to_network
66
from pam.read import read_matsim
77

8-
TEST_DATA_DIR = Path(__file__).parent / "test_data"
9-
108

119
def test_add_snapping_adds_link_attribute(population_heh):
12-
network = gp.read_file(os.path.join(TEST_DATA_DIR, "test_link_geometry.geojson"))
10+
network = gp.read_file(pytest.test_data_dir / "test_link_geometry.geojson")
1311
for _, _, person in population_heh.people():
1412
for act in person.activities:
1513
assert act.location.link is None
@@ -19,13 +17,22 @@ def test_add_snapping_adds_link_attribute(population_heh):
1917
for act in person.activities:
2018
assert act.location.link is not None
2119

20+
# check that the link is indeed the nearest one
21+
link_distance = (
22+
network.set_index("id")
23+
.loc[act.location.link, "geometry"]
24+
.distance(act.location.loc)
25+
)
26+
min_distance = network.distance(act.location.loc).min()
27+
assert link_distance == min_distance
28+
2229

2330
def test_links_resnapped(tmpdir):
2431
path_out = os.path.join(tmpdir, "pop_snapped.xml")
2532
run_facility_link_snapping(
26-
path_population_in=os.path.join(TEST_DATA_DIR, "1.plans.xml"),
33+
path_population_in=pytest.test_data_dir / "1.plans.xml",
2734
path_population_out=path_out,
28-
path_network_geometry=os.path.join(TEST_DATA_DIR, "test_link_geometry.geojson"),
35+
path_network_geometry=pytest.test_data_dir / "test_link_geometry.geojson",
2936
)
3037
assert os.path.exists(path_out)
3138
pop_snapped = read_matsim(path_out)

0 commit comments

Comments
 (0)