Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show listwidget coordinates in project crs in main GUI #266

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ RELEASING:
14. Create new release in GitHub with tag version and release title of `vX.X.X`
-->

## Unreleased
### added
- Show coordinates in point list in project crs ([#200](https://github.com/GIScience/orstools-qgis-plugin/issues/200))

## [1.8.3] - 2024-05-29

### Fixed
Expand Down
30 changes: 9 additions & 21 deletions ORStools/gui/ORStoolsDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
PREFERENCES,
)
from ORStools.gui import directions_gui
from ORStools.utils import exceptions, maptools, logger, configmanager, transform
from ORStools.utils import exceptions, maptools, logger, configmanager
from .ORStoolsDialogConfig import ORStoolsDialogConfigMain
from .ORStoolsDialogUI import Ui_ORStoolsDialogBase

Expand Down Expand Up @@ -269,16 +269,6 @@ def run_gui_control(self) -> None:
layer_out.loadNamedStyle(qml_path, True)
layer_out.triggerRepaint()

# Associate annotations with map layer, so they get deleted when layer is deleted
for annotation in self.dlg.annotations:
# Has the potential to be pretty cool: instead of deleting, associate with mapLayer
# , you can change order after optimization
# Then in theory, when the layer is remove, the annotation is removed as well
# Doesn't work though, the annotations are still there when project is re-opened
# annotation.setMapLayer(layer_out)
self.project.annotationManager().removeAnnotation(annotation)
self.dlg.annotations = []

provider_id = self.dlg.provider_combo.currentIndex()
provider = configmanager.read_config()["providers"][provider_id]

Expand Down Expand Up @@ -315,7 +305,7 @@ def run_gui_control(self) -> None:
clnt = client.Client(provider, agent)
clnt_msg = ""

directions = directions_gui.Directions(self.dlg)
directions = directions_gui.Directions(self.dlg, self.iface)
params = None
try:
params = directions.get_parameters()
Expand Down Expand Up @@ -498,6 +488,7 @@ def _save_vertices_to_layer(self) -> None:
self.routing_fromline_list.item(x).text()
for x in range(self.routing_fromline_list.count())
]
map_crs = self._iface.mapCanvas().mapSettings().destinationCrs()

if len(items) > 0:
point_layer = QgsVectorLayer(
Expand All @@ -512,6 +503,7 @@ def _save_vertices_to_layer(self) -> None:
feature.setAttributes([idx])

point_layer.dataProvider().addFeature(feature)
point_layer.setCrs(map_crs)
QgsProject.instance().addMapLayer(point_layer)
self._iface.mapCanvas().refresh()

Expand Down Expand Up @@ -540,7 +532,7 @@ def _on_clear_listwidget_click(self) -> None:
else:
# else clear all items and annotations
self.routing_fromline_list.clear()
self._clear_annotations()
self.clear_annotations()

# Remove blue lines (rubber band)
if self.line_tool:
Expand All @@ -567,7 +559,7 @@ def _linetool_annotate_point(

return QgsMapCanvasAnnotationItem(annotation, self.annotation_canvas).annotation()

def _clear_annotations(self) -> None:
def clear_annotations(self) -> None:
"""Clears annotations"""
for annotation_item in self.annotation_canvas.annotationItems():
annotation = annotation_item.annotation()
Expand All @@ -584,7 +576,7 @@ def _on_linetool_init(self) -> None:
self.hide()
self.routing_fromline_list.clear()
# Remove all annotations which were added (if any)
self._clear_annotations()
self.clear_annotations()

self.line_tool = maptools.LineTool(self._iface.mapCanvas())
self._iface.mapCanvas().setMapTool(self.line_tool)
Expand All @@ -595,11 +587,7 @@ def _on_linetool_init(self) -> None:

def _on_linetool_map_click(self, point: QgsPointXY, idx: int) -> None:
"""Adds an item to QgsListWidget and annotates the point in the map canvas"""
map_crs = self._iface.mapCanvas().mapSettings().destinationCrs()

transformer = transform.transformToWGS(map_crs)
point_wgs = transformer.transform(point)
self.routing_fromline_list.addItem(f"Point {idx}: {point_wgs.x():.6f}, {point_wgs.y():.6f}")
self.routing_fromline_list.addItem(f"Point {idx}: {point.x():.6f}, {point.y():.6f}")

annotation = self._linetool_annotate_point(point, idx)
self.project.annotationManager().addAnnotation(annotation)
Expand All @@ -611,7 +599,7 @@ def _reindex_list_items(self) -> None:
for x in range(self.routing_fromline_list.count())
]
self.routing_fromline_list.clear()
self._clear_annotations()
self.clear_annotations()
crs = QgsCoordinateReferenceSystem(f"EPSG:{4326}")
for idx, x in enumerate(items):
coords = x.split(":")[1]
Expand Down
16 changes: 14 additions & 2 deletions ORStools/gui/directions_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

from ORStools.utils import transform

from qgis.core import QgsPointXY


def _get_avoid_polygons(layer):
"""
Expand Down Expand Up @@ -84,12 +86,13 @@ def _get_avoid_options(avoid_boxes):
class Directions:
"""Extended functionality for directions endpoint for GUI."""

def __init__(self, dlg):
def __init__(self, dlg, iface):
"""
:param dlg: Main GUI dialog.
:type dlg: QDialog
"""
self.dlg = dlg
self.iface = iface

self.options = dict()

Expand All @@ -100,6 +103,9 @@ def get_request_line_feature(self):
:returns: coordinate list of line
:rtype: list
"""
map_crs = self.iface.mapCanvas().mapSettings().destinationCrs()
transformer = transform.transformToWGS(map_crs)

coordinates = []
layers_list = self.dlg.routing_fromline_list
for idx in range(layers_list.count()):
Expand All @@ -108,7 +114,13 @@ def get_request_line_feature(self):

coordinates.append([float(coord) for coord in coords.split(", ")])

return [[round(x, 6), round(y, 6)] for x, y in coordinates]
transformed_coordinates = []
for coord in coordinates:
point = QgsPointXY(coord[0], coord[1])
transformed_point = transformer.transform(point)
transformed_coordinates.append([transformed_point.x(), transformed_point.y()])

return [[round(x, 6), round(y, 6)] for x, y in transformed_coordinates]

def get_parameters(self):
"""
Expand Down
Loading