3
3
from pathlib import Path
4
4
5
5
import geopandas as gp
6
+ import numpy as np
6
7
7
8
from pam .core import Population
8
9
from pam .read import read_matsim
9
10
from pam .write import write_matsim
11
+ from scipy .spatial import cKDTree
10
12
11
13
12
14
def snap_facilities_to_network (
@@ -19,11 +21,29 @@ def snap_facilities_to_network(
19
21
network (gp.GeoDataFrame): A network geometry shapefile.
20
22
link_id_field (str, optional): The link ID field to use in the network shapefile. Defaults to "id".
21
23
"""
22
- link_ids = network [link_id_field ]
24
+ if network .geometry .geom_type [0 ] == 'Point' :
25
+ coordinates = np .array (list (zip (network .geometry .x , network .geometry .y )))
26
+ else :
27
+ coordinates = np .array (list (zip (network .geometry .centroid .x , network .geometry .centroid .y )))
28
+
29
+ tree = cKDTree (coordinates )
30
+ link_ids = network [link_id_field ].values
31
+
32
+ activity_points = []
33
+ activities_info = []
23
34
for _ , _ , person in population .people ():
24
35
for act in person .activities :
25
- link_id = link_ids [network .distance (act .location .loc ).argmin ()]
26
- act .location .link = link_id
36
+ point = act .location .loc
37
+ if not hasattr (point , 'x' ) or not hasattr (point , 'y' ):
38
+ point = point .centroid
39
+ activity_points .append ((point .x , point .y ))
40
+ activities_info .append (act )
41
+
42
+ activity_points = np .array (activity_points )
43
+ distances , indices = tree .query (activity_points )
44
+
45
+ for act , index in zip (activities_info , indices ):
46
+ act .location .link = link_ids [index ]
27
47
28
48
29
49
def run_facility_link_snapping (
0 commit comments