GeoDonkey for QGIS 1.12
This commit is contained in:
commit
20ab32da30
63 files changed
+11157
No files matched your search
@@ -0,0 +1,181 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List, Tuple
|
||||
|
||||
from qgis.core import (
|
||||
QgsProcessing,
|
||||
QgsProcessingException,
|
||||
QgsProcessingParameterBoolean,
|
||||
QgsProcessingParameterEnum,
|
||||
QgsProcessingParameterFeatureSink,
|
||||
QgsProcessingParameterFeatureSource,
|
||||
QgsProcessingParameterPoint,
|
||||
QgsWkbTypes,
|
||||
)
|
||||
|
||||
from .. import config, results, settings, style
|
||||
from ..errors import GeoDonkeyError
|
||||
from ..i18n import tr
|
||||
from .base import GeoDonkeyAlgorithm, attribution
|
||||
|
||||
LANGUAGE_OPTIONS = ("", ) + tuple(config.ROUTE_LANGUAGES)
|
||||
|
||||
|
||||
class RouteAlgorithm(GeoDonkeyAlgorithm):
|
||||
ICON = "route.svg"
|
||||
GROUP = "Routing"
|
||||
GROUP_ID = "routing"
|
||||
COST_KEY = "route"
|
||||
|
||||
START = "START"
|
||||
END = "END"
|
||||
VIA = "VIA"
|
||||
MODE = "MODE"
|
||||
OPTIMIZE = "OPTIMIZE"
|
||||
STEPS = "STEPS"
|
||||
LANGUAGE = "LANGUAGE"
|
||||
OUTPUT_ROUTE = "OUTPUT_ROUTE"
|
||||
OUTPUT_POINTS = "OUTPUT_POINTS"
|
||||
|
||||
def createInstance(self): # noqa: N802
|
||||
return RouteAlgorithm()
|
||||
|
||||
def name(self) -> str:
|
||||
return "route"
|
||||
|
||||
def displayName(self) -> str: # noqa: N802
|
||||
return tr("Route")
|
||||
|
||||
def shortHelpString(self) -> str: # noqa: N802
|
||||
return tr(
|
||||
"<p>Calculates a route from the start through any stopovers to the "
|
||||
"destination and returns two layers: the driven legs as lines and "
|
||||
"the stops as points.</p>"
|
||||
"<p><b>Stopovers</b> is an optional point layer. Its features are "
|
||||
"visited in the order of the layer. Start, stopovers and "
|
||||
"destination together may be at most {max_points} points.</p>"
|
||||
"<p><b>Optimise stopover order</b> searches for the shortest "
|
||||
"order of the stopovers; start and destination stay where they "
|
||||
"are. The field <code>stop</code> then holds the driven order and "
|
||||
"<code>input</code> the order as entered.</p>"
|
||||
"<p><b>Turn-by-turn directions</b> adds the instructions of each "
|
||||
"leg to the field <code>directions</code>.</p>"
|
||||
"<p>Maximum total length: {walking} km walking, {cycling} km "
|
||||
"cycling, {driving} km driving.</p>"
|
||||
"<p>Data © OpenStreetMap contributors, ODbL.</p>"
|
||||
"<p><b>A run costs {plain} DonkeyCoins, {optimized} with optimised "
|
||||
"stopover order</b>, once the {free} free calls of the current "
|
||||
"hour are used up. The API key is taken from the plugin settings "
|
||||
"(Web ▸ GeoDonkey ▸ GeoDonkey settings).</p>"
|
||||
).format(max_points=config.ROUTE_MAX_POINTS,
|
||||
walking=int(config.ROUTE_MAX_KILOMETRES["walking"]),
|
||||
cycling=int(config.ROUTE_MAX_KILOMETRES["cycling"]),
|
||||
driving=int(config.ROUTE_MAX_KILOMETRES["driving"]),
|
||||
plain=config.COIN_COSTS.get("route", 2),
|
||||
optimized=config.COIN_COSTS.get("route_optimized", 3),
|
||||
free=config.FREE_CALLS_PER_HOUR)
|
||||
|
||||
def initAlgorithm(self, configuration=None): # noqa: N802
|
||||
self.addParameter(QgsProcessingParameterPoint(
|
||||
self.START, tr("Start")))
|
||||
|
||||
self.addParameter(QgsProcessingParameterPoint(
|
||||
self.END, tr("Destination")))
|
||||
|
||||
self.addParameter(QgsProcessingParameterFeatureSource(
|
||||
self.VIA, tr("Stopovers (optional point layer)"),
|
||||
types=[QgsProcessing.SourceType.TypeVectorPoint], optional=True))
|
||||
|
||||
self.addParameter(QgsProcessingParameterEnum(
|
||||
self.MODE, tr("Travel mode"),
|
||||
options=[tr("Walking"), tr("Driving"), tr("Cycling")],
|
||||
defaultValue=1))
|
||||
|
||||
self.addParameter(QgsProcessingParameterBoolean(
|
||||
self.OPTIMIZE, tr("Optimise stopover order"), defaultValue=False))
|
||||
|
||||
self.addParameter(QgsProcessingParameterBoolean(
|
||||
self.STEPS, tr("Turn-by-turn directions"), defaultValue=False))
|
||||
|
||||
self.addParameter(QgsProcessingParameterEnum(
|
||||
self.LANGUAGE, tr("Language of the directions"),
|
||||
options=[tr("Follow QGIS"), tr("German"), tr("English"),
|
||||
tr("French"), tr("Spanish")],
|
||||
defaultValue=0))
|
||||
|
||||
self.addParameter(QgsProcessingParameterFeatureSink(
|
||||
self.OUTPUT_ROUTE, tr("Route"),
|
||||
type=QgsProcessing.SourceType.TypeVectorLine))
|
||||
|
||||
self.addParameter(QgsProcessingParameterFeatureSink(
|
||||
self.OUTPUT_POINTS, tr("Stops"),
|
||||
type=QgsProcessing.SourceType.TypeVectorPoint))
|
||||
|
||||
def processAlgorithm(self, parameters, context, feedback): # noqa: N802
|
||||
stops = self._stops(parameters, context, feedback)
|
||||
mode = config.TRAVEL_MODES[self.parameterAsEnum(parameters, self.MODE, context)]
|
||||
optimize = self.parameterAsBool(parameters, self.OPTIMIZE, context)
|
||||
steps = self.parameterAsBool(parameters, self.STEPS, context)
|
||||
language = LANGUAGE_OPTIONS[
|
||||
self.parameterAsEnum(parameters, self.LANGUAGE, context)]
|
||||
if not language:
|
||||
language = settings.language()
|
||||
|
||||
api = self.client(feedback)
|
||||
feedback.pushInfo(tr("{count} points · {mode}").format(
|
||||
count=len(stops), mode=mode))
|
||||
feedback.setProgress(5)
|
||||
try:
|
||||
payload = api.route(stops, mode=mode, optimize=optimize, steps=steps,
|
||||
language=language, feedback=feedback)
|
||||
except GeoDonkeyError as exc:
|
||||
self.fail(exc, feedback)
|
||||
|
||||
if feedback.isCanceled():
|
||||
return {}
|
||||
|
||||
feedback.setProgress(60)
|
||||
self.report(api, feedback)
|
||||
legs, points_rows = results.route_rows(payload)
|
||||
feedback.pushInfo(results.route_summary(payload))
|
||||
attribution(results.metadata(payload), feedback)
|
||||
|
||||
out: Dict[str, Any] = {}
|
||||
|
||||
sink, dest = self.make_sink(parameters, context, self.OUTPUT_ROUTE,
|
||||
results.ROUTE_LEG_FIELDS,
|
||||
QgsWkbTypes.Type.MultiLineString)
|
||||
if sink is None:
|
||||
raise QgsProcessingException(tr("The output could not be created."))
|
||||
self.write(sink, results.ROUTE_LEG_FIELDS, legs, feedback)
|
||||
self.style(context, dest, style.route_leg_plan(
|
||||
[row.get("attrs") or {} for row in legs]))
|
||||
out[self.OUTPUT_ROUTE] = dest
|
||||
feedback.setProgress(80)
|
||||
|
||||
sink, dest = self.make_sink(parameters, context, self.OUTPUT_POINTS,
|
||||
results.ROUTE_STOP_FIELDS,
|
||||
QgsWkbTypes.Type.Point)
|
||||
if sink is not None:
|
||||
self.write(sink, results.ROUTE_STOP_FIELDS, points_rows, feedback)
|
||||
self.style(context, dest, style.route_stop_plan(
|
||||
[row.get("attrs") or {} for row in points_rows]))
|
||||
out[self.OUTPUT_POINTS] = dest
|
||||
feedback.setProgress(100)
|
||||
return out
|
||||
|
||||
def _stops(self, parameters, context, feedback) -> List[Tuple[float, float]]:
|
||||
start = self.wgs84_point(parameters, self.START, context)
|
||||
end = self.wgs84_point(parameters, self.END, context)
|
||||
|
||||
via: List[Tuple[float, float]] = []
|
||||
source = self.parameterAsSource(parameters, self.VIA, context)
|
||||
if source is not None:
|
||||
via, _features = self.source_points(source, context, feedback)
|
||||
|
||||
stops = [start] + via + [end]
|
||||
if len(stops) > config.ROUTE_MAX_POINTS:
|
||||
raise QgsProcessingException(tr(
|
||||
"{count} points given, at most {limit} are allowed.").format(
|
||||
count=len(stops), limit=config.ROUTE_MAX_POINTS))
|
||||
return stops
|
||||
Reference in new issue
Block a user