Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix#32 #33

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
7 changes: 6 additions & 1 deletion routingpy/isochrone.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ def __init__(self, geometry=None, interval=None, center=None):
@property
def geometry(self):
"""
The geometry of the isochrone as [[lon1, lat1], [lon2, lat2], ...] list.
The geometry of the isochrone as
[[[[lon_ex1, lat_ex1], [lon_ex2, lat_ex2], ...], [[lon_in1, lat_in1], [lon_in2, lat_in2], ...]], [other_polygon]] list.

:rtype: list or None

.. note::
Since it's not known whether providers' responses adhere to OGC standards, caution is advised with regard
to possible orientation issues of exterior and interior rings of the resulting polygons.
"""
return self._geometry

Expand Down
9 changes: 6 additions & 3 deletions routingpy/routers/graphhopper.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,14 @@ def _parse_isochrone_json(response, type, max_range, buckets, center):
isochrones = []
accessor = "polygons" if type == "json" else "features"
for index, polygon in enumerate(response[accessor]):
geometry = (
[[polygon["geometry"]["coordinates"]]]
if polygon["geometry"]["type"] == "polygon"
else [polygon["geometry"]["coordinates"]] # if MultiPolygon
)
isochrones.append(
Isochrone(
geometry=[
l[:2] for l in polygon["geometry"]["coordinates"][0] # noqa: E741
], # takes in elevation for some reason
geometry=geometry, # noqa: E741
interval=int(max_range * ((polygon["properties"]["bucket"] + 1) / buckets)),
center=center,
)
Expand Down
2 changes: 1 addition & 1 deletion routingpy/routers/heremaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ def _parse_isochrone_json(response, intervals):

geometries.append(
Isochrone(
geometry=range_polygons,
geometry=[range_polygons],
interval=intervals[idx],
center=list(response["response"]["start"]["mappedPosition"].values()),
)
Expand Down
2 changes: 1 addition & 1 deletion routingpy/routers/mapbox_osrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def _parse_isochrone_json(response, intervals, locations):
return Isochrones(
[
Isochrone(
geometry=isochrone["geometry"]["coordinates"],
geometry=[[isochrone["geometry"]["coordinates"]]],
interval=intervals[idx],
center=locations,
)
Expand Down
7 changes: 6 additions & 1 deletion routingpy/routers/openrouteservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,14 @@ def _parse_isochrone_json(response):

isochrones = []
for idx, isochrone in enumerate(response["features"]):
geometry = (
[isochrone["geometry"]["coordinates"]]
if isochrone["geometry"]["type"] == "Polygon"
else [isochrone["geometry"]["coordinates"]] # if MultiPolygon
)
isochrones.append(
Isochrone(
geometry=isochrone["geometry"]["coordinates"][0],
geometry=geometry,
interval=isochrone["properties"]["value"],
center=isochrone["properties"]["center"],
)
Expand Down
2 changes: 1 addition & 1 deletion routingpy/routers/valhalla.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def _parse_isochrone_json(response, intervals, locations):
if feature["geometry"]["type"] in ("LineString", "Polygon"):
isochrones.append(
Isochrone(
geometry=feature["geometry"]["coordinates"],
geometry=[[feature["geometry"]["coordinates"]]],
interval=intervals[idx],
center=locations,
)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_graphhopper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
from routingpy.isochrone import Isochrones, Isochrone
from routingpy.matrix import Matrix


from tests.test_helper import *
import tests as _test
from tests.utils import get_max_depth

import responses
from copy import deepcopy
Expand Down Expand Up @@ -128,6 +130,7 @@ def test_full_isochrones(self):
self.assertIsInstance(iso.geometry, list)
self.assertIsInstance(iso.interval, int)
self.assertIsInstance(iso.center, list)
self.assertEqual(get_max_depth(iso.geometry), 4)

@responses.activate
def test_full_matrix(self):
Expand Down
16 changes: 12 additions & 4 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
},
"geometry": {
"coordinates": [
[[8.684544, 49.423295], [8.684665, 49.423101], [8.684706, 49.423036]]
[8.684544, 49.423295],
[8.684665, 49.423101],
[8.684706, 49.423036],
],
"type": "Polygon",
},
Expand All @@ -74,7 +76,9 @@
},
"geometry": {
"coordinates": [
[[8.684544, 49.423295], [8.684665, 49.423101], [8.684706, 49.423036]]
[8.684544, 49.423295],
[8.684665, 49.423101],
[8.684706, 49.423036],
],
"type": "Polygon",
},
Expand Down Expand Up @@ -141,7 +145,9 @@
},
"geometry": {
"coordinates": [
[[8.684544, 49.423295], [8.684665, 49.423101], [8.684706, 49.423036]]
[8.684544, 49.423295],
[8.684665, 49.423101],
[8.684706, 49.423036],
],
"type": "Polygon",
},
Expand All @@ -155,7 +161,9 @@
},
"geometry": {
"coordinates": [
[[8.684544, 49.423295], [8.684665, 49.423101], [8.684706, 49.423036]]
[8.684544, 49.423295],
[8.684665, 49.423101],
[8.684706, 49.423036],
],
"type": "Polygon",
},
Expand Down
3 changes: 3 additions & 0 deletions tests/test_heremaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from routingpy.direction import Direction, Directions
from routingpy.isochrone import Isochrones, Isochrone
from routingpy.matrix import Matrix
from tests.utils import get_max_depth

from tests.test_helper import *
import tests as _test
Expand Down Expand Up @@ -171,6 +172,7 @@ def test_full_isochrones_response_object(self):
self.assertIsInstance(iso.geometry, list)
self.assertIsInstance(iso.center, list)
self.assertIsInstance(iso.interval, int)
self.assertEqual(get_max_depth(iso.geometry), 4)

@responses.activate
def test_full_matrix(self):
Expand Down Expand Up @@ -356,6 +358,7 @@ def test_full_isochrones_response_object(self):
self.assertIsInstance(iso.geometry, list)
self.assertIsInstance(iso.center, list)
self.assertIsInstance(iso.interval, int)
self.assertEqual(get_max_depth(iso.geometry), 4)

@responses.activate
def test_full_matrix(self):
Expand Down
12 changes: 7 additions & 5 deletions tests/test_mapbox_osrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from routingpy.matrix import Matrix
from tests.test_helper import *
import tests as _test
from tests.utils import get_max_depth

import responses
from copy import deepcopy
Expand Down Expand Up @@ -134,11 +135,12 @@ def test_full_isochrones(self):
)
self.assertIsInstance(iso, Isochrones)
self.assertEqual(2, len(iso))
for ischrone in iso:
self.assertIsInstance(ischrone, Isochrone)
self.assertIsInstance(ischrone.geometry, list)
self.assertIsInstance(ischrone.interval, int)
self.assertIsInstance(ischrone.center, list)
for isochrone in iso:
self.assertIsInstance(isochrone, Isochrone)
self.assertIsInstance(isochrone.geometry, list)
self.assertIsInstance(isochrone.interval, int)
self.assertIsInstance(isochrone.center, list)
self.assertEqual(get_max_depth(isochrone.geometry), 4)

@responses.activate
def test_full_matrix(self):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_openrouteservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from tests.test_helper import *
import tests as _test
from tests.utils import get_max_depth

import responses
import json
Expand Down Expand Up @@ -122,6 +123,7 @@ def test_full_isochrones(self):
self.assertIsInstance(iso.geometry, list)
self.assertIsInstance(iso.center, list)
self.assertIsInstance(iso.interval, int)
self.assertEqual(get_max_depth(iso.geometry), 4)

@responses.activate
def test_full_matrix(self):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_valhalla.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from routingpy.matrix import Matrix
from tests.test_helper import *
import tests as _test
from tests.utils import get_max_depth

import json
import responses
Expand Down Expand Up @@ -111,6 +112,7 @@ def test_full_isochrones(self):
self.assertIsInstance(i.geometry, list)
self.assertIsInstance(i.interval, int)
self.assertIsInstance(i.center, list)
self.assertEqual(get_max_depth(i.geometry), 4)

@responses.activate
def test_isodistances(self):
Expand Down
3 changes: 3 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def get_max_depth(list_):
"""Returns the maximum depth of a nested list."""
return isinstance(list_, list) and max(map(get_max_depth, list_)) + 1