Repository navigation
Expand file tree
/
Copy pathzonal_stack.py
More file actions
217 lines (171 loc) · 8.52 KB
/
Copy pathzonal_stack.py
File metadata and controls
217 lines (171 loc) · 8.52 KB
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
# -*- coding: utf-8 -*-
"""
***************************************************************************
StackZonalStatistics.py
Date : January 2021
Copyright : (C) 2021 by Giacomo Fontanelli
Email : giacomofontanelli76 at 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.
***************************************************************************
This script run zonal statistics
for each layer of a multi band raster
***************************************************************************
"""
# --------------------------------------------------------------------------------------------------------------------
# 0 -----------------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------
__author__ = 'Giacomo Fontanelli'
__date__ = 'December 2020'
__copyright__ = '(C) 2020, Giacomo Fontanelli'
# --------------------------------------------------------------------------------------------------------------------
# 1 -----------------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterString,
QgsProcessingParameterEnum,
QgsProcessingOutputVectorLayer,
QgsProcessingException)
from qgis import processing
# --------------------------------------------------------------------------------------------------------------------
# 2 ----- Define the algorithm as a class inheriting from QgsProcessingAlgorithm -------
# --------------------------------------------------------------------------------------------------------------------
class ZonalStatisticsStack(QgsProcessingAlgorithm):
# 2A
STACK = 'STACK' # stack - INPUT_RASTER
POLYGONS = 'POLYGONS' # polygons - INPUT_VECTOR
PREFIX = 'PREFIX' # column_prefix - COLUMN_PREFIX
STAT = 'STAT' # stat - STATISTICS
# 2B
def tr(self, string):
return QCoreApplication.translate('Processing', string)
# 2C
def createInstance(self):
return ZonalStatisticsStack()
# 2D
def name(self):
return 'Zonal stat for stacks'
# 2E
def displayName(self):
return self.tr('Zonal stat for stacks')
# 2F
def group(self):
return self.tr('Sampling')
# 2G
def groupId(self):
return 'rasteranalysis'
# 2H
def shortHelpString(self):
return self.tr("This script perform statistics on multilayer stacks")
# --------------------------------------------------------------------------------------------------------------------
# 3 ---------- Define the parameters of the processing framework -----------------------------
# --------------------------------------------------------------------------------------------------------------------
def initAlgorithm(self, config=None):
# 3A Input vector polygon
self.addParameter(QgsProcessingParameterVectorLayer(
name = self.POLYGONS,
description = self.tr('Vector layer containing polygons'),
types = [QgsProcessing.TypeVectorPolygon],
defaultValue = None,
optional = False))
# 3B Input stack polygon
self.addParameter(QgsProcessingParameterRasterLayer(
name = self.STACK,
description = self.tr('Stack'),
defaultValue = None,
optional = False))
# 3C Input band prefix
self.addParameter(QgsProcessingParameterString(
name = self.PREFIX,
description = self.tr('Output column prefix'),
defaultValue = 'b_',
optional = False))
# 3D select statistics
self.addParameter(QgsProcessingParameterEnum(
name = self.STAT,
description = self.tr('Statistics to calculate'),
options = [self.tr("Number = 0"),
self.tr("Sum = 1"),
self.tr("Mean = 2"),
self.tr("Median = 3"),
self.tr("Dev std = 5"),
self.tr("Minimum = 6"),
self.tr("Maximum = 7"),
self.tr("Range = 8"),
self.tr("Minority = 9"),
self.tr("Majourity = 10"),
self.tr("Variety = 11"),
self.tr("Variance = 12")],
allowMultiple=True,
defaultValue=[2],
optional = False))
# --------------------------------------------------------------------------------------------------------------------
# 4 ----------------------------------------- Import layers ----------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------
def processAlgorithm(
self,
parameters,
context,
feedback):
# 4A Input polygons
polygons = self.parameterAsSource(
parameters,
self.POLYGONS,
context)
# 4B Input stack
stack = self.parameterAsRasterLayer(
parameters,
self.STACK,
context)
# 4C Input prefix for each column of sampling
columnPrefix = self.parameterAsString(
parameters,
self.PREFIX,
context)
# 4D Select statistical index
stat = self.parameterAsEnums(
parameters,
self.STAT,
context)
# -------------------------------------------------------------------------------------------------------------
# 5 ------------------------------------- Check -----------------------------------------------------------
# -------------------------------------------------------------------------------------------------------------
# 5A If source was not found, throw an exception
if polygons is None:
raise QgsProcessingException(self.invalidSourceError(parameters, POLYGONS))
# 5B Check for cancelation
if feedback.isCanceled():
return {}
# --------------------------------------------------------------------------------------------------------------------
# 6 ----------------------------------------- Execution ----------------------------------------------------
# --------------------------------------------------------------------------------------------------------------------
# 6A retrieve band number
nBands = stack.bandCount()
listBands = range(1, (nBands+1))
# 6B
for iBand in listBands:
processPar = {
"INPUT_RASTER": parameters[self.STACK],
"RASTER_BAND": iBand,
"INPUT_VECTOR": parameters[self.POLYGONS],
"COLUMN_PREFIX": 'b_',
"STATISTICS": parameters[self.STAT]}
# 6C
processOut = processing.run(
'qgis:zonalstatistics',
processPar,
is_child_algorithm = False,
context = context,
feedback = feedback)
# 6D Check for cancelation
if feedback.isCanceled():
return {}
return {self.POLYGONS: processOut["INPUT_VECTOR"]}