-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubnet Areas.py
303 lines (257 loc) · 11.5 KB
/
Subnet Areas.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
"""
Subnet Areas
v0.2
By Matt Marotta
2020-05-24
With QGIS : 31202
This script takes a road and point layer and creates areas that are closest to the points.
Requirements:
1. Always run these from the Processing Toolbox, not the model editor. In the Processing toolbox,
click the gears icon and select "Add Model to Toolbox", or the python icon and select "Add Script
to Toolbox". Then just load in the model3/python file.
2. When running the model from either the model file or python script, the "network_allocation"
output must be saved to a file. Temporary Layer will cause the model execution to fail.
3. Output may load but look blank - you need to change the CRS of the layer in the later properties.
"""
from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterVectorDestination
from qgis.core import QgsProcessingParameterFeatureSink
import processing
class SubnetAreas(QgsProcessingAlgorithm):
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterVectorLayer('points', 'Points', types=[QgsProcessing.TypeVectorPoint], defaultValue=None))
self.addParameter(QgsProcessingParameterVectorLayer('roads', 'Roads', types=[QgsProcessing.TypeVectorLine], defaultValue=None))
self.addParameter(QgsProcessingParameterVectorDestination('Network_allocation', 'network_allocation', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))
self.addParameter(QgsProcessingParameterFeatureSink('Subnet_areas', 'subnet_areas', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))
def processAlgorithm(self, parameters, context, model_feedback):
# Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
# overall progress through the model
feedback = QgsProcessingMultiStepFeedback(16, model_feedback)
results = {}
outputs = {}
# v.net.alloc
alg_params = {
'-g': False,
'GRASS_MIN_AREA_PARAMETER': 0.0001,
'GRASS_OUTPUT_TYPE_PARAMETER': 0,
'GRASS_REGION_PARAMETER': None,
'GRASS_SNAP_TOLERANCE_PARAMETER': -1,
'GRASS_VECTOR_DSCO': '',
'GRASS_VECTOR_EXPORT_NOCAT': False,
'GRASS_VECTOR_LCO': '',
'arc_backward_column': '',
'arc_column': '',
'arc_type': [0,1],
'center_cats': '1-100000',
'input': parameters['roads'],
'node_column': '',
'points': parameters['points'],
'threshold': 500,
'output': parameters['Network_allocation']
}
outputs['Vnetalloc'] = processing.run('grass7:v.net.alloc', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
results['Network_allocation'] = outputs['Vnetalloc']['output']
feedback.setCurrentStep(1)
if feedback.isCanceled():
return {}
# Explode lines
alg_params = {
'INPUT': outputs['Vnetalloc']['output'],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['ExplodeLines'] = processing.run('native:explodelines', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(2)
if feedback.isCanceled():
return {}
# Extract vertices
alg_params = {
'INPUT': outputs['ExplodeLines']['OUTPUT'],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['ExtractVertices'] = processing.run('native:extractvertices', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(3)
if feedback.isCanceled():
return {}
# Join attributes by location (summary)
alg_params = {
'DISCARD_NONMATCHING': False,
'INPUT': outputs['ExplodeLines']['OUTPUT'],
'JOIN': outputs['ExtractVertices']['OUTPUT'],
'JOIN_FIELDS': ['cat'],
'PREDICATE': [0],
'SUMMARIES': [1],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['JoinAttributesByLocationSummary'] = processing.run('qgis:joinbylocationsummary', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(4)
if feedback.isCanceled():
return {}
# outlen
alg_params = {
'FIELD_LENGTH': 20,
'FIELD_NAME': 'outlen',
'FIELD_PRECISION': 8,
'FIELD_TYPE': 0,
'FORMULA': 'if(\"cat_unique\" > 1, length($geometry) - (length($geometry) * 0.005), length($geometry))',
'INPUT': outputs['JoinAttributesByLocationSummary']['OUTPUT'],
'NEW_FIELD': True,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['Outlen'] = processing.run('qgis:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(5)
if feedback.isCanceled():
return {}
# x0
alg_params = {
'FIELD_LENGTH': 10,
'FIELD_NAME': 'x0',
'FIELD_PRECISION': 3,
'FIELD_TYPE': 0,
'FORMULA': 'x(centroid($geometry)) - \n (outlen/2) * sin(radians(angle_at_vertex($geometry, 0)))',
'INPUT': outputs['Outlen']['OUTPUT'],
'NEW_FIELD': True,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['X0'] = processing.run('qgis:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(6)
if feedback.isCanceled():
return {}
# y0
alg_params = {
'FIELD_LENGTH': 10,
'FIELD_NAME': 'y0',
'FIELD_PRECISION': 3,
'FIELD_TYPE': 0,
'FORMULA': 'y(centroid($geometry)) - \n (outlen/2) * cos(radians(angle_at_vertex($geometry, 0)))\n',
'INPUT': outputs['X0']['OUTPUT'],
'NEW_FIELD': True,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['Y0'] = processing.run('qgis:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(7)
if feedback.isCanceled():
return {}
# x1
alg_params = {
'FIELD_LENGTH': 10,
'FIELD_NAME': 'x1',
'FIELD_PRECISION': 3,
'FIELD_TYPE': 0,
'FORMULA': 'x(centroid($geometry)) + \n (outlen/2) * sin(radians(angle_at_vertex($geometry, 0)))\n',
'INPUT': outputs['Y0']['OUTPUT'],
'NEW_FIELD': True,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['X1'] = processing.run('qgis:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(8)
if feedback.isCanceled():
return {}
# y1
alg_params = {
'FIELD_LENGTH': 10,
'FIELD_NAME': 'y1',
'FIELD_PRECISION': 3,
'FIELD_TYPE': 0,
'FORMULA': 'y(centroid($geometry)) + \n (outlen/2) * cos(radians(angle_at_vertex($geometry, 0)))',
'INPUT': outputs['X1']['OUTPUT'],
'NEW_FIELD': True,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['Y1'] = processing.run('qgis:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(9)
if feedback.isCanceled():
return {}
# Geometry by expression
alg_params = {
'EXPRESSION': 'geom_from_wkt(\'Linestring(\'||\"x1\"||\' \'||\"y1\"||\',\'||\"x0\"||\' \'||\"y0\"||\')\')',
'INPUT': outputs['Y1']['OUTPUT'],
'OUTPUT_GEOMETRY': 1,
'WITH_M': False,
'WITH_Z': False,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['GeometryByExpression'] = processing.run('native:geometrybyexpression', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(10)
if feedback.isCanceled():
return {}
# Extract vertices 2
alg_params = {
'INPUT': outputs['GeometryByExpression']['OUTPUT'],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['ExtractVertices2'] = processing.run('native:extractvertices', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(11)
if feedback.isCanceled():
return {}
# x
alg_params = {
'FIELD_LENGTH': 10,
'FIELD_NAME': 'x',
'FIELD_PRECISION': 1,
'FIELD_TYPE': 0,
'FORMULA': 'round(x($geometry),1)',
'INPUT': outputs['ExtractVertices2']['OUTPUT'],
'NEW_FIELD': True,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['X'] = processing.run('qgis:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(12)
if feedback.isCanceled():
return {}
# y
alg_params = {
'FIELD_LENGTH': 10,
'FIELD_NAME': 'y',
'FIELD_PRECISION': 1,
'FIELD_TYPE': 0,
'FORMULA': 'round(y($geometry),1)',
'INPUT': outputs['X']['OUTPUT'],
'NEW_FIELD': True,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['Y'] = processing.run('qgis:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(13)
if feedback.isCanceled():
return {}
# Delete duplicates by attribute
alg_params = {
'FIELDS': ['cat_unique','x','y'],
'INPUT': outputs['Y']['OUTPUT'],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['DeleteDuplicatesByAttribute'] = processing.run('native:removeduplicatesbyattribute', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(14)
if feedback.isCanceled():
return {}
# Voronoi polygons
alg_params = {
'BUFFER': 0,
'INPUT': outputs['DeleteDuplicatesByAttribute']['OUTPUT'],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['VoronoiPolygons'] = processing.run('qgis:voronoipolygons', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(15)
if feedback.isCanceled():
return {}
# Dissolve
alg_params = {
'FIELD': ['cat'],
'INPUT': outputs['VoronoiPolygons']['OUTPUT'],
'OUTPUT': parameters['Subnet_areas']
}
outputs['Dissolve'] = processing.run('native:dissolve', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
results['Subnet_areas'] = outputs['Dissolve']['OUTPUT']
return results
def name(self):
return 'Subnet Areas'
def displayName(self):
return 'Subnet Areas'
def group(self):
return ''
def groupId(self):
return ''
def createInstance(self):
return SubnetAreas()