-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwater_from_complement.py
566 lines (400 loc) · 20.8 KB
/
water_from_complement.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# -*- coding: utf-8 -*-
"""
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__version__ = '2024.05.12'
import pprint, os, datetime, tempfile
#from osgeo import gdal
from qgis import processing
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
QgsFeatureSink,
QgsProcessingException, #still works
QgsProcessingAlgorithm,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterRasterDestination,
QgsProcessingParameterNumber,
QgsProcessingParameterString,
QgsRasterLayer,
QgsCoordinateTransformContext,
QgsMapLayerStore,
QgsProcessingOutputLayerDefinition,
QgsProject,
QgsProcessingFeatureSourceDefinition,
QgsFeatureRequest,
QgsVectorLayer,
)
from qgis.analysis import QgsNativeAlgorithms, QgsRasterCalculatorEntry, QgsRasterCalculator
import pandas as pd
import numpy as np
from osgeo import gdal
class WaterFromComp(QgsProcessingAlgorithm):
"""build a WSH or WSE grid from teh DEM and the complement
"""
# Constants used to refer to parameters and outputs. They will be
# used when calling the algorithm from another algorithm, or when
# calling from the QGIS console.
INPUT_DEM = 'INPUT_DEM'
INPUT_WATER = 'INPUT_WATER'
INPUT_TYPE = 'INPUT_TYPE'
#outputs
OUTPUT_WATER = 'OUTPUT_WATER'
def tr(self, string):
"""
Returns a translatable string with the self.tr() function.
"""
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return WaterFromComp()
def name(self):
"""
Returns the algorithm name, used for identifying the algorithm. This
string should be fixed for the algorithm, and must not be localised.
The name should be unique within each provider. Names should contain
lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return 'WaterFromComp'
def displayName(self):
"""
Returns the translated algorithm name, which should be used for any
user-visible display of the algorithm name.
"""
return self.tr('Water Grid from Complement')
def group(self):
"""
Returns the name of the group this algorithm belongs to. This string
should be localised.
"""
return self.tr('FloodRescaling')
def groupId(self):
"""
Returns the unique ID of the group this algorithm belongs to. This
string should be fixed for the algorithm, and must not be localised.
The group id should be unique within each provider. Group id should
contain lowercase alphanumeric characters only and no spaces or other
formatting characters.
"""
return 'floodrescaler'
def shortHelpString(self):
return self.tr(
"""
<p>Two tools to generate a Water Surface Elevation (WSE) or Water Surface Height (WSH) grid from its complement and a DEM.
Useful for pre-processing other tools.</p>
<ul>
<li><strong>WSH to WSE</strong>: Add the WSH to the DEM and mask zeros</li>
<li><strong>WSE to WSH</strong>: Subtract the DEM from the WSE and set masked values to zero </li>
</ul>
<h3>Tips and Tricks and Notes</h3>
Grids need to have identical Geospatial attributes (e.g., extents and shape match)
Inputs (and outputs) adopt dry=null for WSE and dry=0 for WSH grids
DEM and Input grids are assumed to be hydraulically consistent (i.e., the same DEM was used to generate the Input)
<h3>Issues and Updates</h3>
See the <a href="https://github.com/cefect/FloodRescaler">project repository</a> for updates, to post an issue/question, or to request new features.
<h3>Attribution</h3>
If you use these tools for your work, please cite <a href="https://doi.org/10.1029/2023WR035100">Bryant et. al., (2023)</a>
"""
)
def initAlgorithm(self, config=None):
"""
Here we define the inputs and output of the algorithm, along
with some other properties.
"""
#=======================================================================
# control
#=======================================================================
#=======================================================================
# INPUTS-----------
#=======================================================================
#=======================================================================
# raster layers
#=======================================================================
self.addParameter(
QgsProcessingParameterRasterLayer(self.INPUT_DEM, self.tr('DEM'))
)
self.addParameter(
QgsProcessingParameterRasterLayer(self.INPUT_WATER, self.tr('Input Water Grid'))
)
#=======================================================================
# pars
#=======================================================================
# add methods parameter with some hints
param = QgsProcessingParameterString(self.INPUT_TYPE,
self.tr('Input Water Grid Type'),
defaultValue='WSH', multiLine=False)
param.setMetadata({'widget_wrapper':{ 'value_hints': ['WSH','WSE'] }})
self.addParameter(param)
#=======================================================================
# OUTPUTS
#=======================================================================
obj = QgsProcessingParameterRasterDestination
self.addParameter(
obj(self.OUTPUT_WATER, self.tr('Output Water Grid'))
)
def processAlgorithm(self, params, context, feedback):
"""
Here is where the processing itself takes place.
"""
#=======================================================================
# defaults
#=======================================================================
feedback.pushInfo('starting w/ \n%s'%(pprint.pformat(params, width=30)))
self._init_algo(params, context, feedback)
#=======================================================================
# retrieve inputs---
#=======================================================================
#=======================================================================
# input rasters
#=======================================================================
def get_rlay(attn):
"""load a raster layer and do some checks"""
inrlay = self.parameterAsRasterLayer(params, getattr(self, attn), context)
if not isinstance(inrlay, QgsRasterLayer):
raise QgsProcessingException(f'bad type on {attn}')
return inrlay
input_dem = get_rlay('INPUT_DEM')
input_water = get_rlay('INPUT_WATER')
feedback.pushInfo('finished loading raster layers')
#=======================================================================
# params
#=======================================================================
input_type = self.parameterAsString(params, self.INPUT_TYPE, context)
kwargs = dict()
feedback.pushInfo(f'running with \'{input_type}\'\n{kwargs}')
if feedback.isCanceled():
return {}
#=======================================================================
# exceute specified method----
#=======================================================================.
return self.run_water_grid_convert(input_dem, input_water, input_type,
#context=context, feedback=feedback,
**params, **kwargs)
def run_water_grid_convert(self, dem_rlay, water_rlay, water_type,
**params):
"""convert from WSE or WSH using the DEM"""
#=======================================================================
# defaults
#=======================================================================
start = now()
OUTPUT=self._get_out(self.OUTPUT_WATER)
assert not OUTPUT==''
#=======================================================================
# #check consistency
#=======================================================================
assert_spatial_equal(dem_rlay, water_rlay, msg='input grid extent mismatch')
#=======================================================================
# run based on types
#=======================================================================
skwargs = dict(OUTPUT=OUTPUT)
if water_type == 'WSE':
func = self.wse_to_wsh
elif water_type=='WSH':
func = self.wsh_to_wse
else:
raise KeyError(water_type)
result = func(water_rlay, dem_rlay, **skwargs)
return {self.OUTPUT_WATER:result}
def wse_to_wsh(self, wse_rlay, dem_rlay,
OUTPUT='TEMPORARY_OUTPUT'):
"""convert WSE to WSH
ds['WSHf'] = (ds['WSEf'] - ds['DEM']).fillna(0)
"""
tfp =lambda x:self._tfp(prefix=x, _dir=self.temp_dir)
f=self.feedback
#cf_kwargs = dict(context=self.context, feedback=f)
f.pushInfo('\ncomputing WSH from WSE\n=========================\n\n')
#=======================================================================
# check WSE expectations
#=======================================================================
assert getNoDataCount(wse_rlay.source())>0, f'WSE is expected to contain some nulls (dry cells)'
#=======================================================================
# #get raw delta
#=======================================================================
#still has nulls from WSE mask
delta_fp = self._gdal_calc({
'INPUT_A' : wse_rlay, 'BAND_A' : 1, 'INPUT_B' : dem_rlay, 'BAND_B' : 1,
'FORMULA':'A-B',
'NO_DATA' : -9999, 'OUTPUT' : tfp('delta_'), 'RTYPE' : 5,
})
f.pushInfo(f'got delta\n {delta_fp}')
#=======================================================================
# handle mask
#=======================================================================
wsh_fp = processing.run('native:fillnodata',
{ 'BAND' : 1, 'FILL_VALUE' : 0,
'INPUT' : delta_fp, 'OUTPUT' : OUTPUT },
**self.proc_kwargs)['OUTPUT']
f.pushInfo(f'nulls filled')
#=======================================================================
# wrap
#=======================================================================
stats_d = self._rasterlayerstatistics(wsh_fp)
f.pushInfo(f'WSH grid computed w/\n%s'%{'%.2f'%stats_d[k] for k in ['MAX', 'MEAN', 'MIN', 'SUM']})
if not stats_d['MIN']==0:
f.pushWarning('returned a WSH with min!=0.\nYour DEM and WSE may be inconsistent')
if stats_d['MAX']>99:
f.pushWarning('WSH result has an unexpectedly high max')
return OUTPUT
def wsh_to_wse(self, wsh_rlay, dem_rlay,
OUTPUT='TEMPORARY_OUTPUT'):
"""convert WSH to WSE
wse_mar = ma.array(wsh_mar.data+dem_mar.data,mask = np.logical_or(wsh_mar.mask, dem_mar.mask),
fill_value=np.nan)
"""
tfp =lambda x:self._tfp(prefix=x, _dir=self.temp_dir)
f=self.feedback
#cf_kwargs = dict(context=self.context, feedback=f)
f.pushInfo('\ncomputing WSE from WSH\n=========================\n\n')
#=======================================================================
# check WSH meets expectations
#=======================================================================
stats_d = self._rasterlayerstatistics(wsh_rlay)
assert stats_d['MIN']==0, f'WSH layer must have a minimum of zero (negative depths not supported)'
#=======================================================================
# add
#=======================================================================
delta_fp = self._gdal_calc({
'INPUT_A' : wsh_rlay, 'BAND_A' : 1, 'INPUT_B' : dem_rlay, 'BAND_B' : 1,
'FORMULA':'A+B',
'NO_DATA' : -9999, 'OUTPUT' : tfp('delta_'), 'RTYPE' : 5,
})
f.pushInfo(f'\n\ncomputed delta\n {delta_fp}')
#=======================================================================
# mask
#=======================================================================
mask_fp = self._gdal_calc_mask(wsh_rlay)
wse_fp = self._gdal_calc({
'INPUT_A' : delta_fp, 'BAND_A' : 1, 'INPUT_B' : mask_fp, 'BAND_B' : 1,
'FORMULA':'A*B',
'NO_DATA' : -9999, 'OUTPUT' : OUTPUT, 'RTYPE' : 5,
})
f.pushInfo(f'\n\napplied mask and got \n {wse_fp}')
#=======================================================================
# check
#=======================================================================
stats_d = self._rasterlayerstatistics(wse_fp)
stats_d['null_cnt'] = getNoDataCount(wse_fp)
f.pushInfo(f'WSE grid computed w/\n%s'%{'%.2f'%stats_d[k] for k in ['MAX', 'MEAN', 'MIN', 'SUM']})
if not stats_d['null_cnt']>0:
f.pushWarning(f'WSE result has no nulls (dry cells)')
if stats_d['MIN']==0:
f.pushWarning('WSE result has a min of zero')
return wse_fp
def _init_algo(self, params, context, feedback,
temp_dir=None,
):
"""common init for tests"""
self.proc_kwargs = dict(feedback=feedback, context=context, is_child_algorithm=True)
self.context, self.feedback, self.params = context, feedback, params
if temp_dir is None:
temp_dir = os.path.join(tempfile.TemporaryDirectory().name, 'dscale')
if not os.path.exists(temp_dir):os.makedirs(temp_dir)
self.temp_dir=temp_dir
self.logger = log(feedback=feedback)
def _get_out(self, attn):
return self.parameterAsOutputLayer(self.params, getattr(self, attn), self.context)
def _gdal_calc(self, pars_d):
"""Raster calculator (gdal:rastercalculator)
- 0: Byte
- 1: Int16
- 2: UInt16
- 3: UInt32
- 4: Int32
- 5: Float32
- 6: Float64
"""
ofp = processing.run('gdal:rastercalculator', pars_d, **self.proc_kwargs)['OUTPUT']
if not os.path.exists(ofp):
raise QgsProcessingException('gdal:rastercalculator failed to get a result for \n%s'%pars_d['FORMULA'])
return ofp
def _gdal_calc_mask(self, rlay, OUTPUT='TEMPORARY_OUTPUT'):
"""1=data, null=nodata"""
return self._gdal_calc({'FORMULA':'A/A', 'INPUT_A':rlay, 'BAND_A':1,'NO_DATA':-9999,'OUTPUT':OUTPUT, 'RTYPE':5})
def _rasterlayerstatistics(self, rlay):
return processing.run('native:rasterlayerstatistics', {'INPUT':rlay}, **self.proc_kwargs)
def _tfp(self, prefix=None, suffix='.tif', _dir=None):
if _dir is None: _dir=self.temp_dir
return tempfile.NamedTemporaryFile(suffix=suffix,prefix=prefix, dir=_dir).name
#===============================================================================
# HELPERS----------
#===============================================================================
"""cant do imports without creating a provider and a plugin"""
def now():
return datetime.datetime.now()
def rlay_get_resolution(rlay):
return (rlay.rasterUnitsPerPixelY() + rlay.rasterUnitsPerPixelX())*0.5
class log(object):
"""log emulator"""
def __init__(self, feedback):
self.feedback=feedback
def debug(self, msg):
self.feedback.pushDebugInfo(msg)
def info(self, msg):
self.feedback.pushInfo(msg)
def assert_spatial_equal(left, right, msg='',):
""" extents check"""
assert isinstance(left, QgsRasterLayer), f'got bad type :{type(left).__name__}'+ '\n'+msg
assert isinstance(right, QgsRasterLayer), type(right).__name__+ '\n'+msg
__tracebackhide__ = True
#===========================================================================
# crs
#===========================================================================
if not left.crs()==right.crs():
raise QgsProcessingException('crs mismatch')
#===========================================================================
# extents
#===========================================================================
if not left.extent()==right.extent():
raise QgsProcessingException('%s != %s extent\n %s != %s\n '%(
left.name(), right.name(), left.extent(), right.extent()) +msg)
#===========================================================================
# shape
#===========================================================================
#left.height()
lshape = rlay_get_shape(left)
rshape=rlay_get_shape(right)
if not lshape==rshape:
raise QgsProcessingException('%s != %s shape\n %s != %s\n '%(
left.name(), right.name(), lshape,rshape) +msg)
def assert_extent_equal(left, right, msg='',):
""" extents check"""
assert isinstance(left, QgsRasterLayer), f'got bad type :{type(left).__name__}'+ '\n'+msg
assert isinstance(right, QgsRasterLayer), type(right).__name__+ '\n'+msg
__tracebackhide__ = True
#===========================================================================
# crs
#===========================================================================
if not left.crs()==right.crs():
raise QgsProcessingException('crs mismatch')
#===========================================================================
# extents
#===========================================================================
if not left.extent()==right.extent():
raise QgsProcessingException('%s != %s extent\n %s != %s\n '%(
left.name(), right.name(), left.extent(), right.extent()) +msg)
def rlay_get_shape(r):
return (r.height(), r.width())
def getNoDataCount(fp, **kwargs):
"""2022-05-10: this was returning some nulls
for rasters where I could not find any nulls"""
#get raw data
ar = rlay_to_array(fp, **kwargs)
return np.isnan(ar).astype(int).sum()
def rlay_to_array(rlay_fp, dtype=np.dtype('float32')):
"""context managger?"""
#get raw data
ds = gdal.Open(rlay_fp)
band = ds.GetRasterBand(1)
ar_raw = np.array(band.ReadAsArray(), dtype=dtype)
#remove nodata values
ndval = band.GetNoDataValue()
del ds
del band
return np.where(ar_raw==ndval, np.nan, ar_raw)