Skip to content

Commit

Permalink
Update the code to address the comment
Browse files Browse the repository at this point in the history
  • Loading branch information
syhwawa committed Aug 6, 2024
1 parent baffc5e commit fda1ccc
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/pam/operations/snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import geopandas as gp
import numpy as np
from scipy.spatial import cKDTree

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(
Expand All @@ -21,29 +21,19 @@ def snap_facilities_to_network(
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':
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]
distance, index = tree.query([(point.x, point.y)])
act.location.link = link_ids[index[0]]


def run_facility_link_snapping(
Expand Down

0 comments on commit fda1ccc

Please sign in to comment.