-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineRoute.py
288 lines (247 loc) · 11.6 KB
/
LineRoute.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
# -*- coding: utf-8 -*-
from qgis.PyQt.QtCore import QCoreApplication, QVariant
from qgis.core import (QgsProcessing,
QgsProcessingException,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterField,
QgsProcessingParameterNumber,
QgsField,
QgsExpression,
QgsExpressionContext,
QgsExpressionContextUtils,
edit,
QgsEditError,
QgsProperty)
from qgis import processing
class LineSegmentTimes(QgsProcessingAlgorithm):
# Constants used to refer to parameters and outputs.
INPUT = 'INPUT'
TIMESTAMPS = 'TIMESTAMPS'
SECONDS = 'SECONDS'
OUTPUT = 'OUTPUT'
def tr(self, string):
"""
Returns a translatable string with the self.tr() function.
"""
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return LineSegmentTimes()
def name(self):
"""
Returns the algorithm name, used for identifying the algorithm. This
string should be fixed for the algorithm, and must not be localised.
The name should be unique within each provider. Names should contain
lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return 'linesegmenttimes'
def displayName(self):
"""
Returns the translated algorithm name, which should be used for any
user-visible display of the algorithm name.
"""
return self.tr('Line segment times')
def group(self):
"""
Returns the name of the group this algorithm belongs to. This string
should be localised.
"""
return self.tr('Route processing')
def groupId(self):
"""
Returns the unique ID of the group this algorithm belongs to. This
string should be fixed for the algorithm, and must not be localised.
The group id should be unique within each provider. Group id should
contain lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return 'routeprocessing'
def shortHelpString(self):
"""
Returns a localised short helper string for the algorithm. This string
should provide a basic description about what the algorithm does and the
parameters and outputs associated with it.
"""
return self.tr("Take an input layer of waypoints and create a timed route polyline layer where the length of each line segment corresponds to a given number of seconds.")
def helpUrl(self):
return "https://github.com/hjarnek/kso_model_testing_and_mapplotting/tree/main/geoprocessing"
def AddField(self, layer, field, fieldtype, expr):
expression = QgsExpression(expr)
context = QgsExpressionContext()
context.appendScopes(QgsExpressionContextUtils.globalProjectLayerScopes(layer))
try:
with edit(layer):
layer.dataProvider().addAttributes([QgsField(field, fieldtype)])
layer.updateFields()
for f in layer.getFeatures():
context.setFeature(f)
f[field] = expression.evaluate(context)
layer.updateFeature(f)
except QgsEditError as err:
print(repr(err))
def initAlgorithm(self, config=None):
"""
Here we define the inputs and output of the algorithm, along
with some other properties.
"""
self.addParameter(
QgsProcessingParameterFeatureSource(
self.INPUT,
self.tr('Input point layer'),
types=[QgsProcessing.TypeVectorPoint]
)
)
self.addParameter(
QgsProcessingParameterField(
self.TIMESTAMPS,
self.tr('Timestamp field'),
parentLayerParameterName = self.INPUT,
defaultValue = 'UNIXTime'
)
)
self.addParameter(
QgsProcessingParameterNumber(
self.SECONDS,
self.tr('Seconds corresponding to line segment length'),
type = QgsProcessingParameterNumber.Double,
defaultValue = 1,
minValue = 0.01
)
)
self.addParameter(
QgsProcessingParameterFeatureSink(
self.OUTPUT,
self.tr('Output layer')
)
)
def processAlgorithm(self, parameters, context, feedback):
"""
Here is where the processing itself takes place.
"""
# If any of the required inputs are missing, throw an exception to
# indicate that the algorithm encountered a fatal error.
if self.INPUT is None:
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
if self.TIMESTAMPS is None:
raise QgsProcessingException(self.invalidSourceError(parameters, self.TIMESTAMPS))
if self.SECONDS is None:
raise QgsProcessingException(self.invalidSourceError(parameters, self.SECONDS))
# Retrieve the feature source.
source = self.parameterAsVectorLayer(parameters, self.INPUT, context)
layername = source.id()
fieldname = parameters[self.TIMESTAMPS]
source_first_fid = next(source.getFeatures()).id()
if (source_first_fid != 0) and (source_first_fid != 1):
raise QgsProcessingException('ERROR: The first feature ID of the input \
point layer is neither 0 nor 1. Must be either to continue.')
# Send some information to the user
feedback.pushInfo('CRS is {}'.format(source.sourceCrs().authid()))
feedback.pushInfo('Number of points: {}'.format(source.featureCount()))
layer1_line = processing.run("native:pointstopath", {
'INPUT':parameters[self.INPUT],
'CLOSE_PATH':False,
'ORDER_EXPRESSION':fieldname,
'NATURAL_SORT':False,
'GROUP_EXPRESSION':'',
'OUTPUT':'TEMPORARY_OUTPUT'
}, is_child_algorithm=True, context=context, feedback=feedback)['OUTPUT']
layer2_exploded = processing.run("native:explodelines", {
'INPUT':layer1_line,
'OUTPUT':'TEMPORARY_OUTPUT'
}, is_child_algorithm=True, context=context, feedback=feedback)['OUTPUT']
layer2_exploded_layerobject = context.getMapLayer(layer2_exploded)
first_fid = next(layer2_exploded_layerobject.getFeatures()).id()
diff = source_first_fid - first_fid
expr_enter = 'attributes(get_feature_by_id(\'{}\', ($id+{})))[\'{}\']'\
.format(layername, diff, fieldname)
expr_exit = 'attributes(get_feature_by_id(\'{}\', ($id+{}+1)))[\'{}\']'\
.format(layername, diff, fieldname)
expr_speed = '$length / ((attributes(get_feature_by_id(\'{}\', ($id+1+{})\
))[\'{}\']) - (attributes(get_feature_by_id(\'{}\', ($id+{})))[\'{}\']))'\
.format(layername, diff, fieldname, layername, diff, fieldname)
self.AddField(layer2_exploded_layerobject, 'id', QVariant.Int, '$id')
self.AddField(layer2_exploded_layerobject, 'enter', QVariant.Double, expr_enter)
self.AddField(layer2_exploded_layerobject, 'exit', QVariant.Double, expr_exit)
self.AddField(layer2_exploded_layerobject, 'speed', QVariant.Double, expr_speed)
f1id = layer2_exploded_layerobject.fields().lookupField('begin')
f2id = layer2_exploded_layerobject.fields().lookupField('end')
layer2_exploded_layerobject.dataProvider().deleteAttributes([f1id, f2id])
layer2_exploded_layerobject.updateFields()
layer3_interpolated = processing.run("native:pointsalonglines", {
'INPUT':layer2_exploded,
'DISTANCE':QgsProperty.fromExpression('\"speed\" * {}'.format(parameters[self.SECONDS])),
'START_OFFSET':0,
'END_OFFSET':0,
'OUTPUT':'TEMPORARY_OUTPUT'
}, is_child_algorithm=True, context=context, feedback=feedback)['OUTPUT']
expr_enter = '\"enter\" + (\"distance\" / \"speed\")'
expr_exit = 'array_agg(\"enter\")[(@row_number+1)]'
layer4_fieldcalc = processing.run("native:fieldcalculator", {
'INPUT':layer3_interpolated,
'FIELD_NAME':'enter',
'FIELD_TYPE':0,
'FIELD_LENGTH':0,
'FIELD_PRECISION':0,
'FORMULA':expr_enter,
'OUTPUT':'TEMPORARY_OUTPUT'
}, is_child_algorithm=True, context=context, feedback=feedback)['OUTPUT']
layer5_fieldcalc = processing.run("native:fieldcalculator", {
'INPUT':layer4_fieldcalc,
'FIELD_NAME':'exit',
'FIELD_TYPE':0,
'FIELD_LENGTH':0,
'FIELD_PRECISION':0,
'FORMULA':expr_exit,
'OUTPUT':'TEMPORARY_OUTPUT'
}, is_child_algorithm=True, context=context, feedback=feedback)['OUTPUT']
layer6_fieldcalc = processing.run("native:fieldcalculator", {
'INPUT':layer5_fieldcalc,
'FIELD_NAME':'id',
'FIELD_TYPE':1,
'FIELD_LENGTH':0,
'FIELD_PRECISION':0,
'FORMULA':'$id',
'OUTPUT':'TEMPORARY_OUTPUT'
}, is_child_algorithm=True, context=context, feedback=feedback)['OUTPUT']
layer7_line = processing.run("native:pointstopath", {
'INPUT':layer6_fieldcalc,
'CLOSE_PATH':False,
'ORDER_EXPRESSION':'\"enter\"',
'NATURAL_SORT':False,
'GROUP_EXPRESSION':'',
'OUTPUT':'TEMPORARY_OUTPUT'
}, is_child_algorithm=True, context=context, feedback=feedback)['OUTPUT']
layer8_exploded = processing.run("native:explodelines", {
'INPUT':layer7_line,
'OUTPUT':'TEMPORARY_OUTPUT'
}, is_child_algorithm=True, context=context, feedback=feedback)['OUTPUT']
layer9_fieldcalc = processing.run("native:fieldcalculator", {
'INPUT':layer8_exploded,
'FIELD_NAME':'id',
'FIELD_TYPE':1,
'FIELD_LENGTH':0,
'FIELD_PRECISION':0,
'FORMULA':'$id',
'OUTPUT':'TEMPORARY_OUTPUT'
}, is_child_algorithm=True, context=context, feedback=feedback)['OUTPUT']
layer10_droppedfields = processing.run("native:retainfields", {
'INPUT':layer9_fieldcalc,
'FIELDS':['id'],
'OUTPUT':'TEMPORARY_OUTPUT'
}, is_child_algorithm=True, context=context, feedback=feedback)['OUTPUT']
layer11_joined = processing.run("native:joinattributestable", {
'INPUT':layer10_droppedfields,
'FIELD':'id',
'INPUT_2':layer6_fieldcalc,
'FIELD_2':'id',
'FIELDS_TO_COPY':['enter','exit','speed','distance','angle'],
'METHOD':1,
'DISCARD_NONMATCHING':False,
'PREFIX':'',
'OUTPUT':parameters[self.OUTPUT]
}, is_child_algorithm=True, context=context, feedback=feedback)['OUTPUT']
layer11_joined_layerobject = context.getMapLayer(layer11_joined)
layer11_joined_layerobject.setName('Interpolated line route')
return {self.OUTPUT: layer11_joined}