Skip to content

Commit ca9a95f

Browse files
merydiankoebi
andauthored
fix: improve error handling with radius error (#327)
Co-authored-by: Jakob Schnell <[email protected]>
1 parent d1c234c commit ca9a95f

File tree

2 files changed

+53
-39
lines changed

2 files changed

+53
-39
lines changed

ORStools/gui/ORStoolsDialog.py

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
***************************************************************************/
2828
"""
2929

30+
import json
3031
import os
3132
from datetime import datetime
3233
from typing import Optional
@@ -80,7 +81,7 @@
8081
PROFILES,
8182
PREFERENCES,
8283
)
83-
from ORStools.utils import maptools, configmanager, transform, gui
84+
from ORStools.utils import maptools, configmanager, transform, gui, exceptions
8485
from .ORStoolsDialogConfig import ORStoolsDialogConfigMain
8586

8687
MAIN_WIDGET, _ = uic.loadUiType(gui.GuiUtils.get_ui_file_path("ORStoolsDialogUI.ui"))
@@ -249,37 +250,48 @@ def run_gui_control(self) -> None:
249250
"""Slot function for OK button of main dialog."""
250251
if self.dlg.routing_fromline_list.count() == 0:
251252
return
252-
basepath = os.path.dirname(__file__)
253-
254-
# add ors svg path
255-
my_new_path = os.path.join(basepath, "img/svg")
256-
svg_paths = QgsSettings().value("svg/searchPathsForSVG") or []
257-
if my_new_path not in svg_paths:
258-
svg_paths.append(my_new_path)
259-
QgsSettings().setValue("svg/searchPathsForSVG", svg_paths)
260-
261-
# Associate annotations with map layer, so they get deleted when layer is deleted
262-
for annotation in self.dlg.annotations:
263-
# Has the potential to be pretty cool: instead of deleting, associate with mapLayer
264-
# , you can change order after optimization
265-
# Then in theory, when the layer is remove, the annotation is removed as well
266-
# Doesn't work though, the annotations are still there when project is re-opened
267-
# annotation.setMapLayer(layer_out)
268-
self.project.annotationManager().removeAnnotation(annotation)
269-
self.dlg.annotations = []
270-
self.dlg.rubber_band.reset()
271-
272-
layer_out = route_as_layer(self.dlg)
273-
274-
# style output layer
275-
qml_path = os.path.join(basepath, "linestyle.qml")
276-
layer_out.loadNamedStyle(qml_path, True)
277-
layer_out.triggerRepaint()
278-
279-
self.project.addMapLayer(layer_out)
280-
281-
self.dlg._clear_listwidget()
282-
self.dlg.line_tool = maptools.LineTool(self.dlg)
253+
254+
try:
255+
basepath = os.path.dirname(__file__)
256+
257+
layer_out = route_as_layer(self.dlg)
258+
259+
# style output layer
260+
qml_path = os.path.join(basepath, "linestyle.qml")
261+
layer_out.loadNamedStyle(qml_path, True)
262+
layer_out.triggerRepaint()
263+
264+
self.project.addMapLayer(layer_out)
265+
266+
# add ors svg path
267+
my_new_path = os.path.join(basepath, "img/svg")
268+
svg_paths = QgsSettings().value("svg/searchPathsForSVG") or []
269+
if my_new_path not in svg_paths:
270+
svg_paths.append(my_new_path)
271+
QgsSettings().setValue("svg/searchPathsForSVG", svg_paths)
272+
273+
# Associate annotations with map layer, so they get deleted when layer is deleted
274+
for annotation in self.dlg.annotations:
275+
# Has the potential to be pretty cool: instead of deleting, associate with mapLayer
276+
# , you can change order after optimization
277+
# Then in theory, when the layer is remove, the annotation is removed as well
278+
# Doesn't work though, the annotations are still there when project is re-opened
279+
# annotation.setMapLayer(layer_out)
280+
self.project.annotationManager().removeAnnotation(annotation)
281+
282+
self.dlg.annotations = []
283+
self.dlg.rubber_band.reset()
284+
285+
self.dlg._clear_listwidget()
286+
self.dlg.line_tool = maptools.LineTool(self.dlg)
287+
288+
except exceptions.ApiError as e:
289+
# Error thrown by ORStools/common/client.py, line 243, in _check_status
290+
parsed = json.loads(e.message)
291+
error_code = int(parsed["error"]["code"])
292+
if error_code == 2010:
293+
maptools.LineTool(self.dlg).radius_message_box(e)
294+
return
283295

284296
def tr(self, string: str) -> str:
285297
return QCoreApplication.translate(str(self.__class__.__name__), string)
@@ -453,6 +465,7 @@ def _clear_listwidget(self) -> None:
453465
# Remove blue lines (rubber band)
454466
if self.rubber_band:
455467
self.rubber_band.reset()
468+
del self.line_tool
456469
self.line_tool = maptools.LineTool(self)
457470

458471
def _linetool_annotate_point(

ORStools/utils/maptools.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def canvasReleaseEvent(self, event: QEvent) -> None:
216216
item = f"Point {i}:{coords}"
217217
self.dlg.routing_fromline_list.addItem(item)
218218
self.dlg._reindex_list_items()
219-
self.radius_message_box()
219+
self.radius_message_box(e)
220220
self.mouseMoved.connect(lambda pos: self.change_cursor_on_hover(pos))
221221
else:
222222
raise e
@@ -250,7 +250,7 @@ def canvasReleaseEvent(self, event: QEvent) -> None:
250250
self.dlg._reindex_list_items()
251251
self.create_rubber_band()
252252

253-
self.radius_message_box()
253+
self.radius_message_box(e)
254254
else:
255255
raise e
256256
except Exception as e:
@@ -315,13 +315,14 @@ def get_error_code(self, e: QEvent) -> int:
315315
error_dict = json.loads(json_str)
316316
return error_dict["error"]["code"]
317317

318-
def radius_message_box(self) -> None:
318+
def radius_message_box(self, e) -> None:
319+
parsed = json.loads(e.message)
320+
error_message = parsed["error"]["message"]
319321
self.dlg._iface.messageBar().pushMessage(
320322
self.tr("Please use a different point"),
321-
self.tr("""Could not find routable point within a radius of 350.0 meters of specified coordinate.
322-
Use a different point closer to a road."""),
323+
self.tr(f"""{error_message} Use a different point closer to a road."""),
323324
level=Qgis.MessageLevel.Warning,
324-
duration=3,
325+
duration=5,
325326
)
326327

327328
def api_key_message_bar(self) -> None:
@@ -340,7 +341,7 @@ def _toggle_preview(self) -> None:
340341
except ApiError as e:
341342
self.dlg.toggle_preview.setChecked(state)
342343
if self.get_error_code(e) == 2010:
343-
self.radius_message_box()
344+
self.radius_message_box(e)
344345
else:
345346
raise e
346347
except Exception as e:

0 commit comments

Comments
 (0)