-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_raster_bands_algorithm.py
More file actions
375 lines (323 loc) · 13.8 KB
/
Copy pathsplit_raster_bands_algorithm.py
File metadata and controls
375 lines (323 loc) · 13.8 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# -*- coding: utf-8 -*-
"""
Split Raster Bands - Processing algorithm to separate a multi-band raster
into individual single-band rasters (monthly) or yearly multi-band rasters.
"""
import os
from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtGui import QColor, QIcon
from qgis.core import (
Qgis,
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingException,
QgsProcessingOutputMultipleLayers,
QgsProcessingParameterBoolean,
QgsProcessingParameterEnum,
QgsProcessingParameterFolderDestination,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterString,
QgsProject,
QgsRasterBandStats,
QgsRasterLayer,
QgsRasterShader,
QgsColorRampShader,
QgsSingleBandPseudoColorRenderer,
)
import processing
try:
PROCESSING_NUMBER_INTEGER = QgsProcessingParameterNumber.Type.Integer
except AttributeError:
PROCESSING_NUMBER_INTEGER = getattr(QgsProcessingParameterNumber, "Integer")
try:
RASTER_STAT_MIN = QgsRasterBandStats.Stats.Min
RASTER_STAT_MAX = QgsRasterBandStats.Stats.Max
except AttributeError:
RASTER_STAT_MIN = getattr(QgsRasterBandStats, "Min")
RASTER_STAT_MAX = getattr(QgsRasterBandStats, "Max")
try:
SHADER_INTERPOLATED = QgsColorRampShader.Type.Interpolated
except AttributeError:
SHADER_INTERPOLATED = getattr(QgsColorRampShader, "Interpolated")
try:
SHADER_CONTINUOUS = QgsColorRampShader.ClassificationMode.Continuous
except AttributeError:
SHADER_CONTINUOUS = getattr(QgsColorRampShader, "Continuous")
class SplitRasterBands(QgsProcessingAlgorithm):
"""
Split a multi-band raster into separate rasters by month or by year.
"""
INPUT_RASTER = "INPUT_RASTER"
OUTPUT_FOLDER = "OUTPUT_FOLDER"
PREFIX = "PREFIX"
START_YEAR = "START_YEAR"
SPLIT_MODE = "SPLIT_MODE"
ADD_TO_MAP = "ADD_TO_MAP"
OUTPUT_LAYERS = "OUTPUT_LAYERS"
SPLIT_MODE_OPTIONS = [
"Monthly (one band per file)",
"Yearly (12 bands per file)",
]
MONTH_NAMES = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
]
def tr(self, string):
return QCoreApplication.translate("SplitRasterBands", string)
def createInstance(self):
return SplitRasterBands()
def name(self):
return "splitrasterbands"
def displayName(self):
return self.tr("Split Raster Bands")
def icon(self):
return QIcon(os.path.join(os.path.dirname(__file__), "icon.svg"))
def group(self):
return ''
def groupId(self):
return ''
def shortHelpString(self):
return self.tr(
"Splits a multi-band raster into separate files with automatic month/year labelling.\n\n"
"Two split modes:\n\n"
"MONTHLY - each band becomes its own single-band GeoTIFF:\n"
" 24 bands + start 2023 = 24 files (Jan_2023 ... Dec_2024)\n\n"
"YEARLY - every 12 bands are grouped into one file per year:\n"
" 24 bands + start 2023 = 2 files (2023.tif with 12 bands, 2024.tif with 12 bands)\n\n"
"Works with any multiple of 12 bands. If the band count is not a multiple "
"of 12, only Monthly mode with numeric labels is used."
)
def _apply_singleband_pseudocolor(self, raster_layer, feedback=None):
"""Apply a Singleband Pseudocolor renderer using band 1."""
try:
provider = raster_layer.dataProvider()
band_count = raster_layer.bandCount()
if band_count > 1:
global_min = float("inf")
global_max = float("-inf")
for band_num in range(1, band_count + 1):
stats = provider.bandStatistics(
band_num,
RASTER_STAT_MIN | RASTER_STAT_MAX,
raster_layer.extent(),
0,
)
if stats.minimumValue != -9999.0 and stats.minimumValue < global_min:
global_min = stats.minimumValue
if stats.maximumValue != -9999.0 and stats.maximumValue > global_max:
global_max = stats.maximumValue
min_val = global_min
max_val = global_max
else:
stats = provider.bandStatistics(
1,
RASTER_STAT_MIN | RASTER_STAT_MAX,
raster_layer.extent(),
0,
)
min_val = stats.minimumValue
max_val = stats.maximumValue
if min_val == float("inf") or max_val == float("-inf"):
return False
if min_val == max_val:
max_val = min_val + 1
shader = QgsRasterShader()
color_ramp = QgsColorRampShader()
color_ramp.setColorRampType(SHADER_INTERPOLATED)
color_ramp.setMinimumValue(min_val)
color_ramp.setMaximumValue(max_val)
color_ramp.setClassificationMode(SHADER_CONTINUOUS)
color_ramp.setColorRampItemList([
QgsColorRampShader.ColorRampItem(min_val, QColor(68, 1, 84), f"{min_val:.1f}"),
QgsColorRampShader.ColorRampItem(min_val + (max_val - min_val) * 0.25, QColor(59, 82, 139), ""),
QgsColorRampShader.ColorRampItem(min_val + (max_val - min_val) * 0.5, QColor(33, 145, 140), ""),
QgsColorRampShader.ColorRampItem(min_val + (max_val - min_val) * 0.75, QColor(94, 201, 98), ""),
QgsColorRampShader.ColorRampItem(max_val, QColor(253, 231, 37), f"{max_val:.1f}"),
])
shader.setRasterShaderFunction(color_ramp)
renderer = QgsSingleBandPseudoColorRenderer(provider, 1, shader)
renderer.setClassificationMin(min_val)
renderer.setClassificationMax(max_val)
raster_layer.setRenderer(renderer)
raster_layer.triggerRepaint()
if feedback:
feedback.pushInfo(
f" Applied Singleband Pseudocolor styling (range: {min_val:.2f} to {max_val:.2f})"
)
return True
except Exception as exc:
if feedback:
feedback.reportError(f" Warning: Could not apply pseudocolor style: {exc}")
return False
def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterRasterLayer(
self.INPUT_RASTER,
self.tr("Input multi-band raster"),
)
)
self.addParameter(
QgsProcessingParameterString(
self.PREFIX,
self.tr("Output filename prefix (e.g. tmax, ppt)"),
defaultValue="band",
optional=False,
)
)
self.addParameter(
QgsProcessingParameterNumber(
self.START_YEAR,
self.tr("Start year (first band = January of this year)"),
type=PROCESSING_NUMBER_INTEGER,
defaultValue=2024,
minValue=1958,
maxValue=2100,
)
)
self.addParameter(
QgsProcessingParameterEnum(
self.SPLIT_MODE,
self.tr("Split mode"),
options=self.SPLIT_MODE_OPTIONS,
defaultValue=0,
)
)
self.addParameter(
QgsProcessingParameterBoolean(
self.ADD_TO_MAP,
self.tr("Add output layers to map"),
defaultValue=True,
)
)
self.addParameter(
QgsProcessingParameterFolderDestination(
self.OUTPUT_FOLDER,
self.tr("Output folder"),
)
)
self.addOutput(
QgsProcessingOutputMultipleLayers(
self.OUTPUT_LAYERS,
self.tr("Output layers"),
)
)
def processAlgorithm(self, parameters, context, feedback):
raster_layer = self.parameterAsRasterLayer(parameters, self.INPUT_RASTER, context)
output_folder = self.parameterAsString(parameters, self.OUTPUT_FOLDER, context)
prefix = self.parameterAsString(parameters, self.PREFIX, context)
start_year = self.parameterAsInt(parameters, self.START_YEAR, context)
split_mode = self.parameterAsEnum(parameters, self.SPLIT_MODE, context)
add_to_map = self.parameterAsBool(parameters, self.ADD_TO_MAP, context)
if raster_layer is None:
raise QgsProcessingException(self.tr("Invalid input raster."))
band_count = raster_layer.bandCount()
if band_count < 2:
raise QgsProcessingException(self.tr("Input raster has only 1 band - nothing to split."))
is_multiple_of_12 = band_count % 12 == 0
if split_mode == 1 and not is_multiple_of_12:
raise QgsProcessingException(
self.tr(
f"Yearly mode requires a band count that is a multiple of 12. "
f"This raster has {band_count} bands. Use Monthly mode instead."
)
)
os.makedirs(output_folder, exist_ok=True)
source_path = raster_layer.source()
num_years = band_count // 12 if is_multiple_of_12 else 0
end_year = start_year + num_years - 1 if num_years else start_year
mode_label = self.SPLIT_MODE_OPTIONS[split_mode]
feedback.pushInfo("=" * 60)
feedback.pushInfo("Split Raster Bands")
feedback.pushInfo("=" * 60)
feedback.pushInfo(f"Input: {source_path}")
feedback.pushInfo(f"Total bands: {band_count}")
feedback.pushInfo(f"Split mode: {mode_label}")
if is_multiple_of_12:
feedback.pushInfo(f"Year span: {start_year} - {end_year} ({num_years} year(s))")
else:
feedback.pushInfo("Band count is not a multiple of 12 - using numeric labels.")
feedback.pushInfo(f"Prefix: {prefix}")
feedback.pushInfo(f"Output folder: {output_folder}")
feedback.pushInfo("")
output_paths = []
if split_mode == 0:
for band_num in range(1, band_count + 1):
if feedback.isCanceled():
break
if is_multiple_of_12:
month_index = (band_num - 1) % 12
year = start_year + (band_num - 1) // 12
month_name = self.MONTH_NAMES[month_index]
label = f"{band_num:02d}_{month_name}_{year}"
else:
label = f"{band_num:02d}"
out_filename = f"{prefix}_{label}.tif"
out_path = os.path.join(output_folder, out_filename)
feedback.pushInfo(f" [{band_num}/{band_count}] -> {out_filename}")
result = processing.run(
"gdal:translate",
{
"INPUT": source_path,
"TARGET_CRS": None,
"NODATA": None,
"COPY_SUBDATASETS": False,
"OPTIONS": "COMPRESS=LZW",
"EXTRA": f"-b {band_num}",
"DATA_TYPE": 0,
"OUTPUT": out_path,
},
context=context,
feedback=feedback,
is_child_algorithm=True,
)
output_paths.append(result["OUTPUT"])
if add_to_map:
layer_name = f"{prefix}_{label}"
new_layer = QgsRasterLayer(result["OUTPUT"], layer_name)
if new_layer.isValid():
QgsProject.instance().addMapLayer(new_layer)
feedback.setProgress(int((band_num / band_count) * 100))
elif split_mode == 1:
for year_idx in range(num_years):
if feedback.isCanceled():
break
year = start_year + year_idx
first_band = year_idx * 12 + 1
last_band = first_band + 11
out_filename = f"{prefix}_{year}.tif"
out_path = os.path.join(output_folder, out_filename)
feedback.pushInfo(
f" [{year_idx + 1}/{num_years}] -> {out_filename} "
f"(bands {first_band}-{last_band})"
)
band_flags = " ".join(f"-b {band}" for band in range(first_band, last_band + 1))
result = processing.run(
"gdal:translate",
{
"INPUT": source_path,
"TARGET_CRS": None,
"NODATA": None,
"COPY_SUBDATASETS": False,
"OPTIONS": "COMPRESS=LZW",
"EXTRA": band_flags,
"DATA_TYPE": 0,
"OUTPUT": out_path,
},
context=context,
feedback=feedback,
is_child_algorithm=True,
)
output_paths.append(result["OUTPUT"])
if add_to_map:
layer_name = f"{prefix}_{year}"
new_layer = QgsRasterLayer(result["OUTPUT"], layer_name)
if new_layer.isValid():
QgsProject.instance().addMapLayer(new_layer)
self._apply_singleband_pseudocolor(new_layer, feedback=feedback)
feedback.setProgress(int(((year_idx + 1) / num_years) * 100))
feedback.pushInfo("")
feedback.pushInfo("=" * 60)
feedback.pushInfo(f"COMPLETE - {len(output_paths)} file(s) exported.")
feedback.pushInfo("=" * 60)
return {self.OUTPUT_LAYERS: output_paths}