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 export endpoint #269

Merged
merged 26 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a400dec
Create export_proc.py
merydian Jul 1, 2024
2557d6a
feat: add functionality of other proc to adapt it
merydian Jul 1, 2024
b033649
feat: add main logic
merydian Jul 3, 2024
fa197d0
feat: add method to extract coordinates of node by id
merydian Jul 3, 2024
bf8bf4b
feat: set fields for output layer
merydian Jul 3, 2024
41336a5
feat: load proc
merydian Jul 3, 2024
88a3263
docs: add german help sidebar
merydian Jul 3, 2024
53a16b1
docs: add help sidebar
merydian Jul 3, 2024
ae3bcda
docs: correct algorithm name
merydian Jul 4, 2024
5bfebdc
style: run ruff
merydian Jul 4, 2024
49e871e
feat: remove advanced parameters from export proc
merydian Jul 6, 2024
879659b
style: run ruff
merydian Jul 6, 2024
a6dbf5d
docs: add changelog entry
merydian Jul 6, 2024
712fce9
Merge branch 'main' into add-export-endpoint
koebi Sep 13, 2024
d3c517c
fix: rename help files
koebi Sep 16, 2024
7bc55cd
feat: pasre nodes to dict for more performance
merydian Oct 15, 2024
2809327
feat: export nodes to layer
merydian Oct 15, 2024
9165814
style: run ruff
merydian Oct 15, 2024
3d3e5c1
test: add export test stub
koebi Oct 18, 2024
bb19f07
fix: rename help file
koebi Oct 18, 2024
b572bef
feat: rename node export output parameter
koebi Oct 18, 2024
cb320a1
fix: set target crs for export extent directly
koebi Oct 18, 2024
1aa153e
feat: add german translation
koebi Oct 18, 2024
6014e42
tests: add export proc test
koebi Oct 18, 2024
288b72d
fix: remove redundant imports
koebi Oct 18, 2024
2c346be
chore: run ruff format
koebi Oct 18, 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
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
- Processing algorithms for the Network Export endpoint ([#210](https://github.com/GIScience/orstools-qgis-plugin/issues/210))

## [1.8.3] - 2024-05-29

### Fixed
Expand Down
9 changes: 9 additions & 0 deletions ORStools/help/export_from_map.help
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Export the base graph for different modes of transport.

You need to have a valid API key ('Web' menu ► 'ORS Tools' ► 'Configuration') or sign up at <a href="https://openrouteservice.org/sign-up/">https://openrouteservice.org/sign-up/</a>.
Current <a href="https://openrouteservice.org/restrictions/">restriction limits</a> for the openrouteservice API apply.

<i>Travel Mode</i>: determines the profile used.

<i>Input Extent</i>: Choose an extent, the content of which will be exported.

7 changes: 7 additions & 0 deletions ORStools/help/export_from_map_de.help
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Das Basisgraph für verschiedene Verkehrsmittel exportieren.

Ein gültiger API-Schlüssel ist erforderlich ('Web'-Menü ► 'ORS Tools' ► 'Konfiguration') oder eine Anmeldung unter <a href="https://openrouteservice.org/sign-up/">https://openrouteservice.org/sign-up/</a>. Aktuelle <a href="https://openrouteservice.org/restrictions/">Beschränkungslimits</a> für die openrouteservice API gelten.

<i>Verkehrsmittel</i>: bestimmt das genutzte Reise-Profil

<i>Input-Extent</i>: Es ist ein Bereich auszuwählen, dessen Inhalt exportiert wird.
19 changes: 13 additions & 6 deletions ORStools/proc/base_processing_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,19 @@ def initAlgorithm(self, configuration: Dict) -> None:
Combines default and algorithm parameters and adds them in order to the
algorithm dialog window.
"""
parameters = (
[self.provider_parameter(), self.profile_parameter()]
+ self.PARAMETERS
+ self.option_parameters()
+ [self.output_parameter()]
)
if self.ALGO_NAME not in ["export_network_from_map"]:
parameters = (
[self.provider_parameter(), self.profile_parameter()]
+ self.PARAMETERS
+ self.option_parameters()
+ [self.output_parameter()]
)
else:
parameters = (
[self.provider_parameter(), self.profile_parameter()]
+ self.PARAMETERS
+ [self.output_parameter()]
)
for param in parameters:
if param.name() in ADVANCED_PARAMETERS:
if self.GROUP == "Matrix":
Expand Down
168 changes: 168 additions & 0 deletions ORStools/proc/export_proc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# -*- coding: utf-8 -*-
"""
/***************************************************************************
ORStools
A QGIS plugin
QGIS client to query openrouteservice
-------------------
begin : 2017-02-01
git sha : $Format:%H$
copyright : (C) 2021 by HeiGIT gGmbH
email : [email protected]
***************************************************************************/

This plugin provides access to openrouteservice API functionalities
(https://openrouteservice.org), developed and
maintained by the openrouteservice team of HeiGIT gGmbH, Germany. By using
this plugin you agree to the ORS terms of service
(https://openrouteservice.org/terms-of-service/).

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""

from typing import Dict

from qgis.core import (
QgsWkbTypes,
QgsFeature,
QgsField,
QgsFields,
QgsProject,
QgsCoordinateTransform,
QgsCoordinateReferenceSystem,
QgsProcessingParameterExtent,
QgsProcessingContext,
QgsProcessingFeedback,
QgsPointXY,
QgsGeometry,
)

from qgis.PyQt.QtCore import QVariant

from qgis.utils import iface


from ORStools.common import PROFILES
from ORStools.utils import exceptions, logger
from .base_processing_algorithm import ORSBaseProcessingAlgorithm


# noinspection PyPep8Naming
class ORSExportAlgo(ORSBaseProcessingAlgorithm):
def __init__(self):
super().__init__()
self.ALGO_NAME: str = "export_network_from_map"
self.GROUP: str = "Export"
self.IN_EXPORT: str = "INPUT_EXPORT"
self.PARAMETERS: list = [
QgsProcessingParameterExtent(
name=self.IN_EXPORT,
description=self.tr("Input Extent"),
),
]

def processAlgorithm(
self, parameters: dict, context: QgsProcessingContext, feedback: QgsProcessingFeedback
) -> Dict[str, str]:
ors_client = self._get_ors_client_from_provider(parameters[self.IN_PROVIDER], feedback)

# Get profile value
profile = dict(enumerate(PROFILES))[parameters[self.IN_PROFILE]]

rect = self.parameterAsExtent(parameters, self.IN_EXPORT, context)

target_crs = QgsCoordinateReferenceSystem("EPSG:4326")
source_crs = iface.mapCanvas().mapSettings().destinationCrs()

transform = QgsCoordinateTransform(source_crs, target_crs, QgsProject.instance())

bottom_left = transform.transform(rect.xMinimum(), rect.yMinimum())
top_right = transform.transform(rect.xMaximum(), rect.yMaximum())

extent = [[bottom_left.x(), bottom_left.y()], [top_right.x(), top_right.y()]]

params = {
"bbox": extent,
"id": "export_request",
}

sink_fields = self.get_fields()

(sink, dest_id) = self.parameterAsSink(
parameters,
self.OUT,
context,
sink_fields,
QgsWkbTypes.Type.LineString,
QgsCoordinateReferenceSystem.fromEpsgId(4326),
)

# Make request and catch ApiError
try:
response = ors_client.request("/v2/export/" + profile, {}, post_json=params)
edges = response["edges"]
for edge in edges:
from_id = edge["fromId"]
to_id = edge["toId"]
weight = edge["weight"]

to_coords = self.get_location_by_id(to_id, response["nodes"])
from_coords = self.get_location_by_id(from_id, response["nodes"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This iterates all nodes twice for every edge, taking what is in theory a linear operation and making it quadratic.

I think it makes sense to parse nodes into a dict to look up coordinates easily.


geometry = QgsGeometry.fromPolylineXY(
[
QgsPointXY(from_coords[0], from_coords[1]),
QgsPointXY(to_coords[0], to_coords[1]),
]
)

feat = QgsFeature()
feat.setGeometry(geometry)
feat.setAttributes([from_id, to_id, weight])
sink.addFeature(feat)

except (exceptions.ApiError, exceptions.InvalidKey, exceptions.GenericServerError) as e:
msg = f"{e.__class__.__name__}: {str(e)}"
feedback.reportError(msg)
logger.log(msg)

return {self.OUT: dest_id}

@staticmethod
def get_fields():
fields = QgsFields()
fields.append(QgsField("FROM_ID", QVariant.Double))
fields.append(QgsField("TO_ID", QVariant.Double))
fields.append(QgsField("WEIGHT", QVariant.Double))

return fields

def get_location_by_id(self, node_id, nodes):
"""
Get the location of a node by its ID.

Args:
node_id (int): The ID of the node.
nodes (list): The list of nodes.

Returns:
list: The location of the node, or None if the node ID is not found.
"""
for node in nodes:
if node["nodeId"] == node_id:
return node["location"]
return None

def displayName(self) -> str:
"""
Algorithm name shown in QGIS toolbox
:return:
"""
return self.tr("Export Network from Map")
2 changes: 2 additions & 0 deletions ORStools/proc/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from .directions_lines_proc import ORSDirectionsLinesAlgo
from .directions_points_layer_proc import ORSDirectionsPointsLayerAlgo
from .directions_points_layers_proc import ORSDirectionsPointsLayersAlgo
from .export_proc import ORSExportAlgo
from .isochrones_layer_proc import ORSIsochronesLayerAlgo
from .isochrones_point_proc import ORSIsochronesPointAlgo
from .matrix_proc import ORSMatrixAlgo
Expand Down Expand Up @@ -63,6 +64,7 @@ def loadAlgorithms(self) -> None:
self.addAlgorithm(ORSIsochronesLayerAlgo())
self.addAlgorithm(ORSIsochronesPointAlgo())
self.addAlgorithm(ORSMatrixAlgo())
self.addAlgorithm(ORSExportAlgo())

@staticmethod
def icon() -> QIcon:
Expand Down
Loading