from __future__ import annotations from typing import Any, Dict from qgis.core import ( QgsProcessing, QgsProcessingException, QgsProcessingParameterEnum, QgsProcessingParameterFeatureSink, QgsProcessingParameterPoint, QgsProcessingParameterString, QgsWkbTypes, ) from .. import config, results, style from ..errors import GeoDonkeyError from ..i18n import tr from .base import GeoDonkeyAlgorithm, attribution MEASURE_MINUTES = 0 MEASURE_KILOMETRES = 1 class IsochroneAlgorithm(GeoDonkeyAlgorithm): ICON = "isochrone.svg" GROUP = "Routing" GROUP_ID = "routing" COST_KEY = "isochrone" POINT = "POINT" MODE = "MODE" MEASURE = "MEASURE" VALUES = "VALUES" OUTPUT = "OUTPUT" def createInstance(self): # noqa: N802 return IsochroneAlgorithm() def name(self) -> str: return "isochrone" def displayName(self) -> str: # noqa: N802 return tr("Isochrone (reachable area)") def shortHelpString(self) -> str: # noqa: N802 return tr( "
Calculates the area that can be reached from a point within a " "given travel time or distance, using the road network – not a " "circle around the point.
" "Contours is a comma separated list, for example "
"5,10,15. Up to {rings} rings are calculated in a "
"single call and cost the same as one: travel time from {min_min} "
"to {max_min} minutes, distance from {min_km} to {max_km} "
"kilometres.
The rings are returned from the largest to the smallest, so "
"that drawing them in that order keeps all of them visible. The "
"field label holds the ring as readable text, for "
"example 10 min.
Data © OpenStreetMap contributors, ODbL.
" ).format(rings=config.ISOCHRONE_MAX_RINGS, min_min=int(config.ISOCHRONE_MIN_MINUTES), max_min=int(config.ISOCHRONE_MAX_MINUTES), min_km=config.ISOCHRONE_MIN_KILOMETRES, max_km=int(config.ISOCHRONE_MAX_KILOMETRES)) + self.cost_note() def initAlgorithm(self, configuration=None): # noqa: N802 self.addParameter(QgsProcessingParameterPoint( self.POINT, tr("Start point"))) self.addParameter(QgsProcessingParameterEnum( self.MODE, tr("Travel mode"), options=[tr("Walking"), tr("Driving"), tr("Cycling")], defaultValue=0)) self.addParameter(QgsProcessingParameterEnum( self.MEASURE, tr("Measure"), options=[tr("Travel time (minutes)"), tr("Distance (kilometres)")], defaultValue=MEASURE_MINUTES)) self.addParameter(QgsProcessingParameterString( self.VALUES, tr("Contours (comma separated)"), defaultValue=config.ISOCHRONE_DEFAULT_MINUTES)) self.addParameter(QgsProcessingParameterFeatureSink( self.OUTPUT, tr("Reachable area"), type=QgsProcessing.SourceType.TypeVectorPolygon)) def processAlgorithm(self, parameters, context, feedback): # noqa: N802 lon, lat = self.wgs84_point(parameters, self.POINT, context) mode = config.TRAVEL_MODES[self.parameterAsEnum(parameters, self.MODE, context)] measure = self.parameterAsEnum(parameters, self.MEASURE, context) values = (self.parameterAsString(parameters, self.VALUES, context) or "").strip() api = self.client(feedback) feedback.setProgress(5) try: if measure == MEASURE_KILOMETRES: payload = api.isochrone(lon, lat, mode=mode, kilometres=values, feedback=feedback) else: payload = api.isochrone(lon, lat, mode=mode, minutes=values, feedback=feedback) except GeoDonkeyError as exc: self.fail(exc, feedback) if feedback.isCanceled(): return {} feedback.setProgress(60) self.report(api, feedback) rows = results.isochrone_rows(payload) if not rows: feedback.pushWarning(tr("No reachable area was returned for this point.")) attribution(results.metadata(payload), feedback) sink, dest = self.make_sink(parameters, context, self.OUTPUT, results.ISOCHRONE_FIELDS, QgsWkbTypes.Type.MultiPolygon) if sink is None: raise QgsProcessingException(tr("The output could not be created.")) written = self.write(sink, results.ISOCHRONE_FIELDS, rows, feedback) self.style(context, dest, style.isochrone_plan( [row.get("attrs") or {} for row in rows])) feedback.pushInfo(tr("{count} rings written.").format(count=written)) feedback.setProgress(100) out: Dict[str, Any] = {self.OUTPUT: dest} return out