Skip to content

Commit a11d402

Browse files
author
nilsnolde
committed
restrict optimize_waypoints to not trigger when options or shortest are used
1 parent 6b36880 commit a11d402

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 2.2.0
44

5+
- restrict optimize_waypoints parameter to not trigger when options or 'shortest' is used
56
- Add optimize_waypoints option to directions for simple TSP
67
- get rid of validators, too maintenance-heavy
78

openrouteservice/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# the License.
1818
#
1919

20-
__version__ = "2.2.1"
20+
__version__ = "2.2.2"
2121

2222

2323
def get_ordinal(number):

openrouteservice/directions.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def directions(client,
167167
:param optimize_waypoints: If True, a `Vroom <https://github.com/VROOM-Project/vroom>`_ instance (ORS optimization
168168
endpoint) will optimize the `via` waypoints, i.e. all coordinates between the first and the last. It assumes
169169
the first coordinate to be the start location and the last coordinate to be the end location. Only requests with
170-
a minimum of 4 coordinates can be optimized. Default False.
170+
a minimum of 4 coordinates, no routing options and fastest weighting. Default False.
171171
:type optimize_waypoints: bool
172172
173173
:param validate: Specifies whether parameters should be validated before sending the request. Default True.
@@ -184,9 +184,13 @@ def directions(client,
184184
"""
185185

186186
# call optimization endpoint and get new order of waypoints
187-
if optimize_waypoints is not None:
187+
if optimize_waypoints is not None and not dry_run:
188188
if len(coordinates) <= 3:
189-
warnings.warn("Less than 4 coordinates, nothing to optimize!")
189+
warnings.warn("Less than 4 coordinates, nothing to optimize!", UserWarning)
190+
elif options:
191+
warnings.warn("Options are not compatible with optimization.", UserWarning)
192+
elif preference == 'shortest':
193+
warnings.warn("Shortest is not compatible with optimization.", UserWarning)
190194
else:
191195
coordinates = _optimize_waypoint_order(client, coordinates, profile)
192196

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def readme():
2222

2323
setup(
2424
name='openrouteservice',
25-
version='2.2.1',
25+
version='2.2.2',
2626
description='Python client for requests to openrouteservice API services',
2727
long_description=readme(),
2828
long_description_content_type='text/x-rst',

test/test_directions.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,67 @@ def test_optimized_waypoints(self):
7474
# Too exhausting to really test this
7575
with self.assertRaises(openrouteservice.exceptions.ApiError):
7676
resp = self.client.directions(**query)
77+
78+
@responses.activate
79+
def test_optimize_warnings(self):
80+
query = deepcopy(self.valid_query)
81+
query['optimize_waypoints'] = True
82+
83+
# Test Coordinates
84+
85+
responses.add(responses.POST,
86+
'https://api.openrouteservice.org/v2/directions/{}/geojson'.format(query['profile']),
87+
json=query,
88+
status=200,
89+
content_type='application/json')
90+
91+
with warnings.catch_warnings(record=True) as w:
92+
# Cause all warnings to always be triggered.
93+
warnings.simplefilter("always")
94+
95+
resp = self.client.directions(**query)
96+
97+
assert len(w) == 1
98+
assert issubclass(w[-1].category, UserWarning)
99+
assert "4 coordinates" in str(w[-1].message)
100+
101+
# Test Options
102+
103+
query['coordinates'] = [[8.688641, 49.420577], [8.680916, 49.415776],[8.688641, 49.420577], [8.680916, 49.415776]]
104+
105+
responses.add(responses.POST,
106+
'https://api.openrouteservice.org/v2/directions/{}/geojson'.format(query['profile']),
107+
json=query,
108+
status=200,
109+
content_type='application/json')
110+
111+
with warnings.catch_warnings(record=True) as w:
112+
# Cause all warnings to always be triggered.
113+
warnings.simplefilter("always")
114+
115+
resp = self.client.directions(**query)
116+
117+
assert len(w) == 1
118+
assert issubclass(w[-1].category, UserWarning)
119+
assert "Options" in str(w[-1].message)
120+
121+
# Test Preference
122+
123+
query['options'] = None
124+
query['preference'] = 'shortest'
125+
126+
responses.add(responses.POST,
127+
'https://api.openrouteservice.org/v2/directions/{}/geojson'.format(query['profile']),
128+
json=query,
129+
status=200,
130+
content_type='application/json')
131+
132+
with warnings.catch_warnings(record=True) as w:
133+
# Cause all warnings to always be triggered.
134+
warnings.simplefilter("always")
135+
136+
resp = self.client.directions(**query)
137+
138+
assert len(w) == 1
139+
assert issubclass(w[-1].category, UserWarning)
140+
assert "Shortest" in str(w[-1].message)

0 commit comments

Comments
 (0)