|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +/*************************************************************************** |
| 4 | + ORStools |
| 5 | + A QGIS plugin |
| 6 | + QGIS client to query openrouteservice |
| 7 | + ------------------- |
| 8 | + begin : 2017-02-01 |
| 9 | + git sha : $Format:%H$ |
| 10 | + copyright : (C) 2021 by HeiGIT gGmbH |
| 11 | + |
| 12 | + ***************************************************************************/ |
| 13 | +
|
| 14 | + This plugin provides access to openrouteservice API functionalities |
| 15 | + (https://openrouteservice.org), developed and |
| 16 | + maintained by the openrouteservice team of HeiGIT gGmbH, Germany. By using |
| 17 | + this plugin you agree to the ORS terms of service |
| 18 | + (https://openrouteservice.org/terms-of-service/). |
| 19 | +
|
| 20 | +/*************************************************************************** |
| 21 | + * * |
| 22 | + * This program is free software; you can redistribute it and/or modify * |
| 23 | + * it under the terms of the GNU General Public License as published by * |
| 24 | + * the Free Software Foundation; either version 2 of the License, or * |
| 25 | + * (at your option) any later version. * |
| 26 | + * * |
| 27 | + ***************************************************************************/ |
| 28 | +""" |
| 29 | + |
| 30 | +from typing import Dict |
| 31 | + |
| 32 | +from qgis.core import ( |
| 33 | + QgsWkbTypes, |
| 34 | + QgsFeature, |
| 35 | + QgsField, |
| 36 | + QgsFields, |
| 37 | + QgsCoordinateReferenceSystem, |
| 38 | + QgsProcessingParameterExtent, |
| 39 | + QgsProcessingParameterFeatureSink, |
| 40 | + QgsProcessingContext, |
| 41 | + QgsProcessingFeedback, |
| 42 | + QgsPointXY, |
| 43 | + QgsGeometry, |
| 44 | +) |
| 45 | + |
| 46 | +from qgis.PyQt.QtCore import QVariant |
| 47 | + |
| 48 | + |
| 49 | +from ORStools.common import PROFILES |
| 50 | +from ORStools.utils import exceptions, logger |
| 51 | +from .base_processing_algorithm import ORSBaseProcessingAlgorithm |
| 52 | + |
| 53 | + |
| 54 | +# noinspection PyPep8Naming |
| 55 | +class ORSExportAlgo(ORSBaseProcessingAlgorithm): |
| 56 | + def __init__(self): |
| 57 | + super().__init__() |
| 58 | + self.ALGO_NAME: str = "export_network_from_map" |
| 59 | + self.GROUP: str = "Export" |
| 60 | + self.IN_EXPORT: str = "INPUT_EXPORT" |
| 61 | + self.OUT_POINT = "OUTPUT_POINT" |
| 62 | + self.PARAMETERS: list = [ |
| 63 | + QgsProcessingParameterExtent( |
| 64 | + name=self.IN_EXPORT, |
| 65 | + description=self.tr("Input Extent"), |
| 66 | + ), |
| 67 | + QgsProcessingParameterFeatureSink( |
| 68 | + name=self.OUT_POINT, |
| 69 | + description="Node Export", |
| 70 | + ), |
| 71 | + ] |
| 72 | + |
| 73 | + def processAlgorithm( |
| 74 | + self, parameters: dict, context: QgsProcessingContext, feedback: QgsProcessingFeedback |
| 75 | + ) -> Dict[str, str]: |
| 76 | + ors_client = self._get_ors_client_from_provider(parameters[self.IN_PROVIDER], feedback) |
| 77 | + |
| 78 | + # Get profile value |
| 79 | + profile = dict(enumerate(PROFILES))[parameters[self.IN_PROFILE]] |
| 80 | + |
| 81 | + target_crs = QgsCoordinateReferenceSystem("EPSG:4326") |
| 82 | + rect = self.parameterAsExtent(parameters, self.IN_EXPORT, context, crs=target_crs) |
| 83 | + |
| 84 | + extent = [[rect.xMinimum(), rect.yMinimum()], [rect.xMaximum(), rect.yMaximum()]] |
| 85 | + |
| 86 | + params = { |
| 87 | + "bbox": extent, |
| 88 | + "id": "export_request", |
| 89 | + } |
| 90 | + |
| 91 | + (sink_line, dest_id_line) = self.parameterAsSink( |
| 92 | + parameters, |
| 93 | + self.OUT, |
| 94 | + context, |
| 95 | + self.get_fields_line(), |
| 96 | + QgsWkbTypes.Type.LineString, |
| 97 | + QgsCoordinateReferenceSystem.fromEpsgId(4326), |
| 98 | + ) |
| 99 | + |
| 100 | + (sink_point, dest_id_point) = self.parameterAsSink( |
| 101 | + parameters, |
| 102 | + self.OUT_POINT, |
| 103 | + context, |
| 104 | + self.get_fields_point(), |
| 105 | + QgsWkbTypes.Type.Point, |
| 106 | + QgsCoordinateReferenceSystem.fromEpsgId(4326), |
| 107 | + ) |
| 108 | + |
| 109 | + # Make request and catch ApiError |
| 110 | + try: |
| 111 | + response = ors_client.request("/v2/export/" + profile, {}, post_json=params) |
| 112 | + nodes_dict = {item["nodeId"]: item["location"] for item in response["nodes"]} |
| 113 | + edges = response["edges"] |
| 114 | + for edge in edges: |
| 115 | + from_id = edge["fromId"] |
| 116 | + to_id = edge["toId"] |
| 117 | + weight = edge["weight"] |
| 118 | + |
| 119 | + to_coords = nodes_dict[to_id] |
| 120 | + from_coords = nodes_dict[from_id] |
| 121 | + |
| 122 | + geometry = QgsGeometry.fromPolylineXY( |
| 123 | + [ |
| 124 | + QgsPointXY(from_coords[0], from_coords[1]), |
| 125 | + QgsPointXY(to_coords[0], to_coords[1]), |
| 126 | + ] |
| 127 | + ) |
| 128 | + |
| 129 | + feat = QgsFeature() |
| 130 | + feat.setGeometry(geometry) |
| 131 | + feat.setAttributes([from_id, to_id, weight]) |
| 132 | + sink_line.addFeature(feat) |
| 133 | + |
| 134 | + unique_coordinates = { |
| 135 | + tuple(item["location"]): item["nodeId"] for item in response["nodes"] |
| 136 | + } |
| 137 | + points = [(coords, node_id) for coords, node_id in unique_coordinates.items()] |
| 138 | + for item in points: |
| 139 | + point = QgsPointXY(item[0][0], item[0][1]) |
| 140 | + point_geometry = QgsGeometry.fromPointXY(point) |
| 141 | + |
| 142 | + point_feat = QgsFeature() |
| 143 | + point_feat.setGeometry(point_geometry) |
| 144 | + point_feat.setAttributes([item[1]]) |
| 145 | + sink_point.addFeature(point_feat) |
| 146 | + |
| 147 | + except (exceptions.ApiError, exceptions.InvalidKey, exceptions.GenericServerError) as e: |
| 148 | + msg = f"{e.__class__.__name__}: {str(e)}" |
| 149 | + feedback.reportError(msg) |
| 150 | + logger.log(msg) |
| 151 | + |
| 152 | + return {self.OUT: dest_id_line, self.OUT_POINT: dest_id_point} |
| 153 | + |
| 154 | + @staticmethod |
| 155 | + def get_fields_line(): |
| 156 | + fields = QgsFields() |
| 157 | + fields.append(QgsField("FROM_ID", QVariant.Double)) |
| 158 | + fields.append(QgsField("TO_ID", QVariant.Double)) |
| 159 | + fields.append(QgsField("WEIGHT", QVariant.Double)) |
| 160 | + |
| 161 | + return fields |
| 162 | + |
| 163 | + @staticmethod |
| 164 | + def get_fields_point(): |
| 165 | + fields = QgsFields() |
| 166 | + fields.append(QgsField("ID", QVariant.Int)) |
| 167 | + |
| 168 | + return fields |
| 169 | + |
| 170 | + def displayName(self) -> str: |
| 171 | + """ |
| 172 | + Algorithm name shown in QGIS toolbox |
| 173 | + :return: |
| 174 | + """ |
| 175 | + return self.tr("Export Network from Map") |
0 commit comments