-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
facility link snapping #276
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1cc8c3c
facility link snapping
51363ad
update dependencies
172d8e6
update cruft
fd3e3e3
linting
21c362b
comments
baffc5e
Update the snap function using KDTree
syhwawa fda1ccc
Update the code to address the comment
syhwawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
""" Methods for snapping elements to the network or facilities. """ | ||
|
||
from pathlib import Path | ||
|
||
import geopandas as gp | ||
import numpy as np | ||
|
||
from pam.core import Population | ||
from pam.read import read_matsim | ||
from pam.write import write_matsim | ||
from scipy.spatial import cKDTree | ||
|
||
|
||
def snap_facilities_to_network( | ||
population: Population, network: gp.GeoDataFrame, link_id_field: str = "id" | ||
) -> None: | ||
"""Snaps activity facilities to a network geometry (in-place). | ||
|
||
Args: | ||
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". | ||
""" | ||
if network.geometry.geom_type[0] == 'Point': | ||
coordinates = np.array(list(zip(network.geometry.x, network.geometry.y))) | ||
else: | ||
coordinates = np.array(list(zip(network.geometry.centroid.x, network.geometry.centroid.y))) | ||
|
||
tree = cKDTree(coordinates) | ||
link_ids = network[link_id_field].values | ||
|
||
activity_points = [] | ||
activities_info = [] | ||
for _, _, person in population.people(): | ||
for act in person.activities: | ||
point = act.location.loc | ||
if not hasattr(point, 'x') or not hasattr(point, 'y'): | ||
point = point.centroid | ||
activity_points.append((point.x, point.y)) | ||
activities_info.append(act) | ||
|
||
activity_points = np.array(activity_points) | ||
distances, indices = tree.query(activity_points) | ||
|
||
for act, index in zip(activities_info, indices): | ||
act.location.link = link_ids[index] | ||
|
||
|
||
def run_facility_link_snapping( | ||
path_population_in: str, | ||
path_population_out: str, | ||
path_network_geometry: str, | ||
link_id_field: str = "id", | ||
) -> None: | ||
"""Reads a population, snaps activity facilities to a network geometry, and saves the results. | ||
|
||
Args: | ||
path_population_in (str): Path to a PAM population. | ||
path_population_out (str): The path to save the output population. | ||
path_network_geometry (str): Path to the network geometry file. | ||
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 ".parquet" in Path(path_network_geometry).suffixes: | ||
network = gp.read_parquet(path_network_geometry) | ||
else: | ||
network = gp.read_file(path_network_geometry) | ||
snap_facilities_to_network(population=population, network=network, link_id_field=link_id_field) | ||
write_matsim(population=population, plans_path=path_population_out) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
|
||
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 | ||
|
||
|
||
def test_add_snapping_adds_link_attribute(population_heh): | ||
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 | ||
|
||
snap_facilities_to_network(population=population_heh, network=network) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it just calls the function |
||
for _, _, person in population_heh.people(): | ||
for act in person.activities: | ||
assert act.location.link is not None | ||
Theodore-Chatziioannou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# 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=pytest.test_data_dir / "1.plans.xml", | ||
path_population_out=path_out, | ||
path_network_geometry=pytest.test_data_dir / "test_link_geometry.geojson", | ||
) | ||
assert os.path.exists(path_out) | ||
pop_snapped = read_matsim(path_out) | ||
for _, _, person in pop_snapped.people(): | ||
for act in person.activities: | ||
assert "link-" in act.location.link |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::2157" } }, | ||
"features": [ | ||
{ "type": "Feature", "properties": { "id": "link-1" }, "geometry": { "type": "LineString", "coordinates": [ [ 10000.0, 5000.0 ], [ 10000.0, 10000.0 ] ] } }, | ||
{ "type": "Feature", "properties": { "id": "link-2" }, "geometry": { "type": "LineString", "coordinates": [ [ 10000.0, 10000.0 ], [ 5000.0, 5000.0 ] ] } } | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this needed? Loc should always be a point with x and y coordinates (or
None
).See here:
https://github.com/arup-group/pam/blob/3f817a662317d44fca2126295f07d89317db707c/src/pam/location.py#L7-L18
.If that is not the case in the input data, then the inputs should be corrected rather than fixing them silently here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed this.