Skip to content

Commit 4fa16b0

Browse files
authored
feat: add geotiff support valhalla isochrones (#150)
1 parent 96c791c commit 4fa16b0

File tree

6 files changed

+80
-3
lines changed

6 files changed

+80
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212

1313
- Added a `km` and a `mi` property to `Direction` class to get the distance in kilometers and miles ([#139](https://github.com/gis-ops/routingpy/issues/139)).
1414
Distance stored in `Direction.distance` are always in meters.
15-
1615
- Added a `pred_edge_id` property to `Edge` class that is used in `Expansions`.
16+
- `geotiff` support for Valhalla's isochrones ([#150](https://github.com/mthh/routingpy/pull/150))
1717

1818
### Fixed
1919

@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2323
- Added missing support for `kwargs` to Valhalla's `matrix` creation function ([#135](https://github.com/mthh/routingpy/pull/135))
2424
- Fixed Valhalla's `expansion` method to follow the latest Valhalla API changes ([#142](https://github.com/mthh/routingpy/pull/142))
2525
- Updating params with `kwargs` [#145](https://github.com/mthh/routingpy/pull/145)
26+
- Relative import so submoduling works ([#150](https://github.com/mthh/routingpy/pull/150))
2627

2728
### Removed
2829

routingpy/routers/valhalla.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#
1717

1818
from operator import itemgetter
19-
from typing import List, Optional, Sequence, Union # noqa: F401
19+
from typing import List, Optional, Sequence, Union
2020

2121
from .. import utils
2222
from ..client_base import DEFAULT
@@ -25,6 +25,7 @@
2525
from ..expansion import Edge, Expansions
2626
from ..isochrone import Isochrone, Isochrones
2727
from ..matrix import Matrix
28+
from ..raster import Raster
2829
from ..valhalla_attributes import MatchedResults
2930

3031

@@ -488,6 +489,62 @@ def parse_isochrone_json(response, intervals, locations, interval_type):
488489

489490
return Isochrones(isochrones, response)
490491

492+
def raster( # noqa: C901
493+
self,
494+
locations: List[float],
495+
profile: str,
496+
intervals: List[int],
497+
interval_type: Optional[str] = "time",
498+
colors: Optional[List[str]] = None,
499+
polygons: Optional[bool] = None,
500+
denoise: Optional[float] = None,
501+
generalize: Optional[float] = None,
502+
preference: Optional[str] = None,
503+
options: Optional[dict] = None,
504+
avoid_locations: Optional[List[List[float]]] = None,
505+
avoid_polygons: Optional[List[List[List[float]]]] = None,
506+
date_time: Optional[dict] = None,
507+
show_locations: Optional[List[List[float]]] = None,
508+
id: Optional[str] = None,
509+
dry_run: Optional[bool] = None,
510+
**kwargs
511+
):
512+
"""
513+
For parameters see docs for isochrones.
514+
515+
Returns isochrones/isodistances as GeoTIFF
516+
"""
517+
params = self.get_isochrone_params(
518+
locations,
519+
profile,
520+
intervals,
521+
interval_type,
522+
colors,
523+
polygons,
524+
denoise,
525+
generalize,
526+
preference,
527+
options,
528+
avoid_locations,
529+
avoid_polygons,
530+
date_time,
531+
show_locations,
532+
id,
533+
**kwargs
534+
)
535+
params["format"] = "geotiff"
536+
537+
return self.parse_raster_response(
538+
self.client._request("/isochrone", post_params=params, dry_run=dry_run), max(intervals)
539+
)
540+
541+
@staticmethod
542+
def parse_raster_response(response, max_travel_time: int) -> Raster:
543+
if response is None: # pragma: no cover
544+
return Raster()
545+
546+
return Raster(image=response, max_travel_time=max_travel_time)
547+
491548
def matrix(
492549
self,
493550
locations: List[List[float]],
File renamed without changes.

tests/raster_valhalla.tif

1.46 KB
Binary file not shown.

tests/test_opentripplanner_v2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_raster(self):
124124
("arriveBy", "false"),
125125
("cutoff", convert.seconds_to_iso8601(query["cutoff"])),
126126
]
127-
with open("tests/raster_example.tiff", "rb") as raster_file:
127+
with open("tests/raster_otp.tiff", "rb") as raster_file:
128128
image = raster_file.read()
129129
responses.add(
130130
responses.GET,

tests/test_valhalla.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from routingpy.expansion import Expansions
2828
from routingpy.isochrone import Isochrone, Isochrones
2929
from routingpy.matrix import Matrix
30+
from routingpy.raster import Raster
3031
from routingpy.valhalla_attributes import (
3132
MatchedEdge,
3233
MatchedPoint,
@@ -151,6 +152,24 @@ def test_isodistances(self):
151152
self.assertIsInstance(i, Isochrone)
152153
self.assertEqual(i.interval_type, "distance")
153154

155+
@responses.activate
156+
def test_raster(self):
157+
query = deepcopy(ENDPOINTS_QUERIES[self.name]["isochrones"])
158+
query["format"] = "geotiff"
159+
with open("tests/raster_valhalla.tif", "rb") as raster_file:
160+
image = raster_file.read()
161+
responses.add(
162+
responses.POST,
163+
"https://api.mapbox.com/valhalla/v1/isochrone",
164+
status=200,
165+
body=image,
166+
content_type="image/tiff",
167+
)
168+
raster = self.client.raster(**query)
169+
self.assertIsInstance(raster, Raster)
170+
self.assertEqual(raster.image, image)
171+
self.assertEqual(raster.max_travel_time, max(query["intervals"]))
172+
154173
# TODO: test colors having less items than range
155174
@responses.activate
156175
def test_full_matrix(self):

0 commit comments

Comments
 (0)