-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting_field_laylines_algorithm OLD.py
325 lines (244 loc) · 15.4 KB
/
plotting_field_laylines_algorithm OLD.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
illini_drainage_tools
A QGIS plugin
Performs Specific Draiange Related Tasks and Analysis on a Site
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2022-03-15
copyright : (C) 2022 by FALASY Anamelechi
email : fvw.services@gmail.com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
__author__ = 'FALASY Anamelechi'
__date__ = '2022-03-15'
__copyright__ = '(C) 2022 by FALASY Anamelechi'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import processing
import os, math
import inspect
import time
import qgis.utils
import numpy as np
from qgis.gui import *
from osgeo import gdal
from PyQt5 import QtWidgets
from osgeo import gdalnumeric
from collections import Counter
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QCoreApplication, QVariant, QObject
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterRasterLayer
from qgis.core import QgsProcessingParameterFolderDestination
from qgis.core import QgsProcessingParameterFileDestination
from qgis.core import QgsProcessingParameterVectorDestination
from qgis.core import QgsProcessingParameterExtent
from qgis.core import QgsProcessingParameterEnum
from qgis.core import QgsProcessingParameterRasterLayer
from qgis.core import QgsProcessingParameterFeatureSource
from qgis.core import QgsProcessingParameterFeatureSink
from qgis.core import QgsProcessingParameterBoolean
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterNumber
from qgis.core import QgsProcessingParameterPoint
from qgis.core import QgsProcessingParameterField
from qgis.core import QgsProcessingParameterCrs
from qgis.core import QgsCoordinateReferenceSystem
from qgis.core import QgsFeatureSink
from qgis.core import QgsFeatureRequest
from qgis.core import QgsVectorLayer
from qgis.core import QgsLineSymbol
from qgis.core import QgsProperty
from qgis.core import QgsProcessingParameterString
from qgis.core import QgsProcessingLayerPostProcessorInterface
from qgis.core import QgsProcessingParameterRasterDestination
from qgis.core import QgsProcessingParameterVectorDestination
from qgis.core import (edit,QgsField, QgsFeature, QgsPointXY, QgsWkbTypes, QgsGeometry, QgsFields)
class PlottingFieldLaylinesAlgorithm(QgsProcessingAlgorithm):
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return PlottingFieldLaylinesAlgorithm()
def name(self):
return 'c. Plot Field Laylines'
def displayName(self):
return self.tr(self.name())
def group(self):
return self.tr(self.groupId())
def groupId(self):
return ''
def icon(self):
cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
icon = QIcon(os.path.join(os.path.join(cmd_folder, 'logo.png')))
return icon
def shortHelpString(self):
return self.tr( """This tool is used to find the surface water flow paths on a field.
Workflow:
1. Select a LiDAR DEM Raster Layer and a Polygon Vector Layer.
2. Specify a Desired Contour Interval (feet)
3. Save the output files (optional)
4. Click on \"Run\"
The script will give out four outputs.
Colors: Laylines/Drain Nets in (Blue) & Contour Lines Nets in (Yellow)
The help link in the Graphical User Interface (GUI) provides more information about the plugin.
""")
def helpUrl(self):
return "https://publish.illinois.edu/illinoisdrainageguide/files/2022/06/PublicAccess.pdf"
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterRasterLayer('HGF', 'Original LiDAR DEM', defaultValue=None))
self.addParameter(QgsProcessingParameterRasterLayer('MDT', 'Thinned LiDAR DEM', defaultValue=None))
self.addParameter(QgsProcessingParameterVectorLayer('VectorPolygonLayer', 'Field Boundary', types=[QgsProcessing.TypeVectorPolygon], defaultValue=None))
self.addParameter(QgsProcessingParameterCrs('CRSZ', 'Specify Layer CRS', defaultValue='EPSG:3435'))
self.addParameter(QgsProcessingParameterNumber('ContourInterval', 'Contour Line Interval (ft)', type=QgsProcessingParameterNumber.Double, maxValue=100.0, defaultValue=1))
self.addParameter(QgsProcessingParameterNumber('RasterDepth', 'Raster Depth Difference (ft)', type=QgsProcessingParameterNumber.Double, maxValue=100.0, defaultValue=1))
self.addParameter(QgsProcessingParameterVectorDestination('UnfilledDEM', 'Unfilled Laylines', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))
self.addParameter(QgsProcessingParameterVectorDestination('FilledContour', 'Filled Contour Lines', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))
self.addParameter(QgsProcessingParameterVectorDestination('FilledDEM', 'Filled Laylines', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))
self.addParameter(QgsProcessingParameterRasterDestination('DeRaster', 'Identified Depression Areas', 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(12, model_feedback)
results = {}
outputs = {}
# Buffer the Boundary Plot
alg_params = {'INPUT': parameters['VectorPolygonLayer'], 'DISTANCE':20, 'SEGMENTS':5, 'END_CAP_STYLE':0, 'JOIN_STYLE:':0, 'MITER_LIMIT':2, 'DISSOLVE':False, 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT}
feedback.setCurrentStep(1)
if feedback.isCanceled():
return {}
outputs['VectorBuffer'] = processing.run('native:buffer', alg_params, context=context, feedback=feedback, is_child_algorithm=True) #1
results['VectorBuffer'] = outputs['VectorBuffer']['OUTPUT']
# Clip Raster DEM Layer Out
alg_params = {'INPUT': parameters['MDT'], 'MASK': results['VectorBuffer'], 'CROP_TO_CUTLINE': True, 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT}
feedback.setCurrentStep(2)
if feedback.isCanceled():
return {}
outputs['ClipRasterbyMaskLayer'] = processing.run('gdal:cliprasterbymasklayer', alg_params, context=context, feedback=feedback, is_child_algorithm=True)#2
results['ClipRasterbyMaskLayer'] = outputs['ClipRasterbyMaskLayer']['OUTPUT']
# Find Channel Network from Terrain Analysis (unfilled DEM)
alg_params = {'ELEVATION': results['ClipRasterbyMaskLayer'], 'INIT_GRID': outputs['ClipRasterbyMaskLayer']['OUTPUT'], 'INIT_METHOD': 2, 'INIT_VALUE': 0, 'DIV_CELLS': 10, 'MINLEN': 10,
'CHNLNTWRK': QgsProcessing.TEMPORARY_OUTPUT, 'CHNLROUTE': QgsProcessing.TEMPORARY_OUTPUT, 'SHAPES': parameters['UnfilledDEM']}
feedback.setCurrentStep(3)
if feedback.isCanceled():
return {}
outputs['ChannelNetwork'] = processing.run('saga:channelnetwork', alg_params, context=context, feedback=feedback, is_child_algorithm=True) #3
results['ChannelNetwork'] = outputs['ChannelNetwork']['SHAPES']
# Define Current Projection
alg_params = {'INPUT': results['ChannelNetwork'], 'CRS': parameters['CRSZ']}
feedback.setCurrentStep(4)
if feedback.isCanceled():
return {}
outputs['ChannelNetwork_A'] = processing.run('qgis:definecurrentprojection', alg_params, context=context, feedback=feedback, is_child_algorithm=True) #4
results['ChannelNetwork_A'] = outputs['ChannelNetwork_A']['INPUT']
# Fill DEM Sinks
alg_params = {'DEM': results['ClipRasterbyMaskLayer'], 'MINSLOPE': 0.03, 'RESULT': QgsProcessing.TEMPORARY_OUTPUT}
feedback.setCurrentStep(5)
if feedback.isCanceled():
return {}
outputs['FillSinks'] = processing.run('saga:fillsinksplanchondarboux2001', alg_params, context=context, feedback=feedback, is_child_algorithm=True) #5
results['FillSinks'] = outputs['FillSinks']['RESULT']
# Find Field Contour
alg_params = {'INPUT': results['FillSinks'], 'BAND': 1, 'INTERVAL': parameters['ContourInterval'], 'FIELD_NAME': 'ELEV', 'OUTPUT': parameters['FilledContour']}
feedback.setCurrentStep(6)
if feedback.isCanceled():
return {}
outputs['Contour'] = processing.run('gdal:contour', alg_params, context=context, feedback=feedback, is_child_algorithm=True) #6
results['Contour'] = outputs['Contour']['OUTPUT']
# Define Current Projection
alg_params = {'INPUT': results['Contour'], 'CRS': parameters['CRSZ']}
feedback.setCurrentStep(7)
if feedback.isCanceled():
return {}
outputs['Contour_A'] = processing.run('qgis:definecurrentprojection', alg_params, context=context, feedback=feedback, is_child_algorithm=True) #7
results['Contour_A'] = outputs['Contour_A']['INPUT']
# Find Channel Network from Terrain Analysis (filled DEM)
alg_params = {'ELEVATION': outputs['FillSinks']['RESULT'], 'INIT_GRID': outputs['FillSinks']['RESULT'], 'INIT_METHOD': 2, 'INIT_VALUE': 0, 'DIV_CELLS': 10, 'MINLEN': 10,
'CHNLNTWRK': QgsProcessing.TEMPORARY_OUTPUT, 'CHNLROUTE': QgsProcessing.TEMPORARY_OUTPUT, 'SHAPES': parameters['FilledDEM']}
feedback.setCurrentStep(8)
if feedback.isCanceled():
return {}
outputs['ChannelNetwork2'] = processing.run('saga:channelnetwork', alg_params, context=context, feedback=feedback, is_child_algorithm=True) #8
results['ChannelNetwork2'] = outputs['ChannelNetwork2']['SHAPES']
# Define Current Projection
alg_params = {'INPUT': results['ChannelNetwork2'], 'CRS': parameters['CRSZ']}
feedback.setCurrentStep(9)
if feedback.isCanceled():
return {}
outputs['ChannelNetwork_B'] = processing.run('qgis:definecurrentprojection', alg_params, context=context, feedback=feedback, is_child_algorithm=True) #9
results['ChannelNetwork_B'] = outputs['ChannelNetwork_B']['INPUT']
# Clip Original Raster DEM Layer Out
alg_params = {'INPUT': parameters['HGF'], 'MASK': results['VectorBuffer'], 'CROP_TO_CUTLINE': True, 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT}
feedback.setCurrentStep(10)
if feedback.isCanceled():
return {}
outputs['ClipLayer'] = processing.run('gdal:cliprasterbymasklayer', alg_params, context=context, feedback=feedback, is_child_algorithm=True)#10
results['ClipLayer'] = outputs['ClipLayer']['OUTPUT']
# Fill DEM Sinks
alg_params = {'DEM': results['ClipLayer'], 'MINSLOPE': 0.03, 'RESULT': QgsProcessing.TEMPORARY_OUTPUT}
feedback.setCurrentStep(11)
if feedback.isCanceled():
return {}
outputs['FillSinkz'] = processing.run('saga:fillsinksplanchondarboux2001', alg_params, context=context, feedback=feedback, is_child_algorithm=True) #11
results['FillSinkz'] = outputs['FillSinkz']['RESULT']
## Raster Calculator
B = outputs['ClipLayer']['OUTPUT']#10
A = outputs['FillSinkz']['RESULT'] #11
alg_params = {
'INPUT_A': results['FillSinkz'],
'BAND_A': 1,
'INPUT_B': results['ClipLayer'],
'FORMULA': "A-B>parameters['RasterDepth']",
'NO_DATA': None,
'RTYPE': 5,
'OUTPUT': parameters['DeRaster']
}
feedback.setCurrentStep(12)
if feedback.isCanceled():
return {}
outputs['RasterCalculator'] = processing.run('gdal:rastercalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True) #12
results['RasterCalculator'] = outputs['RasterCalculator']['OUTPUT']
if context.willLoadLayerOnCompletion(results['Contour']):
context.layerToLoadOnCompletionDetails(results['Contour']).setPostProcessor(GridPostProcessor.create())
if context.willLoadLayerOnCompletion(results['ChannelNetwork2']):
context.layerToLoadOnCompletionDetails(results['ChannelNetwork2']).setPostProcessor(LinePostProcessor.create())
return results
class GridPostProcessor(QgsProcessingLayerPostProcessorInterface):
instance = None
def postProcessLayer(self, layer, context, feedback):
if not isinstance(layer, QgsVectorLayer):
return
renderer = layer.renderer().clone()
symbol = QgsLineSymbol.createSimple({'line_color': '251,247,4,255', 'line_width': '0.45', 'line_style': 'solid'})
renderer.setSymbol(symbol)
layer.setRenderer(renderer)
@staticmethod
def create() -> 'GridPostProcessor':
GridPostProcessor.instance = GridPostProcessor()
return GridPostProcessor.instance
class LinePostProcessor(QgsProcessingLayerPostProcessorInterface):
instance = None
def postProcessLayer(self, layer, context, feedback):
if not isinstance(layer, QgsVectorLayer):
return
renderer = layer.renderer().clone()
symbol = QgsLineSymbol.createSimple({'line_color': '14,186,238,253', 'line_width': '0.66', 'line_style': 'solid'})
renderer.setSymbol(symbol)
layer.setRenderer(renderer)
@staticmethod
def create() -> 'LinePostProcessor':
LinePostProcessor.instance = LinePostProcessor()
return LinePostProcessor.instance