-
Notifications
You must be signed in to change notification settings - Fork 31
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
Add export endpoint #269
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 2557d6a
feat: add functionality of other proc to adapt it
merydian b033649
feat: add main logic
merydian fa197d0
feat: add method to extract coordinates of node by id
merydian bf8bf4b
feat: set fields for output layer
merydian 41336a5
feat: load proc
merydian 88a3263
docs: add german help sidebar
merydian 53a16b1
docs: add help sidebar
merydian ae3bcda
docs: correct algorithm name
merydian 5bfebdc
style: run ruff
merydian 49e871e
feat: remove advanced parameters from export proc
merydian 879659b
style: run ruff
merydian a6dbf5d
docs: add changelog entry
merydian 712fce9
Merge branch 'main' into add-export-endpoint
koebi d3c517c
fix: rename help files
koebi 7bc55cd
feat: pasre nodes to dict for more performance
merydian 2809327
feat: export nodes to layer
merydian 9165814
style: run ruff
merydian 3d3e5c1
test: add export test stub
koebi bb19f07
fix: rename help file
koebi b572bef
feat: rename node export output parameter
koebi cb320a1
fix: set target crs for export extent directly
koebi 1aa153e
feat: add german translation
koebi 6014e42
tests: add export proc test
koebi 288b72d
fix: remove redundant imports
koebi 2c346be
chore: run ruff format
koebi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | ||
|
||
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.