from __future__ import annotations from typing import Any, Dict, List, Optional, Tuple from qgis.PyQt.QtWidgets import QComboBox, QHBoxLayout, QLabel, QWidget from qgis.core import Qgis from ... import config, plot, points as points_mod, results from ...i18n import later, tr from ..chart import ProfileChart from ..maptab import MapTab RESOLUTIONS = ( (100, later("Rough")), (250, later("Normal")), (config.ELEVATION_MAX_POINTS, later("Fine")), ) DEFAULT_RESOLUTION = 1 class ProfilePage(MapTab): ALGORITHM = "geodonkey:elevationprofile" NAME = later("Elevation profile") ICON = "profile.svg" WANTED = 2 PROMPTS = (later("Click the start point on the map."), later("Click the end point on the map.")) PICK_TEXT = later("Pick two points on the map") TASK_TITLE = later("GeoDonkey: elevation profile") LAYER_TITLE = later("Elevation profile") STYLE_KEY = "profile" PLACEHOLDER = later("Pick two points on the map.") def setup(self) -> None: self._coordinates: List[Tuple[float, float]] = [] def options(self) -> Optional[QWidget]: box = QWidget() row = QHBoxLayout(box) row.setContentsMargins(0, 0, 0, 0) row.setSpacing(6) row.addWidget(QLabel(tr("Detail:"))) self.resolution = QComboBox() for count, label in RESOLUTIONS: self.resolution.addItem(tr(label), count) self.resolution.setCurrentIndex(DEFAULT_RESOLUTION) self.resolution.currentIndexChanged.connect(self.update_cost) row.addWidget(self.resolution, 1) return box def body(self) -> Optional[QWidget]: self.chart = ProfileChart() self.chart.set_placeholder(tr(self.PLACEHOLDER)) self.chart.hovered.connect(self._hovered) return self.chart def controls(self): return (self.resolution,) def max_points(self) -> int: value = self.resolution.currentData() return int(value or config.ELEVATION_MAX_POINTS) def price(self) -> str: return tr("Up to {count} points · {coins} DC per run").format( count=self.max_points(), coins=config.elevation_coins(self.max_points(), "elevation_profile")) def run(self) -> None: if len(self.points) < 2: self.panel.say(tr("Pick two points on the map first."), Qgis.MessageLevel.Warning) return start, end = self.points[0], self.points[1] limit = self.max_points() client = self.panel.api() def work(feedback): return client.elevation_profile( start, end, max_points=limit, step=config.ELEVATION_DEFAULT_STEP_METRES, feedback=feedback) self.start(work, tr("Calculating the elevation profile…")) def clear_result(self) -> None: self.chart.set_placeholder(tr("Calculating…")) self.chart.clear() def forget(self) -> None: self._coordinates = [] self.chart.set_placeholder(tr(self.PLACEHOLDER)) self.chart.clear() def accept(self, payload: Dict[str, Any]) -> None: self.payload = payload series = results.profile_series(self.payload) self._coordinates = results.profile_coordinates(self.payload) if not series: self.chart.set_placeholder(tr("The service returned no points.")) self.chart.clear() self.keep_button.setEnabled(False) self.panel.say(tr("The service returned no points."), Qgis.MessageLevel.Warning) return self.chart.set_samples(series) track = self.track() if track is not None: track.show(self._coordinates) self.keep_button.setEnabled(True) up, down = plot.gain_and_loss(series) self.summary.setText("%s
%s" % ( results.profile_summary(self.payload), tr("Ascent {up} m · descent {down} m").format( up=points_mod.fmt(round(up)), down=points_mod.fmt(round(down))))) missing = sum(1 for _x, y in series if y is None) if missing: self.panel.say(tr("{missing} of {count} points have no elevation " "data.").format(missing=missing, count=len(series)), Qgis.MessageLevel.Warning) else: self.panel.say(results.profile_summary(self.payload), Qgis.MessageLevel.Success) def cancel_task(self) -> None: super().cancel_task() self.chart.set_placeholder(tr(self.PLACEHOLDER)) def rows(self): return list(results.profile_rows(self.payload)) + \ list(results.profile_segment_rows(self.payload)) def _hovered(self, index: int) -> None: marker = self.marker() if marker is None: return if index < 0 or index >= len(self._coordinates): marker.hide() return lon, lat = self._coordinates[index] marker.show(lon, lat)