GeoDonkey for QGIS 1.12
This commit is contained in:
commit
20ab32da30
63 files changed
+11157
No files matched your search
@@ -0,0 +1,18 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from .elevation import ElevationPage
|
||||
from .isochrone import IsochronePage
|
||||
from .profile import ProfilePage
|
||||
from .route import RoutePage
|
||||
from .sight import SightPage
|
||||
|
||||
PAGES = (
|
||||
IsochronePage,
|
||||
RoutePage,
|
||||
ProfilePage,
|
||||
ElevationPage,
|
||||
SightPage,
|
||||
)
|
||||
|
||||
__all__ = ["PAGES", "ElevationPage", "IsochronePage", "ProfilePage",
|
||||
"RoutePage", "SightPage"]
|
||||
@@ -0,0 +1,301 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
from qgis.PyQt.QtCore import Qt
|
||||
from qgis.PyQt.QtGui import QKeySequence
|
||||
from qgis.PyQt.QtWidgets import (
|
||||
QAbstractItemView,
|
||||
QComboBox,
|
||||
QHBoxLayout,
|
||||
QHeaderView,
|
||||
QPushButton,
|
||||
QTableWidget,
|
||||
QTableWidgetItem,
|
||||
QVBoxLayout,
|
||||
QWidget,
|
||||
)
|
||||
from qgis.core import Qgis, QgsProject
|
||||
|
||||
from ... import config, mapcontext, points as points_mod, results
|
||||
from ...compat import QAction
|
||||
from ...i18n import later, tr
|
||||
from .. import theme
|
||||
from ..maptab import MapTab
|
||||
|
||||
COL_X, COL_Y, COL_Z = 0, 1, 2
|
||||
|
||||
MAX_POINTS = 20
|
||||
|
||||
|
||||
class ElevationPage(MapTab):
|
||||
ALGORITHM = "geodonkey:elevationpoints"
|
||||
NAME = later("Point elevation")
|
||||
ICON = "elevation.svg"
|
||||
|
||||
WANTED = MAX_POINTS
|
||||
FREE = True
|
||||
LEAST = 1
|
||||
CONNECT = False
|
||||
AUTO_RUN = False
|
||||
PROMPTS = (later("Click a point on the map."),
|
||||
later("Another point, or right-click to finish."))
|
||||
PICK_TEXT = later("Pick points on the map")
|
||||
TASK_TITLE = later("GeoDonkey: point elevation")
|
||||
LAYER_TITLE = later("Point elevation")
|
||||
STYLE_KEY = "elevation"
|
||||
|
||||
def setup(self) -> None:
|
||||
self._coordinates: List[Tuple[float, float]] = []
|
||||
|
||||
def body(self) -> Optional[QWidget]:
|
||||
holder = QWidget()
|
||||
box = QVBoxLayout(holder)
|
||||
box.setContentsMargins(0, 0, 0, 0)
|
||||
box.setSpacing(theme.TIGHT)
|
||||
|
||||
self.crs_box = QComboBox()
|
||||
self.crs_box.addItem(tr("X/Y in WGS 84 · degrees"))
|
||||
self.crs_box.addItem(tr("X/Y in the map CRS"))
|
||||
self.crs_box.currentIndexChanged.connect(lambda _index: self.fill_places())
|
||||
box.addWidget(self.crs_box)
|
||||
|
||||
self.table = QTableWidget(0, 3)
|
||||
self.table.setHorizontalHeaderLabels([tr("X"), tr("Y"), tr("Z (m)")])
|
||||
self.table.setAlternatingRowColors(True)
|
||||
self.table.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
|
||||
self.table.setSelectionBehavior(
|
||||
QAbstractItemView.SelectionBehavior.SelectRows)
|
||||
self.table.setSelectionMode(
|
||||
QAbstractItemView.SelectionMode.SingleSelection)
|
||||
self.table.horizontalHeader().setSectionResizeMode(
|
||||
QHeaderView.ResizeMode.Stretch)
|
||||
self.table.verticalHeader().setDefaultSectionSize(20)
|
||||
self.table.currentCellChanged.connect(
|
||||
lambda row, _c, _pr, _pc: self._cell_changed(row))
|
||||
|
||||
self._remove_action = QAction(tr("Remove point"), self.table)
|
||||
self._remove_action.setShortcut(QKeySequence.StandardKey.Delete)
|
||||
self._remove_action.setShortcutContext(
|
||||
Qt.ShortcutContext.WidgetShortcut)
|
||||
self._remove_action.triggered.connect(self.remove_point)
|
||||
self.table.addAction(self._remove_action)
|
||||
self.table.setContextMenuPolicy(
|
||||
Qt.ContextMenuPolicy.ActionsContextMenu)
|
||||
box.addWidget(self.table, 1)
|
||||
|
||||
row = QHBoxLayout()
|
||||
row.setSpacing(theme.TIGHT)
|
||||
|
||||
self.remove_button = QPushButton(tr("Remove point"))
|
||||
self.remove_button.setToolTip(tr(
|
||||
"Remove the selected point from the list. "
|
||||
"A right-click on the row or the Delete key does the same."))
|
||||
self.remove_button.setEnabled(False)
|
||||
self.remove_button.clicked.connect(self.remove_point)
|
||||
row.addWidget(self.remove_button)
|
||||
|
||||
self.lookup_button = QPushButton(tr("Look up elevations"))
|
||||
self.lookup_button.setStyleSheet(theme.primary_button_style())
|
||||
self.lookup_button.setEnabled(False)
|
||||
self.lookup_button.clicked.connect(self.run)
|
||||
row.addWidget(self.lookup_button, 1)
|
||||
box.addLayout(row)
|
||||
return holder
|
||||
|
||||
def controls(self):
|
||||
return (self.crs_box,)
|
||||
|
||||
def busy(self, running: bool) -> None:
|
||||
super().busy(running)
|
||||
self.lookup_button.setEnabled(not running and bool(self.points))
|
||||
|
||||
def show_points(self) -> None:
|
||||
if len(self.points) < 2:
|
||||
super().show_points()
|
||||
return
|
||||
self.points_label.setText(tr("{count} points picked.").format(
|
||||
count=len(self.points)))
|
||||
|
||||
def price(self) -> str:
|
||||
return tr("{coins} DC per run · up to {limit} points").format(
|
||||
coins=config.elevation_coins(MAX_POINTS, "elevation_points"),
|
||||
limit=MAX_POINTS)
|
||||
|
||||
def run(self) -> None:
|
||||
if not self.points:
|
||||
self.panel.say(tr("Pick at least one point on the map."),
|
||||
Qgis.MessageLevel.Warning)
|
||||
return
|
||||
coordinates = list(self.points)
|
||||
client = self.panel.api()
|
||||
|
||||
def work(feedback):
|
||||
return client.elevation_points(coordinates, feedback=feedback)
|
||||
|
||||
self.start(work, tr("Looking up the elevations…"))
|
||||
|
||||
def clear_result(self) -> None:
|
||||
for row in range(self.table.rowCount()):
|
||||
self._cell(row, COL_Z, "")
|
||||
marker = self.marker()
|
||||
if marker is not None:
|
||||
marker.hide()
|
||||
|
||||
def accept(self, payload: Dict[str, Any]) -> None:
|
||||
self.payload = payload
|
||||
rows = results.elevation_point_rows(self.payload, self.points)
|
||||
self._coordinates = list(self.points)
|
||||
self.fill_places()
|
||||
if not rows:
|
||||
self.keep_button.setEnabled(False)
|
||||
self.panel.say(tr("The service returned no elevations."),
|
||||
Qgis.MessageLevel.Warning)
|
||||
return
|
||||
|
||||
missing = 0
|
||||
for index, row in enumerate(rows):
|
||||
attrs = row["attrs"]
|
||||
height = attrs.get("elevation")
|
||||
if height is None:
|
||||
missing += 1
|
||||
self._cell(index, COL_Z, "–", tr("No elevation data here."))
|
||||
else:
|
||||
self._cell(index, COL_Z,
|
||||
points_mod.fmt(round(float(height), 1)),
|
||||
attrs.get("ele_source") or "")
|
||||
|
||||
self.keep_button.setEnabled(True)
|
||||
self.summary.setText(self._span(rows))
|
||||
if missing:
|
||||
self.panel.say(tr("{missing} of {count} points have no elevation "
|
||||
"data.").format(missing=missing, count=len(rows)),
|
||||
Qgis.MessageLevel.Warning)
|
||||
else:
|
||||
self.panel.say(tr("{count} elevation(s) found.").format(
|
||||
count=len(rows)), Qgis.MessageLevel.Success)
|
||||
|
||||
@staticmethod
|
||||
def _span(rows) -> str:
|
||||
heights = [row["attrs"]["elevation"] for row in rows
|
||||
if row["attrs"].get("elevation") is not None]
|
||||
if not heights:
|
||||
return ""
|
||||
if len(heights) == 1:
|
||||
return tr("{height} m above sea level").format(
|
||||
height=points_mod.fmt(round(float(heights[0]), 1)))
|
||||
low, high = min(heights), max(heights)
|
||||
return tr("{low} m to {high} m · {span} m difference").format(
|
||||
low=points_mod.fmt(round(float(low), 1)),
|
||||
high=points_mod.fmt(round(float(high), 1)),
|
||||
span=points_mod.fmt(round(float(high) - float(low), 1)))
|
||||
|
||||
def points_changed(self) -> None:
|
||||
self.payload = {}
|
||||
self._coordinates = list(self.points)
|
||||
self.fill_places()
|
||||
self.summary.setText("")
|
||||
self.keep_button.setEnabled(False)
|
||||
self.lookup_button.setEnabled(bool(self.points))
|
||||
dots = self.dots()
|
||||
if dots is not None:
|
||||
dots.show(self._coordinates)
|
||||
self._selection_changed()
|
||||
|
||||
def remove_point(self) -> None:
|
||||
row = self.table.currentRow()
|
||||
if row < 0 or row >= len(self.points):
|
||||
return
|
||||
|
||||
self._drop_from_payload(row)
|
||||
self.points = [point for index, point in enumerate(self.points)
|
||||
if index != row]
|
||||
self._coordinates = list(self.points)
|
||||
self.table.removeRow(row)
|
||||
self.fill_places()
|
||||
self.show_points()
|
||||
|
||||
dots = self.dots()
|
||||
if dots is not None:
|
||||
dots.show(self._coordinates)
|
||||
|
||||
rows = self.rows() if self.payload else []
|
||||
self.summary.setText(self._span(rows) if rows else "")
|
||||
self.keep_button.setEnabled(bool(rows))
|
||||
self.lookup_button.setEnabled(bool(self.points))
|
||||
self._row_changed(self.table.currentRow())
|
||||
self._selection_changed()
|
||||
self.panel.say(tr("Point removed."))
|
||||
|
||||
def _drop_from_payload(self, row: int) -> None:
|
||||
if not isinstance(self.payload, dict):
|
||||
return
|
||||
places = self.payload.get("points")
|
||||
if isinstance(places, list) and len(places) == len(self.points):
|
||||
places.pop(row)
|
||||
return
|
||||
self.payload = {}
|
||||
|
||||
def _selection_changed(self) -> None:
|
||||
row = self.table.currentRow()
|
||||
self.remove_button.setEnabled(0 <= row < len(self._coordinates))
|
||||
self._remove_action.setEnabled(self.remove_button.isEnabled())
|
||||
|
||||
def fill_places(self) -> None:
|
||||
self._name_map_crs()
|
||||
self.table.setRowCount(len(self._coordinates))
|
||||
for row, (lon, lat) in enumerate(self._coordinates):
|
||||
x, y = self._place(lon, lat)
|
||||
self._cell(row, COL_X, x)
|
||||
self._cell(row, COL_Y, y)
|
||||
if self.table.item(row, COL_Z) is None:
|
||||
self._cell(row, COL_Z, "")
|
||||
|
||||
def map_crs(self):
|
||||
canvas = self.canvas()
|
||||
if canvas is not None:
|
||||
return canvas.mapSettings().destinationCrs()
|
||||
return QgsProject.instance().crs()
|
||||
|
||||
def _name_map_crs(self) -> None:
|
||||
crs = self.map_crs()
|
||||
code = crs.authid() if crs is not None and crs.isValid() else ""
|
||||
self.crs_box.setItemText(1, tr("X/Y in {crs}").format(crs=code)
|
||||
if code else tr("X/Y in the map CRS"))
|
||||
|
||||
def _place(self, lon: float, lat: float) -> Tuple[str, str]:
|
||||
if self.crs_box.currentIndex() == 0:
|
||||
return ("%.5f" % lon, "%.5f" % lat)
|
||||
crs = self.map_crs()
|
||||
point = mapcontext.point_from_wgs84(lon, lat, crs)
|
||||
if point is None:
|
||||
return ("", "")
|
||||
places = 5 if crs is not None and crs.isGeographic() else 2
|
||||
return ("%.*f" % (places, point.x()), "%.*f" % (places, point.y()))
|
||||
|
||||
def _cell(self, row: int, column: int, text: str, tip: str = "") -> None:
|
||||
item = self.table.item(row, column)
|
||||
if item is None:
|
||||
item = QTableWidgetItem()
|
||||
item.setTextAlignment(Qt.AlignmentFlag.AlignRight
|
||||
| Qt.AlignmentFlag.AlignVCenter)
|
||||
self.table.setItem(row, column, item)
|
||||
item.setText(text)
|
||||
item.setToolTip(tip)
|
||||
|
||||
def rows(self):
|
||||
return results.elevation_point_rows(self.payload, self._coordinates)
|
||||
|
||||
def _cell_changed(self, index: int) -> None:
|
||||
self._row_changed(index)
|
||||
self._selection_changed()
|
||||
|
||||
def _row_changed(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)
|
||||
@@ -0,0 +1,130 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from qgis.PyQt.QtWidgets import (
|
||||
QComboBox,
|
||||
QFormLayout,
|
||||
QWidget,
|
||||
)
|
||||
from qgis.core import Qgis
|
||||
|
||||
from ... import config, results
|
||||
from ...i18n import later, tr
|
||||
from ..maptab import MapTab
|
||||
|
||||
MODES = (
|
||||
("walking", later("on foot")),
|
||||
("cycling", later("by bike")),
|
||||
("driving", later("by car")),
|
||||
)
|
||||
|
||||
METRICS = (
|
||||
("minutes", later("minutes"), ("5,10,15", "10,20,30", "15,30,45,60")),
|
||||
("kilometres", later("kilometres"), ("1,2,5", "2,5,10", "5,10,25,50")),
|
||||
)
|
||||
|
||||
|
||||
class IsochronePage(MapTab):
|
||||
ALGORITHM = "geodonkey:isochrone"
|
||||
NAME = later("Isochrone")
|
||||
ICON = "isochrone.svg"
|
||||
|
||||
WANTED = 1
|
||||
PROMPTS = (later("Click the centre point on the map."),)
|
||||
PICK_TEXT = later("Pick the centre on the map")
|
||||
TASK_TITLE = later("GeoDonkey: isochrone")
|
||||
LAYER_TITLE = later("Isochrone")
|
||||
STYLE_KEY = "isochrone"
|
||||
|
||||
def options(self) -> Optional[QWidget]:
|
||||
box = QWidget()
|
||||
form = QFormLayout(box)
|
||||
form.setContentsMargins(0, 0, 0, 0)
|
||||
form.setSpacing(6)
|
||||
|
||||
self.mode = QComboBox()
|
||||
for key, label in MODES:
|
||||
self.mode.addItem(tr(label), key)
|
||||
self.mode.setCurrentIndex(
|
||||
[key for key, _ in MODES].index(config.ISOCHRONE_DEFAULT_MODE))
|
||||
form.addRow(tr("Travel:"), self.mode)
|
||||
|
||||
self.metric = QComboBox()
|
||||
for key, label, _presets in METRICS:
|
||||
self.metric.addItem(tr(label), key)
|
||||
self.metric.currentIndexChanged.connect(self._metric_changed)
|
||||
form.addRow(tr("Measured in:"), self.metric)
|
||||
|
||||
self.rings = QComboBox()
|
||||
self.rings.setEditable(True)
|
||||
form.addRow(tr("Rings:"), self.rings)
|
||||
self._metric_changed()
|
||||
return box
|
||||
|
||||
def _metric_changed(self) -> None:
|
||||
index = max(0, self.metric.currentIndex())
|
||||
_key, _label, presets = METRICS[index]
|
||||
self.rings.clear()
|
||||
self.rings.addItems(list(presets))
|
||||
self.rings.setCurrentIndex(0)
|
||||
|
||||
def controls(self):
|
||||
return (self.mode, self.metric, self.rings)
|
||||
|
||||
def price(self) -> str:
|
||||
return tr("{coins} DC per run, regardless of the number of rings").format(
|
||||
coins=config.COIN_COSTS.get("isochrone", 0))
|
||||
|
||||
def run(self) -> None:
|
||||
if not self.points:
|
||||
self.panel.say(tr("Pick the centre on the map first."),
|
||||
Qgis.MessageLevel.Warning)
|
||||
return
|
||||
lon, lat = self.points[0]
|
||||
mode = self.mode.currentData()
|
||||
metric = self.metric.currentData()
|
||||
values = self.rings.currentText().strip()
|
||||
if not values:
|
||||
self.panel.say(tr("Enter at least one ring, for example 5,10,15."),
|
||||
Qgis.MessageLevel.Warning)
|
||||
return
|
||||
client = self.panel.api()
|
||||
|
||||
def work(feedback):
|
||||
if metric == "kilometres":
|
||||
return client.isochrone(lon, lat, mode=mode, kilometres=values,
|
||||
feedback=feedback)
|
||||
return client.isochrone(lon, lat, mode=mode, minutes=values,
|
||||
feedback=feedback)
|
||||
|
||||
self.start(work, tr("Calculating the isochrone…"))
|
||||
|
||||
def clear_result(self) -> None:
|
||||
shapes = self.shapes()
|
||||
if shapes is not None:
|
||||
shapes.clear()
|
||||
|
||||
def accept(self, payload: Dict[str, Any]) -> None:
|
||||
self.payload = payload
|
||||
rows = results.isochrone_rows(self.payload)
|
||||
if not rows:
|
||||
self.keep_button.setEnabled(False)
|
||||
self.panel.say(tr("The service returned no area."),
|
||||
Qgis.MessageLevel.Warning)
|
||||
return
|
||||
|
||||
shapes = self.shapes()
|
||||
if shapes is not None:
|
||||
shapes.show([(row.get("geometry"), row["attrs"].get("color"))
|
||||
for row in rows])
|
||||
self.keep_button.setEnabled(True)
|
||||
|
||||
labels = [row["attrs"].get("label") or "" for row in rows]
|
||||
self.summary.setText(tr("{count} ring(s): {list}").format(
|
||||
count=len(rows), list=" · ".join(reversed(labels))))
|
||||
self.panel.say(tr("{count} ring(s) calculated.").format(count=len(rows)),
|
||||
Qgis.MessageLevel.Success)
|
||||
|
||||
def rows(self):
|
||||
return results.isochrone_rows(self.payload)
|
||||
@@ -0,0 +1,146 @@
|
||||
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<br>%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)
|
||||
@@ -0,0 +1,113 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from qgis.PyQt.QtWidgets import (
|
||||
QCheckBox,
|
||||
QComboBox,
|
||||
QFormLayout,
|
||||
QWidget,
|
||||
)
|
||||
from qgis.core import Qgis
|
||||
|
||||
from ... import config, results
|
||||
from ...i18n import later, tr
|
||||
from ..maptab import MapTab
|
||||
|
||||
MODES = (
|
||||
("driving", later("by car")),
|
||||
("cycling", later("by bike")),
|
||||
("walking", later("on foot")),
|
||||
)
|
||||
|
||||
|
||||
class RoutePage(MapTab):
|
||||
ALGORITHM = "geodonkey:route"
|
||||
NAME = later("Route")
|
||||
ICON = "route.svg"
|
||||
|
||||
WANTED = config.ROUTE_MAX_POINTS
|
||||
FREE = True
|
||||
PROMPTS = (later("Click the start point on the map."),
|
||||
later("Click the destination – or further stopovers."),
|
||||
later("Another stopover, or right-click to finish."))
|
||||
PICK_TEXT = later("Pick the route on the map")
|
||||
TASK_TITLE = later("GeoDonkey: route")
|
||||
LAYER_TITLE = later("Route")
|
||||
STYLE_KEY = "route"
|
||||
|
||||
def options(self) -> Optional[QWidget]:
|
||||
box = QWidget()
|
||||
form = QFormLayout(box)
|
||||
form.setContentsMargins(0, 0, 0, 0)
|
||||
form.setSpacing(6)
|
||||
|
||||
self.mode = QComboBox()
|
||||
for key, label in MODES:
|
||||
self.mode.addItem(tr(label), key)
|
||||
form.addRow(tr("Travel:"), self.mode)
|
||||
|
||||
self.optimize = QCheckBox(tr("Find the shortest order"))
|
||||
self.optimize.toggled.connect(self.update_cost)
|
||||
form.addRow("", self.optimize)
|
||||
|
||||
self.steps = QCheckBox(tr("Include turn-by-turn directions"))
|
||||
form.addRow("", self.steps)
|
||||
return box
|
||||
|
||||
def controls(self):
|
||||
return (self.mode, self.optimize, self.steps)
|
||||
|
||||
def price(self) -> str:
|
||||
key = "route_optimized" if self.optimize.isChecked() else "route"
|
||||
return tr("{coins} DC per run · 2 to {limit} points").format(
|
||||
coins=config.COIN_COSTS.get(key, 0),
|
||||
limit=config.ROUTE_MAX_POINTS)
|
||||
|
||||
def run(self) -> None:
|
||||
if len(self.points) < config.ROUTE_MIN_POINTS:
|
||||
self.panel.say(tr("A route needs at least {count} points.").format(
|
||||
count=config.ROUTE_MIN_POINTS), Qgis.MessageLevel.Warning)
|
||||
return
|
||||
stops = list(self.points)
|
||||
mode = self.mode.currentData()
|
||||
optimize = self.optimize.isChecked()
|
||||
steps = self.steps.isChecked()
|
||||
client = self.panel.api()
|
||||
|
||||
def work(feedback):
|
||||
return client.route(stops, mode=mode, optimize=optimize,
|
||||
steps=steps, feedback=feedback)
|
||||
|
||||
self.start(work, tr("Calculating the route…"))
|
||||
|
||||
def clear_result(self) -> None:
|
||||
shapes = self.shapes()
|
||||
if shapes is not None:
|
||||
shapes.clear()
|
||||
|
||||
def accept(self, payload: Dict[str, Any]) -> None:
|
||||
self.payload = payload
|
||||
legs, stops = results.route_rows(self.payload)
|
||||
if not legs:
|
||||
self.keep_button.setEnabled(False)
|
||||
self.panel.say(tr("The service returned no route."),
|
||||
Qgis.MessageLevel.Warning)
|
||||
return
|
||||
|
||||
shapes = self.shapes()
|
||||
if shapes is not None:
|
||||
shapes.show([(row.get("geometry"), row["attrs"].get("color"))
|
||||
for row in legs])
|
||||
self.keep_button.setEnabled(True)
|
||||
|
||||
summary = results.route_summary(self.payload)
|
||||
self.summary.setText("%s<br>%s" % (
|
||||
summary,
|
||||
tr("{legs} leg(s) · {stops} stop(s)").format(
|
||||
legs=len(legs), stops=len(stops))))
|
||||
self.panel.say(summary, Qgis.MessageLevel.Success)
|
||||
|
||||
def rows(self):
|
||||
legs, stops = results.route_rows(self.payload)
|
||||
return list(legs) + list(stops)
|
||||
@@ -0,0 +1,174 @@
|
||||
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])
|
||||
Reference in new issue
Block a user