Files
geodonkey-qgis-plugin/geodonkey/gui/tools/route.py
T
2026-12-01 19:59:12 +01:00

114 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
from typing import Any, Dict, Optional
from qgis.PyQt.QtWidgets import (
QCheckBox,
QComboBox,
QFormLayout,
QWidget,
)
from qgis.core import Qgis
from ... import config, results
from ...i18n import later, tr
from ..maptab import MapTab
MODES = (
("driving", later("by car")),
("cycling", later("by bike")),
("walking", later("on foot")),
)
class RoutePage(MapTab):
ALGORITHM = "geodonkey:route"
NAME = later("Route")
ICON = "route.svg"
WANTED = config.ROUTE_MAX_POINTS
FREE = True
PROMPTS = (later("Click the start point on the map."),
later("Click the destination – or further stopovers."),
later("Another stopover, or right-click to finish."))
PICK_TEXT = later("Pick the route on the map")
TASK_TITLE = later("GeoDonkey: route")
LAYER_TITLE = later("Route")
STYLE_KEY = "route"
def options(self) -> Optional[QWidget]:
box = QWidget()
form = QFormLayout(box)
form.setContentsMargins(0, 0, 0, 0)
form.setSpacing(6)
self.mode = QComboBox()
for key, label in MODES:
self.mode.addItem(tr(label), key)
form.addRow(tr("Travel:"), self.mode)
self.optimize = QCheckBox(tr("Find the shortest order"))
self.optimize.toggled.connect(self.update_cost)
form.addRow("", self.optimize)
self.steps = QCheckBox(tr("Include turn-by-turn directions"))
form.addRow("", self.steps)
return box
def controls(self):
return (self.mode, self.optimize, self.steps)
def price(self) -> str:
key = "route_optimized" if self.optimize.isChecked() else "route"
return tr("{coins} DC per run · 2 to {limit} points").format(
coins=config.COIN_COSTS.get(key, 0),
limit=config.ROUTE_MAX_POINTS)
def run(self) -> None:
if len(self.points) < config.ROUTE_MIN_POINTS:
self.panel.say(tr("A route needs at least {count} points.").format(
count=config.ROUTE_MIN_POINTS), Qgis.MessageLevel.Warning)
return
stops = list(self.points)
mode = self.mode.currentData()
optimize = self.optimize.isChecked()
steps = self.steps.isChecked()
client = self.panel.api()
def work(feedback):
return client.route(stops, mode=mode, optimize=optimize,
steps=steps, feedback=feedback)
self.start(work, tr("Calculating the route…"))
def clear_result(self) -> None:
shapes = self.shapes()
if shapes is not None:
shapes.clear()
def accept(self, payload: Dict[str, Any]) -> None:
self.payload = payload
legs, stops = results.route_rows(self.payload)
if not legs:
self.keep_button.setEnabled(False)
self.panel.say(tr("The service returned no route."),
Qgis.MessageLevel.Warning)
return
shapes = self.shapes()
if shapes is not None:
shapes.show([(row.get("geometry"), row["attrs"].get("color"))
for row in legs])
self.keep_button.setEnabled(True)
summary = results.route_summary(self.payload)
self.summary.setText("%s<br>%s" % (
summary,
tr("{legs} leg(s) · {stops} stop(s)").format(
legs=len(legs), stops=len(stops))))
self.panel.say(summary, Qgis.MessageLevel.Success)
def rows(self):
legs, stops = results.route_rows(self.payload)
return list(legs) + list(stops)