Skip to content

Commit 7c15f5f

Browse files
committed
fix: update valhalla's expansion API
fixes #142
1 parent 83afe3e commit 7c15f5f

File tree

3 files changed

+157
-45
lines changed

3 files changed

+157
-45
lines changed

routingpy/expansion.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,27 @@
2323
class Edge:
2424
"""
2525
Contains a parsed single line string of an edge and its attributes, if specified in the request.
26-
Access via properties ``geometry``, ``distances`` ``durations``, ``costs``, ``edge_ids``, ``statuses``.
26+
Access via properties ``geometry``, ``distance`` ``duration``, ``cost``, ``edge_id``,
27+
``pred_edge_id``, ``edge_status``.
2728
"""
2829

2930
def __init__(
30-
self, geometry=None, distances=None, durations=None, costs=None, edge_ids=None, statuses=None
31+
self,
32+
geometry=None,
33+
distance=None,
34+
duration=None,
35+
cost=None,
36+
edge_id=None,
37+
pred_edge_id=None,
38+
edge_status=None,
3139
):
3240
self._geometry = geometry
33-
self._distance = distances
34-
self._duration = durations
35-
self._cost = costs
36-
self._edge_id = edge_ids
37-
self._status = statuses
41+
self._distance = distance
42+
self._duration = duration
43+
self._cost = cost
44+
self._edge_id = edge_id
45+
self._pred_edge_id = pred_edge_id
46+
self._status = edge_status
3847

3948
@property
4049
def geometry(self) -> Optional[List[List[float]]]:
@@ -81,6 +90,15 @@ def edge_id(self) -> Optional[int]:
8190
"""
8291
return self._edge_id
8392

93+
@property
94+
def pred_edge_id(self) -> Optional[int]:
95+
"""
96+
The predecessor edge IDs for each edge in order of graph traversal.
97+
98+
:rtype: int or None
99+
"""
100+
return self._pred_edge_id
101+
84102
@property
85103
def status(self) -> Optional[str]:
86104
"""

