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

Add geocoding selection option for start and end point of route #223

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1ded71f
feat: add necessary UI elements
merydian Dec 18, 2023
9875fc9
feat: add autocomplete functionality
merydian Dec 18, 2023
7ea839a
refactor: run ruff
merydian Dec 21, 2023
4ea7721
feat: enable adding of chosen start and dest to list widget
merydian Dec 27, 2023
2f435d7
feat: only add vertices when they're not in the list
merydian Dec 27, 2023
e303fbc
feat: make geocoding work with point selection
merydian Dec 28, 2023
33390e5
chore: update branch
merydian Dec 28, 2023
8c176d2
feat: wait before making api request
merydian Jan 15, 2024
3d2888b
fix: error after selecting option
merydian Jan 15, 2024
c606b1e
docs: add changelog entry
merydian Jan 18, 2024
3e5427c
Revert "docs: add changelog entry"
merydian Jan 18, 2024
fd19e23
Merge branch 'main' into 205-add-geocoding-selection-option-for-start…
merydian Jan 19, 2024
4b1a998
feat: add tooltip to add button
merydian Jan 19, 2024
d9bca32
docs: add to changelog
merydian Jan 19, 2024
839a643
feat: add geocoded item directly after click on option
merydian Jan 23, 2024
a860da1
style: undo change to config
merydian Jan 23, 2024
766e32a
style: delete geocode add button
merydian Jan 23, 2024
5ba6477
bug: raise error when autocomplete request fails
merydian Jan 23, 2024
afeb6d7
style: run ruff
merydian Jan 23, 2024
532733c
feat: add automatic setting of focus point in autocomplete request
merydian Jan 25, 2024
bb71a2e
feat: print error code when autocomplete request fails
merydian Jan 25, 2024
fd02394
style: run ruff
merydian Jan 25, 2024
1c199b4
fix: correct setting of center coordinate
merydian Feb 13, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ RELEASING:
14. Create new release in GitHub with tag version and release title of `vX.X.X`
-->

