-
Notifications
You must be signed in to change notification settings - Fork 1
/
integrated_process.py
149 lines (136 loc) · 4.65 KB
/
integrated_process.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
# -*- coding: utf-8 -*-
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterRasterDestination,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterVectorDestination,
QgsCoordinateReferenceSystem)
from qgis import processing
class FarmReliefProcessingAlgorithm(QgsProcessingAlgorithm):
DEM = 'dem'
FARM_SHP = 'farm_shape'
CLIP_DEM = 'clipped_dem'
FILL_DEM = 'filled_dem'
SLOPE = 'slope'
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return FarmReliefProcessingAlgorithm()
def name(self):
return 'farm_relief'
def displayName(self):
return self.tr('Farm Relief')
def shortHelpString(self):
return self.tr("Clip DEM according to a farm shape and then calculate slope values for the area of interest")
def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterFeatureSource(
self.FARM_SHP,
self.tr('Input Farm Shape'),
[QgsProcessing.TypeVectorAnyGeometry]
)
)
self.addParameter(
QgsProcessingParameterRasterLayer(
self.DEM,
self.tr('Input Raster DEM'),
[QgsProcessing.TypeRaster]
)
)
self.addParameter(
QgsProcessingParameterRasterDestination(
self.CLIP_DEM,
self.tr('Output Clipped DEM'),
[QgsProcessing.TypeRaster]
)
)
self.addParameter(
QgsProcessingParameterRasterDestination(
self.FILL_DEM,
self.tr('Output Fill Dem'),
[QgsProcessing.TypeRaster]
)
)
self.addParameter(
QgsProcessingParameterRasterDestination(
self.SLOPE,
self.tr('Output Slope Raster'),
[QgsProcessing.TypeRaster]
)
)
def processAlgorithm(self, parameters, context, feedback):
farm_shape= self.parameterAsSource(
parameters,
self.FARM_SHP,
context
)
dem = self.parameterAsRasterLayer(
parameters,
self.DEM,
context
)
clipped_dem= self.parameterAsRasterLayer(
parameters,
self.CLIP_DEM,
context
)
filled_dem= self.parameterAsRasterLayer(
parameters,
self.FILL_DEM,
context
)
feedback.pushInfo('Starting clipping')
clipping = processing.runAndLoadResults(
'gdal:cliprasterbymasklayer',
{
'INPUT':parameters['dem'],
'MASK':parameters['farm_shape'],
'SOURCE_CRS':None,
'TARGET_CRS':None,
'TARGET_EXTENT':None,
'NODATA':-9999,
'ALPHA_BAND':False,
'CROP_TO_CUTLINE':True,
'KEEP_RESOLUTION':False,
'SET_RESOLUTION':False,
'X_RESOLUTION':None,
'Y_RESOLUTION':None,
'MULTITHREADING':False,
'OPTIONS':'',
'DATA_TYPE':0,
'EXTRA':'',
'OUTPUT':parameters['clipped_dem']
}
)
feedback.pushInfo('DEM clipped correctly')
feedback.pushInfo('Starting filling')
filledgaps = processing.runAndLoadResults(
'gdal:fillnodata',
{
'INPUT':clipping['OUTPUT'],
'BAND':1,
'DISTANCE':10,
'ITERATIONS':0,
'NO_MASK':False,
'MASK_LAYER':None,
'OPTIONS':'',
'EXTRA':'',
'OUTPUT':parameters['filled_dem']
}
)
feedback.pushInfo('Starting slope calculation')
feedback.pushInfo('DEM filled correctly')
calculate = processing.runAndLoadResults(
'native:slope',
{
'INPUT':filledgaps['OUTPUT'],
'Z_FACTOR':1,
'OUTPUT':parameters['slope']
}
)
return {'clipped_dem': clipping['OUTPUT']}
return {'filled_dem': filledgaps['OUTPUT']}
return {'slope': calculate['OUTPUT']}