Skip to content

Commit 0fb5c25

Browse files
authored
Merge pull request #756 from knaaptime/pdnagraph
exposing mapping_distance in travel builder
2 parents 01d41cf + 099bcfc commit 0fb5c25

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

libpysal/graph/_network.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ def pdna_to_adj(origins, network, node_ids, threshold):
6161
return adj
6262

6363

64-
def build_travel_graph(
65-
df,
66-
network,
67-
threshold,
68-
):
64+
def build_travel_graph(df, network, threshold, mapping_distance):
6965
"""Compute the shortest path between gdf centroids via a pandana.Network
7066
and return an adjacency list with weight=cost. Note unlike distance_band,
7167
:math:`G_{ij}` and :math:`G_{ji}` are often different because travel networks
@@ -81,6 +77,10 @@ def build_travel_graph(
8177
Network that encodes travel costs. See <https://udst.github.io/pandana/>
8278
threshold : int
8379
maximum travel cost to consider neighbors
80+
mapping_distance : int
81+
snapping tolerance passed to ``pandana.Network.get_node_ids`` that defines
82+
the maximum range at which observations are snapped to nearest nodes in the
83+
network. Default is None
8484
8585
Returns
8686
-------
@@ -89,7 +89,9 @@ def build_travel_graph(
8989
"""
9090
df = df.copy()
9191
_validate_geometry_input(df.geometry, ids=None, valid_geometry_types="Point")
92-
df["node_ids"] = network.get_node_ids(df.geometry.x, df.geometry.y)
92+
df["node_ids"] = network.get_node_ids(
93+
df.geometry.x, df.geometry.y, mapping_distance
94+
)
9395

9496
# depending on density of the graph nodes / observations, it is common to have
9597
# multiple observations snapped to the same network node, so use the clique

libpysal/graph/base.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,18 +1456,21 @@ def build_h3(cls, ids, order=1, weight="distance"):
14561456
raise ValueError("weight must be one of 'distance', 'binary', or 'inverse'")
14571457

14581458
@classmethod
1459-
def build_travel_cost(cls, df, network, threshold, kernel=None):
1459+
def build_travel_cost(
1460+
cls, df, network, threshold, kernel=None, mapping_distance=None
1461+
):
14601462
"""Generate a Graph based on shortest travel costs from a pandana.Network
14611463
14621464
Parameters
14631465
----------
14641466
df : geopandas.GeoDataFrame
14651467
geodataframe representing observations which are snapped to the nearest
1466-
node in the pandana.Network. If passing polygon geometries, the spatial
1467-
support will be reduced to Points (via centroid) before snapping.
1468+
node in the pandana.Network. CRS should be the same as the locations
1469+
of ``node_x`` and ``node_y`` in the pandana.Network (usually 4326 if network
1470+
comes from OSM, but sometimes projected to improve snapping quality).
14681471
network : pandana.Network
14691472
pandana Network object describing travel costs between nodes in the study
1470-
area
1473+
area. See <https://udst.github.io/pandana/> for more
14711474
threshold : int
14721475
threshold representing maximum cost distances. This is measured in the same
14731476
units as the pandana.Network (not influenced by the df.crs in any way). For
@@ -1480,6 +1483,10 @@ def build_travel_cost(cls, df, network, threshold, kernel=None):
14801483
libpysal.graph.Graph.build_kernel for more information on kernel
14811484
transformation options. Default is None, in which case the Graph weight
14821485
is pure distance between focal and neighbor
1486+
mapping_distance : int
1487+
snapping tolerance passed to ``pandana.Network.get_node_ids`` that defines
1488+
the maximum range at which observations are snapped to nearest nodes in the
1489+
network. Default is None
14831490
14841491
Returns
14851492
-------
@@ -1524,7 +1531,7 @@ def build_travel_cost(cls, df, network, threshold, kernel=None):
15241531
117 333.639008
15251532
Name: weight, dtype: float64
15261533
"""
1527-
adj = _build_travel_graph(df, network, threshold)
1534+
adj = _build_travel_graph(df, network, threshold, mapping_distance)
15281535
g = cls.from_adjacency(adj)
15291536
if kernel is not None:
15301537
arrays = _kernel(

0 commit comments

Comments
 (0)