GeoDonkey for QGIS 1.12
This commit is contained in:
commit
20ab32da30
63 files changed
+11157
No files matched your search
@@ -0,0 +1,170 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from qgis.core import (
|
||||
QgsFeature,
|
||||
QgsFeatureSink,
|
||||
QgsFields,
|
||||
QgsProcessing,
|
||||
QgsProcessingException,
|
||||
QgsProcessingParameterFeatureSink,
|
||||
QgsProcessingParameterFeatureSource,
|
||||
)
|
||||
|
||||
from .. import config, points, results, style
|
||||
from ..compat import field
|
||||
from ..errors import GeoDonkeyError
|
||||
from ..i18n import tr
|
||||
from .base import GeoDonkeyAlgorithm
|
||||
|
||||
OUTSIDE = "outside"
|
||||
|
||||
|
||||
class ElevationPointsAlgorithm(GeoDonkeyAlgorithm):
|
||||
ICON = "elevation.svg"
|
||||
GROUP = "Elevation"
|
||||
GROUP_ID = "elevation"
|
||||
COST_KEY = "elevation_points"
|
||||
|
||||
INPUT = "INPUT"
|
||||
OUTPUT = "OUTPUT"
|
||||
|
||||
def createInstance(self): # noqa: N802
|
||||
return ElevationPointsAlgorithm()
|
||||
|
||||
def name(self) -> str:
|
||||
return "elevationpoints"
|
||||
|
||||
def displayName(self) -> str: # noqa: N802
|
||||
return tr("Add elevation to points")
|
||||
|
||||
def shortHelpString(self) -> str: # noqa: N802
|
||||
return tr(
|
||||
"<p>Looks up the elevation above sea level for every point of a "
|
||||
"layer and writes it into three new fields: <code>elevation</code>, "
|
||||
"<code>ele_source</code> and <code>ele_state</code>. Geometry and "
|
||||
"existing fields are kept unchanged.</p>"
|
||||
"<p>Layers with more than {max_points} points are split into "
|
||||
"several calls automatically. That does not cost extra: the price "
|
||||
"follows the number of points, not the number of calls.</p>"
|
||||
"<p><b>Elevation data is available for Germany only.</b> Points "
|
||||
"outside keep their place in the layer, get an empty elevation and "
|
||||
"<code>{outside}</code> in <code>ele_state</code> – a single point "
|
||||
"abroad does not abort the run.</p>"
|
||||
"<p><b>The price follows the quantity:</b> {coins} DonkeyCoins per "
|
||||
"started {batch} points, once the {free} free calls of the current "
|
||||
"hour are used up.</p>"
|
||||
"<p>Elevation data by the surveying authorities of the German "
|
||||
"federal states; the exact source of every point is in "
|
||||
"<code>ele_source</code>.</p>"
|
||||
).format(max_points=config.ELEVATION_MAX_POINTS,
|
||||
batch=config.ELEVATION_BILLING_BATCH,
|
||||
coins=config.COIN_COSTS.get("elevation_points", 2),
|
||||
free=config.FREE_CALLS_PER_HOUR, outside=OUTSIDE)
|
||||
|
||||
def initAlgorithm(self, configuration=None): # noqa: N802
|
||||
self.addParameter(QgsProcessingParameterFeatureSource(
|
||||
self.INPUT, tr("Point layer"),
|
||||
types=[QgsProcessing.SourceType.TypeVectorPoint]))
|
||||
|
||||
self.addParameter(QgsProcessingParameterFeatureSink(
|
||||
self.OUTPUT, tr("Points with elevation"),
|
||||
type=QgsProcessing.SourceType.TypeVectorPoint))
|
||||
|
||||
def processAlgorithm(self, parameters, context, feedback): # noqa: N802
|
||||
source = self.parameterAsSource(parameters, self.INPUT, context)
|
||||
if source is None:
|
||||
raise QgsProcessingException(tr("The input layer could not be read."))
|
||||
|
||||
coordinates, features = self.source_points(source, context, feedback)
|
||||
if not coordinates:
|
||||
raise QgsProcessingException(tr("The layer contains no usable points."))
|
||||
|
||||
inside, outside = points.split_germany(coordinates)
|
||||
if outside:
|
||||
feedback.pushWarning(tr(
|
||||
"{count} points lie outside Germany and get no elevation.").format(
|
||||
count=len(outside)))
|
||||
if not inside:
|
||||
raise QgsProcessingException(points.germany_message())
|
||||
|
||||
values = self._lookup(coordinates, inside, feedback)
|
||||
if feedback.isCanceled():
|
||||
return {}
|
||||
|
||||
out_fields = self._fields(source.fields())
|
||||
sink, dest = self.parameterAsSink(
|
||||
parameters, self.OUTPUT, context, out_fields,
|
||||
source.wkbType(), source.sourceCrs())
|
||||
if sink is None:
|
||||
raise QgsProcessingException(tr("The output could not be created."))
|
||||
|
||||
known = 0
|
||||
for feature, extra in zip(features, values):
|
||||
if feedback.isCanceled():
|
||||
return {}
|
||||
out = QgsFeature(out_fields)
|
||||
out.setGeometry(feature.geometry())
|
||||
out.setAttributes(list(feature.attributes()) + [
|
||||
extra.get("elevation"), extra.get("ele_source"),
|
||||
extra.get("ele_state")])
|
||||
sink.addFeature(out, QgsFeatureSink.Flag.FastInsert)
|
||||
if extra.get("elevation") is not None:
|
||||
known += 1
|
||||
|
||||
feedback.pushInfo(tr("{known} of {count} points have an elevation.").format(
|
||||
known=known, count=len(features)))
|
||||
self.style(context, dest, style.elevation_plan())
|
||||
feedback.setProgress(100)
|
||||
|
||||
result: Dict[str, Any] = {self.OUTPUT: dest}
|
||||
return result
|
||||
|
||||
def _lookup(self, coordinates, inside: List[int], feedback) -> List[Dict[str, Any]]:
|
||||
wanted = set(inside)
|
||||
values: List[Dict[str, Any]] = [
|
||||
{"elevation": None, "ele_source": None,
|
||||
"ele_state": None if index in wanted else OUTSIDE}
|
||||
for index in range(len(coordinates))
|
||||
]
|
||||
|
||||
api = self.client(feedback)
|
||||
batches = list(points.chunks(inside, config.ELEVATION_MAX_POINTS))
|
||||
for number, batch in enumerate(batches):
|
||||
if feedback.isCanceled():
|
||||
return values
|
||||
feedback.pushInfo(tr("Batch {number} of {total} · {count} points").format(
|
||||
number=number + 1, total=len(batches), count=len(batch)))
|
||||
try:
|
||||
payload = api.elevation_points([coordinates[i] for i in batch],
|
||||
feedback=feedback)
|
||||
except GeoDonkeyError as exc:
|
||||
self.fail(exc, feedback)
|
||||
self.report(api, feedback)
|
||||
answers = results.elevation_values(payload)
|
||||
if len(answers) != len(batch):
|
||||
raise QgsProcessingException(tr(
|
||||
"The service returned {got} elevations for {sent} points."
|
||||
).format(got=len(answers), sent=len(batch)))
|
||||
for position, index in enumerate(batch):
|
||||
values[index] = answers[position]
|
||||
feedback.setProgress(10 + 80.0 * (number + 1) / len(batches))
|
||||
return values
|
||||
|
||||
@staticmethod
|
||||
def _fields(source_fields: QgsFields) -> QgsFields:
|
||||
out = QgsFields()
|
||||
taken = set()
|
||||
for existing in source_fields:
|
||||
out.append(existing)
|
||||
taken.add(existing.name())
|
||||
for name, kind in results.ELEVATION_FIELDS:
|
||||
unique = name
|
||||
suffix = 2
|
||||
while unique in taken:
|
||||
unique = "%s_%d" % (name, suffix)
|
||||
suffix += 1
|
||||
taken.add(unique)
|
||||
out.append(field(unique, kind))
|
||||
return out
|
||||
Reference in new issue
Block a user