# unreleased
- Add geocoding selection option for start and end point ([#205](https://github.com/GIScience/orstools-qgis-plugin/issues/205))

## [1.7.1] - 2024-01-15

Expand Down
129 changes: 121 additions & 8 deletions ORStools/gui/ORStoolsDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@

import json
import os

import processing
import webbrowser

from qgis._core import Qgis
from PyQt5.QtNetwork import QNetworkRequest
from qgis._core import Qgis, QgsCoordinateTransform
from qgis.core import (
QgsProject,
QgsVectorLayer,
Expand All @@ -42,12 +44,22 @@
QgsPointXY,
QgsGeometry,
QgsCoordinateReferenceSystem,
QgsBlockingNetworkRequest,
)
from qgis.gui import QgsMapCanvasAnnotationItem

from PyQt5.QtCore import QSizeF, QPointF, QCoreApplication, QSettings
from PyQt5.QtCore import QSizeF, QPointF, QCoreApplication, QSettings, Qt, QUrl, QTimer

from PyQt5.QtGui import QIcon, QTextDocument
from PyQt5.QtWidgets import QAction, QDialog, QApplication, QMenu, QMessageBox, QDialogButtonBox
from PyQt5.QtWidgets import (
QAction,
QDialog,
QApplication,
QMenu,
QMessageBox,
QDialogButtonBox,
QCompleter,
)

from ORStools import (
RESOURCE_PREFIX,
Expand Down Expand Up @@ -223,6 +235,7 @@ def _init_gui_control(self):
self.dlg = ORStoolsDialog(
self.iface, self.iface.mainWindow()
) # setting parent enables modal view

# Make sure plugin window stays open when OK is clicked by reconnecting the accepted() signal
self.dlg.global_buttons.accepted.disconnect(self.dlg.accept)
self.dlg.global_buttons.accepted.connect(self.run_gui_control)
Expand Down Expand Up @@ -396,6 +409,9 @@ def __init__(self, iface, parent=None):
:type parent: QDialog/QApplication
"""
QDialog.__init__(self, parent)
self.start_geocode_coords = None
self.dest_geocode_coords = None
self.geocoded = None
self.setupUi(self)

self._iface = iface
Expand Down Expand Up @@ -455,6 +471,96 @@ def __init__(self, iface, parent=None):
self.routing_fromline_list.model().rowsMoved.connect(self._reindex_list_items)
self.routing_fromline_list.model().rowsRemoved.connect(self._reindex_list_items)

# add connection to linedit text change
self.geocode_start.textChanged.connect(
lambda: self.wait_connect_geocode(self.geocode_start)
)
self.geocode_dest.textChanged.connect(lambda: self.wait_connect_geocode(self.geocode_dest))

def wait_connect_geocode(self, lineEdit):
text = lineEdit.text()
QTimer.singleShot(500, lambda: self.reload_geocode_completer(lineEdit, text))

def reload_geocode_completer(self, lineEdit, text):
if lineEdit.text() == text and lineEdit.text() != "":
provider_id = self.provider_combo.currentIndex()
provider = configmanager.read_config()["providers"][provider_id]
api_key = provider["key"]
if api_key != "":

def option_chosen(name, lineEdit):
coords = [
feature["geometry"]["coordinates"]
for feature in data["features"]
if feature["properties"]["name"] == name
][0]
self.add_geocoded_item(coords, lineEdit)
completer.activated.disconnect()
lineEdit.setText("")

e = self._iface.mapCanvas().extent()
lat = e.yMinimum() + (e.yMaximum() - e.yMinimum()) / 2
lon = e.xMinimum() + (e.xMaximum() - e.xMinimum()) / 2
sourceCrs = self._iface.mapCanvas().mapSettings().destinationCrs()
destCrs = QgsCoordinateReferenceSystem.fromEpsgId(4326)
tr = QgsCoordinateTransform(sourceCrs, destCrs, QgsProject.instance())
middle = tr.transform(QgsPointXY(lon, lat))

url = f"https://api.openrouteservice.org/geocode/autocomplete?api_key={api_key}&text={lineEdit.text()}&sources=geonames&focus.point.lat={middle.y()}&focus.point.lon={middle.x()}"
request = QgsBlockingNetworkRequest()
error_code = request.get(QNetworkRequest(QUrl(url)))
if error_code == QgsBlockingNetworkRequest.ErrorCode.NoError:
reply = request.reply()
data = json.loads(reply.content().data().decode("utf-8"))
suggest = set([i["properties"]["name"] for i in data["features"]])
completer = QCompleter(suggest)
completer.setCaseSensitivity(Qt.CaseInsensitive)
completer.setFilterMode(Qt.MatchContains)
completer.highlighted.connect(completer.highlighted.disconnect)
completer.activated.connect(lambda t: option_chosen(t, lineEdit))
lineEdit.setCompleter(completer)
completer.complete()

else:
raise ConnectionError(
f"Error while trying to request geocoding autocomplete, error code: {error_code}"
)
else:
QMessageBox.critical(
self,
"Missing API key",
"""
Did you forget to set an <b>API key</b> for openrouteservice?<br><br>

If you don't have an API key, please visit https://openrouteservice.org/sign-up to get one. <br><br>
Then enter the API key for openrouteservice provider in Web ► ORS Tools ► Provider Settings or the
settings symbol in the main ORS Tools GUI, next to the provider dropdown.""",
)

def add_geocoded_item(self, coordinates, lineEdit):
idx = "0"
p = f"Point {idx}: {coordinates[0]}, {coordinates[1]}"
items = [
self.routing_fromline_list.item(x).text()
for x in range(self.routing_fromline_list.count())
]
coords = [i.split(":")[1] for i in items]
if p.split(":")[1] not in coords:
if lineEdit.objectName() == "geocode_start":
items.insert(0, p)
else:
items.insert(self.routing_fromline_list.count(), p)

self.routing_fromline_list.clear()
self.routing_fromline_list.addItems(items)

point = QgsPointXY(coordinates[0], coordinates[1])
annotation = self._linetool_annotate_point(point, idx)
self.annotations.append(annotation)
self.project.annotationManager().addAnnotation(annotation)
self._reindex_list_items()
self.geocoded = True

def _save_vertices_to_layer(self):
"""Saves the vertices list to a temp layer"""
items = [
Expand Down Expand Up @@ -504,6 +610,7 @@ def _on_clear_listwidget_click(self):
# else clear all items and annotations
self.routing_fromline_list.clear()
self._clear_annotations()
self.geocoded = False

# Remove blue lines (rubber band)
if self.line_tool:
Expand Down Expand Up @@ -542,9 +649,10 @@ def _on_linetool_init(self):
self.line_tool.canvas.scene().removeItem(self.line_tool.rubberBand)

self.hide()
self.routing_fromline_list.clear()
# Remove all annotations which were added (if any)
self._clear_annotations()
if not self.geocoded:
self.routing_fromline_list.clear()
# Remove all annotations which were added (if any)
self._clear_annotations()

self.line_tool = maptools.LineTool(self._iface.mapCanvas())
self._iface.mapCanvas().setMapTool(self.line_tool)
Expand All @@ -559,11 +667,16 @@ def _on_linetool_map_click(self, point, idx):

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}")
p = f"Point {idx}: {point_wgs.x():.6f}, {point_wgs.y():.6f}"
if self.geocoded:
self.routing_fromline_list.insertItem(self.routing_fromline_list.count() - 1, p)
else:
self.routing_fromline_list.addItem(p)

annotation = self._linetool_annotate_point(point, idx)
self.annotations.append(annotation)
self.project.annotationManager().addAnnotation(annotation)
self._reindex_list_items()

def _reindex_list_items(self):
"""Resets the index when an item in the list is moved"""
Expand All @@ -573,7 +686,7 @@ def _reindex_list_items(self):
]
self.routing_fromline_list.clear()
self._clear_annotations()
crs = QgsCoordinateReferenceSystem(f"EPSG:{4326}")
crs = QgsCoordinateReferenceSystem.fromEpsgId("EPSG:4326")
for idx, x in enumerate(items):
coords = x.split(":")[1]
item = f"Point {idx}:{coords}"
Expand Down
22 changes: 20 additions & 2 deletions ORStools/gui/ORStoolsDialogUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ def setupUi(self, ORStoolsDialogBase):
self.horizontalLayout_2.setStretch(1, 3)
self.horizontalLayout_2.setStretch(2, 2)
self.verticalLayout_7.addWidget(self.routing_travel_group)
self.gridLayout_3 = QtWidgets.QGridLayout()
self.gridLayout_3.setContentsMargins(5, 5, 5, 5)
self.gridLayout_3.setObjectName("gridLayout_3")
self.label_6 = QtWidgets.QLabel(self.qwidget)
self.label_6.setObjectName("label_6")
self.gridLayout_3.addWidget(self.label_6, 0, 0, 1, 1)
self.label_7 = QtWidgets.QLabel(self.qwidget)
self.label_7.setObjectName("label_7")
self.gridLayout_3.addWidget(self.label_7, 1, 0, 1, 1)
self.geocode_start = QtWidgets.QLineEdit(self.qwidget)
self.geocode_start.setObjectName("geocode_start")
self.gridLayout_3.addWidget(self.geocode_start, 0, 1, 1, 1)
self.geocode_dest = QtWidgets.QLineEdit(self.qwidget)
self.geocode_dest.setObjectName("geocode_dest")
self.gridLayout_3.addWidget(self.geocode_dest, 1, 1, 1, 1)
self.verticalLayout_7.addLayout(self.gridLayout_3)
self.widget = QtWidgets.QWidget(self.qwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
Expand Down Expand Up @@ -216,7 +232,7 @@ def setupUi(self, ORStoolsDialogBase):
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.advances_group.sizePolicy().hasHeightForWidth())
self.advances_group.setSizePolicy(sizePolicy)
self.advances_group.setMaximumSize(QtCore.QSize(16777215, 20))
self.advances_group.setMaximumSize(QtCore.QSize(16777215, 18))
self.advances_group.setCheckable(False)
self.advances_group.setChecked(False)
self.advances_group.setCollapsed(True)
Expand Down Expand Up @@ -402,7 +418,7 @@ def setupUi(self, ORStoolsDialogBase):
sizePolicy.setHeightForWidth(self.ors_log_group.sizePolicy().hasHeightForWidth())
self.ors_log_group.setSizePolicy(sizePolicy)
self.ors_log_group.setMinimumSize(QtCore.QSize(0, 0))
self.ors_log_group.setMaximumSize(QtCore.QSize(16777215, 20))
self.ors_log_group.setMaximumSize(QtCore.QSize(16777215, 18))
self.ors_log_group.setFlat(True)
self.ors_log_group.setCollapsed(True)
self.ors_log_group.setSaveCollapsedState(False)
Expand Down Expand Up @@ -481,6 +497,8 @@ def retranslateUi(self, ORStoolsDialogBase):
self.routing_travel_label.setText(_translate("ORStoolsDialogBase", "Go with"))
self.routing_travel_combo.setToolTip(_translate("ORStoolsDialogBase", "Mode of travel"))
self.routing_preference_combo.setToolTip(_translate("ORStoolsDialogBase", "Preference"))
self.label_6.setText(_translate("ORStoolsDialogBase", "Start:"))
self.label_7.setText(_translate("ORStoolsDialogBase", "Destination:"))
self.routing_fromline_map.setToolTip(_translate("ORStoolsDialogBase", "<html><head/><body><p>Add wayoints interactively from the map canvas.</p><p>Double-click will terminate waypoint selection.</p></body></html>"))
self.routing_fromline_clear.setToolTip(_translate("ORStoolsDialogBase", "<html><head/><body><p>If waypoints are selected in the list, only these will be deleted. Else all waypoints will be deleted.</p></body></html>"))
self.routing_fromline_list.setToolTip(_translate("ORStoolsDialogBase", "Select waypoints from the map!"))
Expand Down
40 changes: 38 additions & 2 deletions ORStools/gui/ORStoolsDialogUI.ui
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,42 @@
</layout>
</widget>
</item>
<item>
<layout class="QGridLayout" name="gridLayout_3">
<property name="leftMargin">
<number>5</number>
</property>
<property name="topMargin">
<number>5</number>
</property>
<property name="rightMargin">
<number>5</number>
</property>
<property name="bottomMargin">
<number>5</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Start:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Destination:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="geocode_start"/>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="geocode_dest"/>
</item>
</layout>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<property name="sizePolicy">
Expand Down Expand Up @@ -375,7 +411,7 @@
<property name="maximumSize">
<size>
<width>16777215</width>
<height>20</height>
<height>18</height>
</size>
</property>
<property name="title">
Expand Down Expand Up @@ -812,7 +848,7 @@ p, li { white-space: pre-wrap; }
<property name="maximumSize">
<size>
<width>16777215</width>
<height>20</height>
<height>18</height>
</size>
</property>
<property name="title">
Expand Down
Loading