|
27 | 27 | ***************************************************************************/ |
28 | 28 | """ |
29 | 29 |
|
| 30 | +import json |
30 | 31 | import os |
31 | 32 | from datetime import datetime |
32 | 33 | from typing import Optional |
|
80 | 81 | PROFILES, |
81 | 82 | PREFERENCES, |
82 | 83 | ) |
83 | | -from ORStools.utils import maptools, configmanager, transform, gui |
| 84 | +from ORStools.utils import maptools, configmanager, transform, gui, exceptions |
84 | 85 | from .ORStoolsDialogConfig import ORStoolsDialogConfigMain |
85 | 86 |
|
86 | 87 | MAIN_WIDGET, _ = uic.loadUiType(gui.GuiUtils.get_ui_file_path("ORStoolsDialogUI.ui")) |
@@ -249,37 +250,48 @@ def run_gui_control(self) -> None: |
249 | 250 | """Slot function for OK button of main dialog.""" |
250 | 251 | if self.dlg.routing_fromline_list.count() == 0: |
251 | 252 | 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 |
283 | 295 |
|
284 | 296 | def tr(self, string: str) -> str: |
285 | 297 | return QCoreApplication.translate(str(self.__class__.__name__), string) |
@@ -453,6 +465,7 @@ def _clear_listwidget(self) -> None: |
453 | 465 | # Remove blue lines (rubber band) |
454 | 466 | if self.rubber_band: |
455 | 467 | self.rubber_band.reset() |
| 468 | + del self.line_tool |
456 | 469 | self.line_tool = maptools.LineTool(self) |
457 | 470 |
|
458 | 471 | def _linetool_annotate_point( |
|
0 commit comments