-
Notifications
You must be signed in to change notification settings - Fork 3
/
matrix_auto.py
326 lines (270 loc) · 11.3 KB
/
matrix_auto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Valhalla - QGIS plugin
QGIS client to query Valhalla APIs
-------------------
begin : 2019-10-12
git sha : $Format:%H$
copyright : (C) 2020 by Nils Nolde
email : nils@gis-ops.com
***************************************************************************/
This plugin provides access to some of the APIs from Valhalla
(https://github.com/valhalla/valhalla), developed and
maintained by https://gis-ops.com, Berlin, Germany.
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
import os.path
from PyQt5.QtGui import QIcon
from qgis.core import (QgsWkbTypes,
QgsCoordinateReferenceSystem,
QgsProcessingException,
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterField,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterEnum,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterDefinition,
QgsProcessingParameterMapLayer,
)
from .. import HELP_DIR
from ... import RESOURCE_PREFIX, __help__
from ...common import client, matrix_core
from ...utils import configmanager, transform, exceptions, logger
from ..costing_params import CostingAuto
from ..request_builder import get_locations, get_costing_options, get_avoid_locations
class ValhallaMatrixCarAlgo(QgsProcessingAlgorithm):
ALGO_NAME = 'matrix_auto'
ALGO_NAME_LIST = ALGO_NAME.split('_')
HELP = 'algorithm_directions_points.help'
COSTING = CostingAuto
PROFILE = 'auto'
MODE_TYPES = ['Fastest', 'Shortest']
IN_PROVIDER = "INPUT_PROVIDER"
IN_START = "INPUT_START_LAYER"
IN_START_FIELD = "INPUT_START_FIELD"
IN_END = "INPUT_END_LAYER"
IN_END_FIELD = "INPUT_END_FIELD"
IN_MODE = "INPUT_MODE"
IN_AVOID = "avoid_locations"
OUT = 'OUTPUT'
def __init__(self):
super(ValhallaMatrixCarAlgo, self).__init__()
self.providers = configmanager.read_config()['providers']
self.costing_options = self.COSTING()
def initAlgorithm(self, configuration, p_str=None, Any=None, *args, **kwargs):
providers = [provider['name'] for provider in self.providers]
self.addParameter(
QgsProcessingParameterEnum(
self.IN_PROVIDER,
"Provider",
providers,
defaultValue=providers[0]
)
)
self.addParameter(
QgsProcessingParameterFeatureSource(
name=self.IN_START,
description="Input Start Point layer",
types=[QgsProcessing.TypeVectorPoint],
)
)
self.addParameter(
QgsProcessingParameterField(
name=self.IN_START_FIELD,
description="Start ID Field (can be used for joining)",
parentLayerParameterName=self.IN_START,
)
)
self.addParameter(
QgsProcessingParameterFeatureSource(
name=self.IN_END,
description="Input End Point layer",
types=[QgsProcessing.TypeVectorPoint],
)
)
self.addParameter(
QgsProcessingParameterField(
name=self.IN_END_FIELD,
description="End ID Field (can be used for joining)",
parentLayerParameterName=self.IN_END,
)
)
self.addParameter(
QgsProcessingParameterEnum(
self.IN_MODE,
'Mode',
options=self.MODE_TYPES,
defaultValue=self.MODE_TYPES[0]
)
)
self.addParameter(
QgsProcessingParameterFeatureSource(
name=self.IN_AVOID,
description="Point layer with locations to avoid",
types=[QgsProcessing.TypeVectorPoint],
optional=True
)
)
advanced = self.costing_options.get_costing_params()
for p in advanced:
p.setFlags(p.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(p)
self.addParameter(
QgsProcessingParameterFeatureSink(
name=self.OUT,
description="Matrix " + self.PROFILE.capitalize(),
createByDefault=False
)
)
def group(self):
return self.PROFILE.capitalize()
def groupId(self):
return self.PROFILE
def name(self):
return self.ALGO_NAME
def shortHelpString(self):
"""Displays the sidebar help in the algorithm window"""
file = os.path.join(
HELP_DIR,
self.HELP
)
with open(file) as helpf:
msg = helpf.read()
return msg
def helpUrl(self):
"""will be connected to the Help button in the Algorithm window"""
return __help__
def displayName(self):
return " ".join(map(lambda x: x.capitalize(), self.ALGO_NAME_LIST))
def icon(self):
return QIcon(RESOURCE_PREFIX + 'icon_matrix.png')
def createInstance(self):
return ValhallaMatrixCarAlgo()
def processAlgorithm(self, parameters, context, feedback):
# Init ORS client
providers = configmanager.read_config()['providers']
provider = providers[self.parameterAsEnum(parameters, self.IN_PROVIDER, context)]
clnt = client.Client(provider)
clnt.overQueryLimit.connect(lambda: feedback.reportError("OverQueryLimit: Retrying"))
mode = self.MODE_TYPES[self.parameterAsEnum(parameters, self.IN_MODE, context)]
# Get parameter values
source = self.parameterAsSource(
parameters,
self.IN_START,
context
)
source_field_name = self.parameterAsString(
parameters,
self.IN_START_FIELD,
context
)
destination = self.parameterAsSource(
parameters,
self.IN_END,
context
)
destination_field_name = self.parameterAsString(
parameters,
self.IN_END_FIELD,
context
)
avoid_layer = self.parameterAsSource(
parameters,
self.IN_AVOID,
context
)
# Get fields from field name
source_field_id = source.fields().lookupField(source_field_name)
source_field = source.fields().field(source_field_id)
destination_field_id = destination.fields().lookupField(destination_field_name)
destination_field = destination.fields().field(destination_field_id)
(sink, dest_id) = self.parameterAsSink(
parameters,
self.OUT,
context,
matrix_core.get_fields(
source_field.type(),
destination_field.type()
),
QgsWkbTypes.NoGeometry
)
# Abort when MultiPoint type
if (source.wkbType() or destination.wkbType()) == 4:
raise QgsProcessingException("TypeError: Multipoint Layers are not accepted. Please convert to single geometry layer.")
# Get feature amounts/counts
sources_amount = source.featureCount()
destinations_amount = destination.featureCount()
if (sources_amount or destinations_amount) > 10000:
raise QgsProcessingException(
"ProcessingError: Too large input, please decimate."
)
sources_features = list(source.getFeatures())
destinations_features = list(destination.getFeatures())
# Get source and destination features
xformer_source = transform.transformToWGS(source.sourceCrs())
sources_points = [xformer_source.transform(feat.geometry().asPoint()) for feat in sources_features]
xformer_destination = transform.transformToWGS(destination.sourceCrs())
destination_points = [xformer_destination.transform(feat.geometry().asPoint()) for feat in destinations_features]
# Build params
params = dict(
costing=self.PROFILE
)
# Sets all advanced parameters as attributes of self.costing_options
self.costing_options.set_costing_options(self, parameters, context)
costing_params = get_costing_options(self.costing_options, self.PROFILE, mode)
if costing_params:
params['costing_options'] = costing_params
if avoid_layer:
params['avoid_locations'] = get_avoid_locations(avoid_layer)
sources_attributes = [feat.attribute(source_field_name) for feat in sources_features]
destinations_attributes = [feat.attribute(destination_field_name) for feat in destinations_features]
source_attr_iter = self._chunks(sources_attributes, 50)
for sources in self._chunks(sources_points, 50):
params["sources"] = get_locations(sources)
source_attributes = next(source_attr_iter)
destination_attr_iter = self._chunks(destinations_attributes, 50)
for destinations in self._chunks(destination_points, 50):
params["targets"] = get_locations(destinations)
params["id"] = "matrix"
destination_attributes = next(destination_attr_iter)
# Make request and catch ApiError
try:
response = clnt.request('/sources_to_targets', post_json=params)
except (exceptions.ApiError) as e:
msg = "{}: {}".format(
e.__class__.__name__,
str(e))
feedback.reportError(msg)
logger.log(msg)
continue
except (exceptions.InvalidKey, exceptions.GenericServerError) as e:
msg = "{}:\n{}".format(
e.__class__.__name__,
str(e))
logger.log(msg)
raise
feats = matrix_core.get_output_features_matrix(
response,
self.PROFILE,
costing_params,
False,
source_attributes,
destination_attributes
)
for feat in feats:
sink.addFeature(feat)
return {self.OUT: dest_id}
@staticmethod
def _chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]