210 lines
9.2 KiB
Python
210 lines
9.2 KiB
Python
from __future__ import annotations
|
||
|
||
from typing import Any, Dict
|
||
|
||
from qgis.core import (
|
||
QgsProcessing,
|
||
QgsProcessingException,
|
||
QgsProcessingParameterBoolean,
|
||
QgsProcessingParameterFeatureSink,
|
||
QgsProcessingParameterNumber,
|
||
QgsProcessingParameterPoint,
|
||
QgsWkbTypes,
|
||
)
|
||
|
||
from .. import config, results, style
|
||
from ..errors import GeoDonkeyError
|
||
from ..i18n import tr
|
||
from .base import GeoDonkeyAlgorithm, attribution
|
||
|
||
|
||
class LineOfSightAlgorithm(GeoDonkeyAlgorithm):
|
||
ICON = "lineofsight.svg"
|
||
GROUP = "Elevation"
|
||
GROUP_ID = "elevation"
|
||
COST_KEY = "line_of_sight"
|
||
|
||
FROM = "FROM"
|
||
TO = "TO"
|
||
HEIGHT_A = "HEIGHT_A"
|
||
HEIGHT_B = "HEIGHT_B"
|
||
CURVATURE = "CURVATURE"
|
||
K = "K"
|
||
STEP = "STEP"
|
||
MAX_POINTS = "MAX_POINTS"
|
||
OUTPUT_LINE = "OUTPUT_LINE"
|
||
OUTPUT_OBSTACLES = "OUTPUT_OBSTACLES"
|
||
OUTPUT_SAMPLES = "OUTPUT_SAMPLES"
|
||
OUTPUT_SEGMENTS = "OUTPUT_SEGMENTS"
|
||
|
||
def createInstance(self): # noqa: N802
|
||
return LineOfSightAlgorithm()
|
||
|
||
def name(self) -> str:
|
||
return "lineofsight"
|
||
|
||
def displayName(self) -> str: # noqa: N802
|
||
return tr("Line of sight")
|
||
|
||
def shortHelpString(self) -> str: # noqa: N802
|
||
return tr(
|
||
"<p>Checks whether there is a clear line of sight between two "
|
||
"points, using the terrain model including earth curvature and "
|
||
"atmospheric refraction.</p>"
|
||
"<p><b>Height above ground</b> is the height of the eye, the mast "
|
||
"or the roof edge at each end, from 0 to {max_height} m. If the "
|
||
"sight is blocked, the fields <code>raise_a</code> and "
|
||
"<code>raise_b</code> say how much higher one end would have to "
|
||
"be.</p>"
|
||
"<p>Three outputs: the <b>sight line</b> with the result in its "
|
||
"attributes, the <b>obstacles</b> as points at the spot where the "
|
||
"terrain rises highest above the ray, and optionally <b>every "
|
||
"sample</b> with terrain, ray and clearance – the basis for a "
|
||
"profile chart of your own.</p>"
|
||
"<p><b>Refraction factor k</b> is {k_default} for the standard "
|
||
"atmosphere. <code>k = 1</code> applies earth curvature without "
|
||
"refraction; switching curvature off applies neither.</p>"
|
||
"<p><b>Elevation data is available for Germany only.</b></p>"
|
||
"<p><b>Up to {free_metres} m of straight-line distance a run is "
|
||
"free</b>, above that it costs {coins} DonkeyCoins. This endpoint "
|
||
"has its own allowance of {free_calls} calls per hour, replacing "
|
||
"the shared one – after that it requires an API key, as the only "
|
||
"tool in this plugin.</p>"
|
||
"<p>Elevation data by the surveying authorities of the German "
|
||
"federal states.</p>"
|
||
).format(max_height=int(config.LOS_MAX_HEIGHT_METRES),
|
||
k_default=round(config.LOS_REFRACTION_K, 3),
|
||
free_metres=int(config.LOS_FREE_METRES),
|
||
coins=config.COIN_COSTS.get("line_of_sight", 2),
|
||
free_calls=config.ENDPOINT_FREE_CALLS.get("line_of_sight", 5))
|
||
|
||
def initAlgorithm(self, configuration=None): # noqa: N802
|
||
self.addParameter(QgsProcessingParameterPoint(
|
||
self.FROM, tr("Observer point")))
|
||
|
||
self.addParameter(QgsProcessingParameterPoint(
|
||
self.TO, tr("Target point")))
|
||
|
||
self.addParameter(QgsProcessingParameterNumber(
|
||
self.HEIGHT_A, tr("Height above ground at the observer (metres)"),
|
||
type=QgsProcessingParameterNumber.Type.Double,
|
||
minValue=0.0, maxValue=config.LOS_MAX_HEIGHT_METRES,
|
||
defaultValue=config.LOS_DEFAULT_HEIGHT_METRES))
|
||
|
||
self.addParameter(QgsProcessingParameterNumber(
|
||
self.HEIGHT_B, tr("Height above ground at the target (metres)"),
|
||
type=QgsProcessingParameterNumber.Type.Double,
|
||
minValue=0.0, maxValue=config.LOS_MAX_HEIGHT_METRES,
|
||
defaultValue=config.LOS_DEFAULT_HEIGHT_METRES))
|
||
|
||
self.addParameter(QgsProcessingParameterBoolean(
|
||
self.CURVATURE, tr("Account for earth curvature"), defaultValue=True))
|
||
|
||
self.addParameter(QgsProcessingParameterNumber(
|
||
self.K, tr("Refraction factor k"),
|
||
type=QgsProcessingParameterNumber.Type.Double,
|
||
minValue=0.1, maxValue=10.0,
|
||
defaultValue=config.LOS_REFRACTION_K))
|
||
|
||
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(QgsProcessingParameterNumber(
|
||
self.MAX_POINTS, tr("Maximum samples"),
|
||
type=QgsProcessingParameterNumber.Type.Integer,
|
||
minValue=2, maxValue=config.ELEVATION_MAX_POINTS,
|
||
defaultValue=config.ELEVATION_MAX_POINTS))
|
||
|
||
self.addParameter(QgsProcessingParameterFeatureSink(
|
||
self.OUTPUT_LINE, tr("Sight line"),
|
||
type=QgsProcessing.SourceType.TypeVectorLine))
|
||
|
||
self.addParameter(QgsProcessingParameterFeatureSink(
|
||
self.OUTPUT_OBSTACLES, tr("Obstacles"),
|
||
type=QgsProcessing.SourceType.TypeVectorPoint))
|
||
|
||
self.addParameter(QgsProcessingParameterFeatureSink(
|
||
self.OUTPUT_SEGMENTS, tr("Sections by visibility (optional)"),
|
||
type=QgsProcessing.SourceType.TypeVectorLine,
|
||
optional=True, createByDefault=False))
|
||
|
||
self.addParameter(QgsProcessingParameterFeatureSink(
|
||
self.OUTPUT_SAMPLES, tr("Samples (optional)"),
|
||
type=QgsProcessing.SourceType.TypeVectorPoint,
|
||
optional=True, createByDefault=False))
|
||
|
||
def processAlgorithm(self, parameters, context, feedback): # noqa: N802
|
||
start = self.wgs84_point(parameters, self.FROM, context)
|
||
end = self.wgs84_point(parameters, self.TO, context)
|
||
height_a = self.parameterAsDouble(parameters, self.HEIGHT_A, context)
|
||
height_b = self.parameterAsDouble(parameters, self.HEIGHT_B, context)
|
||
curvature = self.parameterAsBool(parameters, self.CURVATURE, context)
|
||
k = self.parameterAsDouble(parameters, self.K, context)
|
||
step = self.parameterAsDouble(parameters, self.STEP, context)
|
||
max_points = self.parameterAsInt(parameters, self.MAX_POINTS, context)
|
||
|
||
wants_segments = bool(parameters.get(self.OUTPUT_SEGMENTS))
|
||
wants_samples = bool(parameters.get(self.OUTPUT_SAMPLES)) or wants_segments
|
||
|
||
api = self.client(feedback)
|
||
feedback.setProgress(5)
|
||
try:
|
||
payload = api.line_of_sight(start, end, height_a=height_a,
|
||
height_b=height_b, curvature=curvature,
|
||
k=k, step=step, max_points=max_points,
|
||
samples=wants_samples, feedback=feedback)
|
||
except GeoDonkeyError as exc:
|
||
self.fail(exc, feedback)
|
||
|
||
if feedback.isCanceled():
|
||
return {}
|
||
|
||
feedback.setProgress(60)
|
||
self.report(api, feedback)
|
||
line_rows, obstacle_rows, sample_rows = results.sight_rows(payload, start, end)
|
||
feedback.pushInfo(results.sight_summary(payload))
|
||
attribution(results.metadata(payload), feedback)
|
||
|
||
out: Dict[str, Any] = {}
|
||
|
||
sink, dest = self.make_sink(parameters, context, self.OUTPUT_LINE,
|
||
results.SIGHT_FIELDS,
|
||
QgsWkbTypes.Type.LineString)
|
||
if sink is None:
|
||
raise QgsProcessingException(tr("The output could not be created."))
|
||
self.write(sink, results.SIGHT_FIELDS, line_rows, feedback)
|
||
self.style(context, dest, style.sight_segment_plan())
|
||
out[self.OUTPUT_LINE] = dest
|
||
feedback.setProgress(70)
|
||
|
||
sink, dest = self.make_sink(parameters, context, self.OUTPUT_OBSTACLES,
|
||
results.OBSTACLE_FIELDS, QgsWkbTypes.Type.Point)
|
||
if sink is not None:
|
||
self.write(sink, results.OBSTACLE_FIELDS, obstacle_rows, feedback)
|
||
self.style(context, dest, style.sight_obstacle_plan())
|
||
out[self.OUTPUT_OBSTACLES] = dest
|
||
feedback.setProgress(80)
|
||
|
||
if wants_segments:
|
||
sink, dest = self.make_sink(parameters, context, self.OUTPUT_SEGMENTS,
|
||
results.SIGHT_SEGMENT_FIELDS,
|
||
QgsWkbTypes.Type.LineString)
|
||
if sink is not None:
|
||
self.write(sink, results.SIGHT_SEGMENT_FIELDS,
|
||
results.sight_segment_rows(payload, start, end), feedback)
|
||
self.style(context, dest, style.sight_segment_plan())
|
||
out[self.OUTPUT_SEGMENTS] = dest
|
||
feedback.setProgress(90)
|
||
|
||
if parameters.get(self.OUTPUT_SAMPLES):
|
||
sink, dest = self.make_sink(parameters, context, self.OUTPUT_SAMPLES,
|
||
results.SAMPLE_FIELDS, QgsWkbTypes.Type.Point)
|
||
if sink is not None:
|
||
self.write(sink, results.SAMPLE_FIELDS, sample_rows, feedback)
|
||
out[self.OUTPUT_SAMPLES] = dest
|
||
feedback.setProgress(100)
|
||
return out
|