175 lines
6.1 KiB
Python
175 lines
6.1 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Dict, List, Optional, Tuple
|
|
|
|
from qgis.PyQt.QtWidgets import (
|
|
QCheckBox,
|
|
QDoubleSpinBox,
|
|
QFormLayout,
|
|
QWidget,
|
|
)
|
|
from qgis.core import Qgis
|
|
|
|
from ... import config, points as points_mod, results
|
|
from ...i18n import later, tr
|
|
from ..chart import ProfileChart
|
|
from ..maptab import MapTab
|
|
|
|
|
|
class SightPage(MapTab):
|
|
ALGORITHM = "geodonkey:lineofsight"
|
|
NAME = later("Line of sight")
|
|
ICON = "lineofsight.svg"
|
|
|
|
WANTED = 2
|
|
PROMPTS = (later("Click the first point on the map."),
|
|
later("Click the second point on the map."))
|
|
PICK_TEXT = later("Pick two points on the map")
|
|
TASK_TITLE = later("GeoDonkey: line of sight")
|
|
LAYER_TITLE = later("Line of sight")
|
|
STYLE_KEY = "sight"
|
|
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()
|
|
form = QFormLayout(box)
|
|
form.setContentsMargins(0, 0, 0, 0)
|
|
form.setSpacing(6)
|
|
|
|
self.height_a = self._height()
|
|
form.addRow(tr("Height at A:"), self.height_a)
|
|
self.height_b = self._height()
|
|
form.addRow(tr("Height at B:"), self.height_b)
|
|
|
|
self.curvature = QCheckBox(tr("Account for earth curvature"))
|
|
self.curvature.setChecked(True)
|
|
form.addRow("", self.curvature)
|
|
return box
|
|
|
|
@staticmethod
|
|
def _height() -> QDoubleSpinBox:
|
|
spin = QDoubleSpinBox()
|
|
spin.setRange(0.0, config.LOS_MAX_HEIGHT_METRES)
|
|
spin.setDecimals(1)
|
|
spin.setSingleStep(0.5)
|
|
spin.setSuffix(" m")
|
|
spin.setValue(config.LOS_DEFAULT_HEIGHT_METRES)
|
|
return spin
|
|
|
|
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.height_a, self.height_b, self.curvature)
|
|
|
|
def price(self) -> str:
|
|
if len(self.points) < 2:
|
|
return tr("Free up to {limit} m air line, {coins} DC above").format(
|
|
limit=points_mod.fmt(round(config.LOS_FREE_METRES)),
|
|
coins=config.COIN_COSTS.get("line_of_sight", 0))
|
|
metres = points_mod.distance(self.points[0], self.points[1])
|
|
coins = config.line_of_sight_coins(metres)
|
|
if not coins:
|
|
return tr("{distance} m air line · free").format(
|
|
distance=points_mod.fmt(round(metres)))
|
|
return tr("{distance} m air line · {coins} DC").format(
|
|
distance=points_mod.fmt(round(metres)), coins=coins)
|
|
|
|
def show_points(self) -> None:
|
|
super().show_points()
|
|
self.update_cost()
|
|
|
|
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]
|
|
height_a = self.height_a.value()
|
|
height_b = self.height_b.value()
|
|
curvature = self.curvature.isChecked()
|
|
client = self.panel.api()
|
|
|
|
def work(feedback):
|
|
return client.line_of_sight(
|
|
start, end, height_a=height_a, height_b=height_b,
|
|
curvature=curvature, samples=True, feedback=feedback)
|
|
|
|
self.start(work, tr("Checking the line of sight…"))
|
|
|
|
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
|
|
start = self.points[0] if self.points else (0.0, 0.0)
|
|
end = self.points[1] if len(self.points) > 1 else start
|
|
lines, obstacles, samples = results.sight_rows(self.payload, start, end)
|
|
|
|
if not samples:
|
|
self.chart.set_placeholder(tr("No terrain samples returned."))
|
|
self.chart.clear()
|
|
else:
|
|
terrain = [(row["attrs"]["distance"], row["attrs"]["terrain"])
|
|
for row in samples]
|
|
sight = [(row["attrs"]["distance"], row["attrs"]["sight"])
|
|
for row in samples]
|
|
self.chart.set_samples(terrain)
|
|
self.chart.set_line(sight)
|
|
self.chart.set_marks([
|
|
(row["attrs"]["distance"], row["attrs"]["terrain"])
|
|
for row in obstacles
|
|
if row["attrs"].get("distance") is not None
|
|
and row["attrs"].get("terrain") is not None])
|
|
self._coordinates = [
|
|
tuple((row.get("geometry") or {}).get("coordinates") or ())
|
|
for row in samples]
|
|
|
|
track = self.track()
|
|
if track is not None:
|
|
track.show([start, end])
|
|
|
|
self.keep_button.setEnabled(bool(lines))
|
|
self.summary.setText(results.sight_summary(self.payload))
|
|
visible = bool((self.payload or {}).get("visible"))
|
|
self.panel.say(results.sight_summary(self.payload),
|
|
Qgis.MessageLevel.Success if visible
|
|
else Qgis.MessageLevel.Warning)
|
|
|
|
def cancel_task(self) -> None:
|
|
super().cancel_task()
|
|
self.chart.set_placeholder(tr(self.PLACEHOLDER))
|
|
|
|
def rows(self):
|
|
if not self.payload:
|
|
return []
|
|
start = self.points[0] if self.points else (0.0, 0.0)
|
|
end = self.points[1] if len(self.points) > 1 else start
|
|
_lines, obstacles, _samples = results.sight_rows(self.payload, start, end)
|
|
return list(results.sight_segment_rows(self.payload, start, end)) + list(obstacles)
|
|
|
|
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
|
|
pair = self._coordinates[index]
|
|
if len(pair) != 2:
|
|
marker.hide()
|
|
return
|
|
marker.show(pair[0], pair[1])
|