routingpy/routers/valhalla.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,6 @@ def isochrones( # noqa: C901
330330
will be filled automatically. For more information, visit:
331331
https://valhalla.github.io/valhalla/api/turn-by-turn/api-reference/#costing-options
332332
333-
:param units: Distance units for output. One of ['mi', 'km']. Default km.
334-
335333
:param language: The language of the narration instructions based on the IETF BCP 47 language tag string.
336334
One of ['ca', 'cs', 'de', 'en', 'pirate', 'es', 'fr', 'hi', 'it', 'pt', 'ru', 'sl', 'sv']. Default 'en'.
337335
@@ -663,18 +661,19 @@ def expansion(
663661
) -> Expansions:
664662
"""Gets the expansion tree for a range of time or distance values around a given coordinate.
665663
666-
For more information, visit https://valhalla.readthedocs.io/en/latest/api/expansion/api-reference/.
664+
For more information, visit https://valhalla.github.io/valhalla/api/expansion/api-reference/.
667665
668666
:param locations: One pair of lng/lat values. Takes the form [Longitude, Latitude].
669667
670668
:param profile: Specifies the mode of transport to use when calculating
671-
directions. One of ["auto", "bicycle", "multimodal", "pedestrian".
669+
directions. One of ["auto", "bicycle", "multimodal", "pedestrian"].
672670
673671
:param intervals: Time ranges to calculate isochrones for. In seconds or meters, depending on `interval_type`.
674672
675673
:param skip_opposites: If set to true the output won't contain an edge's opposing edge. Opposing edges can be thought of as both directions of one road segment. Of the two, we discard the directional edge with higher cost and keep the one with less cost.
676674
677-
:param expansion_properties: A JSON array of strings of the GeoJSON property keys you'd like to have in the response. One or multiple of "durations", "distances", "costs", "edge_ids", "statuses". Note, that each additional property will increase the output size by minimum ~ 25%.
675+
:param expansion_properties: A JSON array of strings of the GeoJSON property keys you'd like to have in the response.
676+
One or multiple of "duration", "distance", "cost", "edge_id", "pre_edge_id", "edge_status". Note, that each additional property will increase the output size by minimum ~ 10%.
678677
679678
:param interval_type: Set 'time' for isochrones or 'distance' for equidistants.
680679
Default 'time'.
@@ -690,7 +689,7 @@ def expansion(
690689
691690
E.g. date_time = {type: 0, value: 2021-03-03T08:06}
692691
693-
:param id: Name your route request. If id is specified, the naming will be sent thru to the response.
692+
:param id: Name your route request. If id is specified, the naming will be sent through to the response.
694693
695694
:param dry_run: Print URL and parameters without sending the request.
696695
@@ -750,17 +749,16 @@ def get_expansion_params(
750749

751750
@staticmethod
752751
def parse_expansion_json(response, locations, expansion_properties, interval_type):
753-
if response is None: # pragma: no cover
752+
if response is None or "features" not in response: # pragma: no cover
754753
return Expansions()
755754

756755
expansions = []
757-
for idx, line in enumerate(response["features"][0]["geometry"]["coordinates"]):
756+
for feature in response["features"]:
757+
line = feature["geometry"]["coordinates"]
758758
properties = {}
759759
if expansion_properties:
760760
for expansion_prop in expansion_properties:
761-
properties[expansion_prop] = response["features"][0]["properties"][expansion_prop][
762-
idx
763-
]
761+
properties[expansion_prop] = feature["properties"][expansion_prop]
764762
expansions.append(Edge(geometry=line, **properties))
765763

766764
return Expansions(expansions, locations, interval_type, response)
@@ -785,7 +783,7 @@ def trace_attributes(
785783
786784
:param locations: One pair of lng/lat values or :class:`Waypoint`. Takes the form [Longitude, Latitude].
787785
:param profile: Specifies the mode of transport to use when calculating
788-
directions. One of ["auto", "bicycle", "multimodal", "pedestrian".
786+
directions. One of ["auto", "bicycle", "multimodal", "pedestrian"].
789787
:param shape_match: It allows some control of the matching algorithm based on the type of input. One of
790788
["edge_walk", "map_snap", "walk_or_snap"]. See for full reference:
791789
https://valhalla.github.io/valhalla/api/map-matching/api-reference/#shape-matching-parameters

tests/data/mock.py

Lines changed: 122 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,34 +89,130 @@
8989
]
9090
},
9191
"expansion": {
92-
"properties": {"algorithm": "unidirectional_dijkstra"},
9392
"type": "FeatureCollection",
9493
"features": [
9594
{
9695
"type": "Feature",
9796
"geometry": {
98-
"type": "MultiLineString",
99-
"coordinates": [
100-
[[0.00027, -0.00017], [0.00027, 0.0]],
101-
[[0.00027, -0.00017], [0.00027, -0.00035]],
102-
[[0.00027, -0.00035], [0.00027, -0.00017]],
103-
[[0.00027, 0.0], [0.00027, -0.00017]],
104-
[[0.00027, -0.00017], [0.00053, -0.00017]],
105-
[[0.00027, -0.00017], [0.0, -0.00017]],
106-
[[0.0, -0.00017], [0.00027, -0.00017]],
107-
[[0.00053, -0.00017], [0.0008, -0.00017]],
108-
[[0.0008, -0.00017], [0.00053, -0.00017]],
109-
[[0.00053, -0.00017], [0.00027, -0.00017]],
110-
[[0.00053, -0.00017], [0.0008, 0.0]],
111-
],
97+
"type": "LineString",
98+
"coordinates": [[8.512978, 47.380938], [8.512694, 47.380651]],
11299
},
113-
"properties": {
114-
"distances": [20, 20, 40, 40, 30, 30, 60, 60, 90, 120, 80],
115-
"durations": [0, 0, 29, 29, 1, 1, 30, 2, 31, 33, 5],
116-
"costs": [0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 11],
100+
"properties": {"duration": 0, "distance": 0, "cost": 0},
101+
},
102+
{
103+
"type": "Feature",
104+
"geometry": {
105+
"type": "LineString",
106+
"coordinates": [[8.512694, 47.380651], [8.512589, 47.380545]],
117107
},
118-
}
108+
"properties": {"duration": 4, "distance": 14, "cost": 8},
109+
},
110+
{
111+
"type": "Feature",
112+
"geometry": {
113+
"type": "LineString",
114+
"coordinates": [[8.512694, 47.380651], [8.512978, 47.380938]],
115+
},
116+
"properties": {"duration": 7, "distance": 37, "cost": 8},
117+
},
118+
{
119+
"type": "Feature",
120+
"geometry": {
121+
"type": "LineString",
122+
"coordinates": [[8.512978, 47.380938], [8.513163, 47.381124]],
123+
},
124+
"properties": {"duration": 11, "distance": 62, "cost": 14},
125+
},
126+
{
127+
"type": "Feature",
128+
"geometry": {
129+
"type": "LineString",
130+
"coordinates": [[8.512589, 47.380545], [8.512461, 47.380427]],
131+
},
132+
"properties": {"duration": 7, "distance": 30, "cost": 16},
133+
},
134+
{
135+
"type": "Feature",
136+
"geometry": {
137+
"type": "LineString",
138+
"coordinates": [[8.512461, 47.380427], [8.512249, 47.380203]],
139+
},
140+
"properties": {"duration": 15, "distance": 60, "cost": 28},
141+
},
142+
{
143+
"type": "Feature",
144+
"geometry": {
145+
"type": "LineString",
146+
"coordinates": [[8.513163, 47.381124], [8.513588, 47.381568]],
147+
},
148+
"properties": {"duration": 23, "distance": 121, "cost": 28},
149+
},
150+
{
151+
"type": "Feature",
152+
"geometry": {
153+
"type": "LineString",
154+
"coordinates": [[8.513588, 47.381568], [8.513632, 47.381613]],
155+
},
156+
"properties": {"duration": 24, "distance": 127, "cost": 35},
157+
},
158+
{
159+
"type": "Feature",
160+
"geometry": {
161+
"type": "LineString",
162+
"coordinates": [[8.513632, 47.381613], [8.514038, 47.382042]],
163+
},
164+
"properties": {"duration": 34, "distance": 184, "cost": 48},
165+
},
166+
{
167+
"type": "Feature",
168+
"geometry": {
169+
"type": "LineString",
170+
"coordinates": [[8.512249, 47.380203], [8.51156, 47.379499]],
171+
},
172+
"properties": {"duration": 33, "distance": 154, "cost": 49},
173+
},
174+
{
175+
"type": "Feature",
176+
"geometry": {
177+
"type": "LineString",
178+
"coordinates": [[8.512978, 47.380938], [8.512871, 47.380983]],
179+
},
180+
"properties": {"duration": 17, "distance": 47, "cost": 644},
181+
},
182+
{
183+
"type": "Feature",
184+
"geometry": {
185+
"type": "LineString",
186+
"coordinates": [[8.512871, 47.380983], [8.512795, 47.381015]],
187+
},
188+
"properties": {"duration": 21, "distance": 54, "cost": 663},
189+
},
190+
{
191+
"type": "Feature",
192+
"geometry": {
193+
"type": "LineString",
194+
"coordinates": [[8.513632, 47.381613], [8.513528, 47.381658]],
195+
},
196+
"properties": {"duration": 31, "distance": 136, "cost": 667},
197+
},
198+
{
199+
"type": "Feature",
200+
"geometry": {
201+
"type": "LineString",
202+
"coordinates": [[8.512795, 47.381015], [8.512679, 47.381069]],
203+
},
204+
"properties": {"duration": 27, "distance": 65, "cost": 673},
205+
},
206+
{
207+
"type": "Feature",
208+
"geometry": {
209+
"type": "LineString",
210+
"coordinates": [[8.512679, 47.381069], [8.512116, 47.381319]],
211+
},
212+
"properties": {"duration": 56, "distance": 122, "cost": 712},
213+
},
119214
],
215+
"properties": {"algorithm": "dijkstras"},
120216
},
121217
"trace_attributes": {
122218
"shape": "qekg}Aq|hqOhIvcBjHo@`]iCFnBbCSh@AlACz@CTCdCvc@vCnl@j@`PjAnTDp@HlBLrBBZpB`b@lBb]~AfZx@`MxWeEDv@FnAFzALz@f@Gt@GzPniBPrC|@Od@MvLgDdWmGjG{C~@v@TpB^?t@CjBMj@I@}@L]RQhAcAxCr@nCx@nCN~UpHrEv@rSrEbZhGFbV",
@@ -1101,9 +1197,9 @@
11011197
"preference": "shortest",
11021198
},
11031199
"expansion": {
1104-
"expansion_properties": ["distances", "durations", "costs"],
1105-
"intervals": [60],
1106-
"locations": [(0.00026949361342338066, -0.00017966240895360996)],
1200+
"expansion_properties": ["distance", "duration", "cost"],
1201+
"intervals": [30],
1202+
"locations": [(8.512516, 47.380742)],
11071203
"profile": "auto",
11081204
},
11091205
"trace_attributes": {
@@ -1249,9 +1345,9 @@
12491345
"units": "km",
12501346
},
12511347
"expansion": {
1252-
"expansion_properties": ["distances", "durations", "costs"],
1253-
"contours": [{"time": 1.0}],
1254-
"locations": [{"lon": 0.00026949361342338066, "lat": -0.00017966240895360996}],
1348+
"expansion_properties": ["distance", "duration", "cost"],
1349+
"contours": [{"time": 0.5}],
1350+
"locations": [{"lon": 8.512516, "lat": 47.380742}],
12551351
"costing": "auto",
12561352
"action": "isochrone",
12571353
},

0 commit comments

Comments
 (0)