Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Thedore-Chatziioannou authored and Thedore-Chatziioannou committed May 15, 2024
1 parent fd3e3e3 commit 21c362b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

### Added
* Facility link snapping.
* Facility link snapping (#276).

### Changed

Expand Down
6 changes: 4 additions & 2 deletions src/pam/operations/snap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
""" Methods for snapping elements to the network or facilities. """

from pathlib import Path

import geopandas as gp

from pam.core import Population
Expand All @@ -13,7 +15,7 @@ def snap_facilities_to_network(
"""Snaps activity facilities to a network geometry (in-place).
Args:
population (Population): A PAM popualtion.
population (Population): A PAM population.
network (gp.GeoDataFrame): A network geometry shapefile.
link_id_field (str, optional): The link ID field to use in the network shapefile. Defaults to "id".
"""
Expand All @@ -39,7 +41,7 @@ def run_facility_link_snapping(
link_id_field (str, optional): The link ID field to use in the network shapefile. Defaults to "id".
"""
population = read_matsim(path_population_in)
if path_network_geometry.endswith(".parquet"):
if ".parquet" in Path(path_network_geometry).suffixes:
network = gp.read_parquet(path_network_geometry)
else:
network = gp.read_file(path_network_geometry)
Expand Down
19 changes: 13 additions & 6 deletions tests/test_29_snap.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import os
from pathlib import Path

import geopandas as gp
import pytest
from pam.operations.snap import run_facility_link_snapping, snap_facilities_to_network
from pam.read import read_matsim

TEST_DATA_DIR = Path(__file__).parent / "test_data"


def test_add_snapping_adds_link_attribute(population_heh):
network = gp.read_file(os.path.join(TEST_DATA_DIR, "test_link_geometry.geojson"))
network = gp.read_file(pytest.test_data_dir / "test_link_geometry.geojson")
for _, _, person in population_heh.people():
for act in person.activities:
assert act.location.link is None
Expand All @@ -19,13 +17,22 @@ def test_add_snapping_adds_link_attribute(population_heh):
for act in person.activities:
assert act.location.link is not None

# check that the link is indeed the nearest one
link_distance = (
network.set_index("id")
.loc[act.location.link, "geometry"]
.distance(act.location.loc)
)
min_distance = network.distance(act.location.loc).min()
assert link_distance == min_distance


def test_links_resnapped(tmpdir):
path_out = os.path.join(tmpdir, "pop_snapped.xml")
run_facility_link_snapping(
path_population_in=os.path.join(TEST_DATA_DIR, "1.plans.xml"),
path_population_in=pytest.test_data_dir / "1.plans.xml",
path_population_out=path_out,
path_network_geometry=os.path.join(TEST_DATA_DIR, "test_link_geometry.geojson"),
path_network_geometry=pytest.test_data_dir / "test_link_geometry.geojson",
)
assert os.path.exists(path_out)
pop_snapped = read_matsim(path_out)
Expand Down

0 comments on commit 21c362b

Please sign in to comment.