from __future__ import annotations
from typing import Any, Dict
from qgis.core import (
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterNumber,
QgsProcessingParameterPoint,
QgsWkbTypes,
)
from .. import config, results, style
from ..errors import GeoDonkeyError
from ..i18n import tr
from .base import GeoDonkeyAlgorithm, attribution
class ElevationProfileAlgorithm(GeoDonkeyAlgorithm):
ICON = "profile.svg"
GROUP = "Elevation"
GROUP_ID = "elevation"
COST_KEY = "elevation_profile"
START = "START"
END = "END"
MAX_POINTS = "MAX_POINTS"
STEP = "STEP"
OUTPUT = "OUTPUT"
OUTPUT_SEGMENTS = "OUTPUT_SEGMENTS"
def createInstance(self): # noqa: N802
return ElevationProfileAlgorithm()
def name(self) -> str:
return "elevationprofile"
def displayName(self) -> str: # noqa: N802
return tr("Elevation profile")
def shortHelpString(self) -> str: # noqa: N802
return tr(
"
Samples the terrain along the straight line between two points "
"and returns one point per sample, carrying the distance from the "
"start and the elevation above sea level.
"
"The service places the samples on the centres of its elevation "
"tiles. Maximum points is the upper limit (at most "
"{max_points}), Minimum spacing the smallest distance "
"between two samples (at least {min_step} m).
"
"Samples without elevation data keep their place in the list "
"and have an empty elevation. Dropping them would "
"tear the distance axis apart.
"
"Elevation data is available for Germany only. Points "
"outside are rejected before the call is sent.
"
"The price follows the quantity: {coins} DonkeyCoins per "
"started {batch} points of Maximum points – {max_points} "
"points therefore cost {full} DC, once the {free} free calls of "
"the current hour are used up.
"
"Elevation data by the surveying authorities of the German "
"federal states; the exact source of every sample is in the field "
"source.
"
).format(max_points=config.ELEVATION_MAX_POINTS,
min_step=config.ELEVATION_MIN_STEP_METRES,
batch=config.ELEVATION_BILLING_BATCH,
coins=config.COIN_COSTS.get("elevation_profile", 2),
full=config.elevation_coins(config.ELEVATION_MAX_POINTS),
free=config.FREE_CALLS_PER_HOUR)
def initAlgorithm(self, configuration=None): # noqa: N802
self.addParameter(QgsProcessingParameterPoint(
self.START, tr("Start point")))
self.addParameter(QgsProcessingParameterPoint(
self.END, tr("End point")))
self.addParameter(QgsProcessingParameterNumber(
self.MAX_POINTS, tr("Maximum points"),
type=QgsProcessingParameterNumber.Type.Integer,
minValue=2, maxValue=config.ELEVATION_MAX_POINTS,
defaultValue=config.ELEVATION_MAX_POINTS))
self.addParameter(QgsProcessingParameterNumber(
self.STEP, tr("Minimum spacing (metres)"),
type=QgsProcessingParameterNumber.Type.Double,
minValue=config.ELEVATION_MIN_STEP_METRES,
defaultValue=config.ELEVATION_DEFAULT_STEP_METRES))
self.addParameter(QgsProcessingParameterFeatureSink(
self.OUTPUT, tr("Elevation profile"),
type=QgsProcessing.SourceType.TypeVectorPoint))
self.addParameter(QgsProcessingParameterFeatureSink(
self.OUTPUT_SEGMENTS, tr("Sections by gradient (optional)"),
type=QgsProcessing.SourceType.TypeVectorLine,
optional=True, createByDefault=False))
def processAlgorithm(self, parameters, context, feedback): # noqa: N802
start = self.wgs84_point(parameters, self.START, context)
end = self.wgs84_point(parameters, self.END, context)
max_points = self.parameterAsInt(parameters, self.MAX_POINTS, context)
step = self.parameterAsDouble(parameters, self.STEP, context)
api = self.client(feedback)
feedback.pushInfo(tr("Up to {count} points · {coins} DC").format(
count=max_points,
coins=config.elevation_coins(max_points, "elevation_profile")))
feedback.setProgress(5)
try:
payload = api.elevation_profile(start, end, max_points=max_points,
step=step, feedback=feedback)
except GeoDonkeyError as exc:
self.fail(exc, feedback)
if feedback.isCanceled():
return {}
feedback.setProgress(60)
self.report(api, feedback)
rows = results.profile_rows(payload)
feedback.pushInfo(results.profile_summary(payload))
attribution(results.metadata(payload), feedback)
covered = results.metadata(payload).get("covered")
if covered is not None and rows and covered < len(rows):
feedback.pushWarning(tr(
"{missing} of {count} points have no elevation data.").format(
missing=len(rows) - int(covered), count=len(rows)))
sink, dest = self.make_sink(parameters, context, self.OUTPUT,
results.PROFILE_FIELDS, QgsWkbTypes.Type.Point)
if sink is None:
raise QgsProcessingException(tr("The output could not be created."))
self.write(sink, results.PROFILE_FIELDS, rows, feedback)
self.style(context, dest, style.profile_point_plan())
out: Dict[str, Any] = {self.OUTPUT: dest}
feedback.setProgress(85)
if parameters.get(self.OUTPUT_SEGMENTS):
sink, dest = self.make_sink(parameters, context, self.OUTPUT_SEGMENTS,
results.PROFILE_SEGMENT_FIELDS,
QgsWkbTypes.Type.LineString)
if sink is not None:
self.write(sink, results.PROFILE_SEGMENT_FIELDS,
results.profile_segment_rows(payload), feedback)
self.style(context, dest, style.profile_segment_plan())
out[self.OUTPUT_SEGMENTS] = dest
feedback.setProgress(100)
return out