-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcloud_masking.py
1335 lines (1145 loc) · 68.6 KB
/
cloud_masking.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
/***************************************************************************
CloudMasking
A QGIS plugin
Cloud masking for landsat products using different process suck as fmask
-------------------
copyright : (C) 2016-2022 by Xavier Corredor Llano, SMByC
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 3 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import os.path
import platform
import shutil
import tempfile
from datetime import datetime
from subprocess import call
from osgeo import gdal
from qgis.PyQt.QtCore import QSettings, QTranslator, qVersion, QCoreApplication, Qt, pyqtSlot, QLocale
from qgis.PyQt.QtWidgets import QAction, QMessageBox, QApplication, QFileDialog, QListWidgetItem, QSizePolicy
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QCheckBox, QGroupBox, QRadioButton
from qgis.core import QgsProject, QgsRasterLayer, QgsMapLayer, QgsCoordinateTransform, \
QgsMapLayerProxyModel, QgsVectorFileWriter, Qgis
from qgis.utils import iface
# Initialize Qt resources from file resources.py
from . import resources
from CloudMasking.core import cloud_filters, color_stack
from CloudMasking.core.utils import apply_symbology, get_prefer_name, update_process_bar, get_extent, \
load_and_select_filepath_in, get_file_path_of_layer, get_nodata_value_from_file, wait_process, error_handler
from CloudMasking.libs import gdal_calc, gdal_merge
from CloudMasking.gui.cloud_masking_dockwidget import CloudMaskingDockWidget
from CloudMasking.gui.about_dialog import AboutDialog
class CloudMasking:
"""QGIS Plugin Implementation."""
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale', QLocale().name())[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'CloudMasking_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
self.menu_name_plugin = self.tr("&Cloud masking for Landsat products")
self.pluginIsActive = False
self.dockwidget = None
# Obtaining the map canvas
self.canvas = iface.mapCanvas()
self.about_dialog = AboutDialog()
# noinspection PyMethodMayBeStatic
def tr(self, message):
"""Get the translation for a string using Qt translation API.
We implement this ourselves since we do not inherit QObject.
:param message: String for translation.
:type message: str, QString
:returns: Translated version of message.
:rtype: QString
"""
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('CloudMasking', message)
def initGui(self):
### Main dockwidget
# Create action that will start plugin configuration
icon_path = ':/plugins/CloudMasking/icons/cloud_masking.svg'
self.dockable_action = QAction(QIcon(icon_path), self.tr('&Cloud Masking'), self.iface.mainWindow())
# connect the action to the run method
self.dockable_action.triggered.connect(self.run)
# Add toolbar button and menu item
self.iface.addToolBarIcon(self.dockable_action)
self.iface.addPluginToMenu(self.menu_name_plugin, self.dockable_action)
### About dialog
# Create action that will start plugin configuration
icon_path = ':/plugins/CloudMasking/icons/about.svg'
self.about_action = QAction(QIcon(icon_path), self.tr('About'), self.iface.mainWindow())
# connect the action to the run method
self.about_action.triggered.connect(self.about)
# Add toolbar button and menu item
self.iface.addPluginToMenu(self.menu_name_plugin, self.about_action)
# --------------------------------------------------------------------------
def onClosePlugin(self):
"""Cleanup necessary items here when plugin dockwidget is closed"""
self.removes_temporary_files()
# disconnects
self.dockwidget.closingPlugin.disconnect(self.onClosePlugin)
QgsProject.instance().layersAdded.disconnect(self.updateLayersList_SingleLayerMask)
QgsProject.instance().layersRemoved.disconnect(self.updateLayersList_SingleLayerMask)
QgsProject.instance().layersAdded.disconnect(self.updateLayersList_MultipleLayerMask)
QgsProject.instance().layersRemoved.disconnect(self.updateLayersList_MultipleLayerMask)
# remove this statement if dockwidget is to remain
# for reuse if plugin is reopened
# Commented next statement since it causes QGIS crashe
# when closing the docked window:
self.dockwidget.deleteLater()
self.dockwidget = None
self.pluginIsActive = False
from qgis.utils import reloadPlugin
reloadPlugin("CloudMasking")
def about(self):
self.about_dialog.show()
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
self.removes_temporary_files()
# Remove the plugin menu item and icon
self.iface.removePluginMenu(self.menu_name_plugin, self.dockable_action)
self.iface.removePluginMenu(self.menu_name_plugin, self.about_action)
self.iface.removeToolBarIcon(self.dockable_action)
# --------------------------------------------------------------------------
def run(self):
"""Run method that loads and starts the plugin"""
if not self.pluginIsActive:
self.pluginIsActive = True
# print "** STARTING CloudMasking"
# dockwidget may not exist if:
# first run of plugin
# removed on close (see self.onClosePlugin method)
if self.dockwidget == None:
# Create the dockwidget (after translation) and keep reference
self.dockwidget = CloudMaskingDockWidget()
# connect to provide cleanup on closing of dockwidget
self.dockwidget.closingPlugin.connect(self.onClosePlugin)
# show the dockwidget
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dockwidget)
self.dockwidget.tabWidget.setCurrentWidget(self.dockwidget.tab_OL) # focus first tab
self.dockwidget.show()
# initial masking and color stack instance
self.masking_result = None
self.color_stack_scene = None
# set properties to QgsMapLayerComboBox for mask list
self.dockwidget.select_SingleLayerMask.setCurrentIndex(-1)
self.dockwidget.select_SingleLayerMask.setFilters(QgsMapLayerProxyModel.RasterLayer)
self.dockwidget.OnlyMaskLayers_SingleMask.clicked.connect(self.updateLayersList_SingleLayerMask)
QgsProject.instance().layersAdded.connect(self.updateLayersList_SingleLayerMask)
QgsProject.instance().layersRemoved.connect(self.updateLayersList_SingleLayerMask)
self.updateLayersList_SingleLayerMask()
# tabwidget
self.update_tab_select_mask(0)
self.dockwidget.select_layer_mask.currentChanged.connect(self.update_tab_select_mask)
# set properties to list for select multiple layer mask
self.updateLayersList_MultipleLayerMask()
self.dockwidget.OnlyMaskLayers_MultipleMask.clicked.connect(self.updateLayersList_MultipleLayerMask)
QgsProject.instance().layersAdded.connect(self.updateLayersList_MultipleLayerMask)
QgsProject.instance().layersRemoved.connect(self.updateLayersList_MultipleLayerMask)
self.dockwidget.QPBtn_SelectAll.clicked.connect(self.selectAll_MultipleLayerMask)
self.dockwidget.QPBtn_DeselectAll.clicked.connect(self.deselectAll_MultipleLayerMask)
# set properties to QgsMapLayerComboBox for shape area
self.dockwidget.QCBox_MaskInShapeArea.setCurrentIndex(-1)
self.dockwidget.QCBox_MaskInShapeArea.setFilters(QgsMapLayerProxyModel.VectorLayer)
# call to browse the shape area for make mask inside it
self.dockwidget.button_BrowseShapeArea.clicked.connect(lambda: self.browser_dialog_to_load_file(
self.dockwidget.QCBox_MaskInShapeArea,
dialog_title=self.tr("Select the vector file"),
file_filters=self.tr("Vector files (*.gpkg *.shp);;All files (*.*)"),
suggested_path=os.path.dirname(self.dockwidget.mtl_path)))
# call to load MTL file
self.dockwidget.button_LoadMTL.clicked.connect(lambda: self.button_load_mtl())
# call to clear all
self.dockwidget.button_ClearAll.clicked.connect(lambda: self.button_clear_all())
# call to load natural color stack
self.dockwidget.button_NaturalColorStack.clicked.connect(lambda: self.set_color_stack("natural_color"))
# call to load false color stack
self.dockwidget.button_FalseColorStack.clicked.connect(lambda: self.set_color_stack("false_color"))
# call to load infrareds stack
self.dockwidget.button_InfraredsStack.clicked.connect(lambda: self.set_color_stack("infrareds"))
# call to process load stack
self.dockwidget.button_processLoadStack.clicked.connect(lambda: self.load_stack())
# call to process mask
self.dockwidget.button_processMask.clicked.connect(lambda: self.process_mask())
# save simple mask
self.dockwidget.button_SimpleSaveMask.clicked.connect(lambda: self.fileDialog_exportSimpleMask())
# save multi mask
self.dockwidget.button_MultiSaveMask.clicked.connect(lambda: self.fileDialog_exportMultiMask())
# select result
self.dockwidget.button_SelectResult.clicked.connect(self.fileDialog_SaveResult)
# select result
self.dockwidget.button_SelectPFile.clicked.connect(self.fileDialog_SelectPFile)
# button for Apply Mask
self.dockwidget.button_processApplyMask.clicked.connect(lambda: self.apply_mask())
def update_tab_select_mask(self, current_tab_idx):
"""Adjust the size tab based on the content"""
for tab_idx in range(self.dockwidget.select_layer_mask.count()):
if tab_idx != current_tab_idx:
self.dockwidget.select_layer_mask.widget(tab_idx).setSizePolicy(QSizePolicy.Ignored,
QSizePolicy.Ignored)
self.dockwidget.select_layer_mask.widget(current_tab_idx).setSizePolicy(QSizePolicy.Preferred,
QSizePolicy.Preferred)
self.dockwidget.select_layer_mask.widget(current_tab_idx).resize(
self.dockwidget.select_layer_mask.widget(current_tab_idx).minimumSizeHint())
self.dockwidget.select_layer_mask.widget(current_tab_idx).adjustSize()
def updateLayersList_SingleLayerMask(self):
# filtering
excepted = []
if self.dockwidget.OnlyMaskLayers_SingleMask.isChecked():
for layer in [l for l in QgsProject.instance().mapLayers().values() if l.type() == QgsMapLayer.RasterLayer]:
if not layer.name().startswith("Cloud Mask"):
excepted.append(layer)
# set excepted layers
self.dockwidget.select_SingleLayerMask.setExceptedLayerList(excepted)
def updateLayersList_MultipleLayerMask(self):
# delete items
self.dockwidget.select_MultipleLayerMask.clear()
# filtering
for layer in [l for l in QgsProject.instance().mapLayers().values() if l.type() == QgsMapLayer.RasterLayer]:
if self.dockwidget.OnlyMaskLayers_MultipleMask.isChecked() and not layer.name().startswith("Cloud Mask"):
continue
item = QListWidgetItem(layer.name())
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setCheckState(Qt.Unchecked)
self.dockwidget.select_MultipleLayerMask.addItem(item)
def selectAll_MultipleLayerMask(self):
for x in range(self.dockwidget.select_MultipleLayerMask.count()):
self.dockwidget.select_MultipleLayerMask.item(x).setCheckState(Qt.Checked)
def deselectAll_MultipleLayerMask(self):
for x in range(self.dockwidget.select_MultipleLayerMask.count()):
self.dockwidget.select_MultipleLayerMask.item(x).setCheckState(Qt.Unchecked)
def getLayerByName(self, layer_name):
for layer in QgsProject.instance().mapLayers().values():
if layer.name() == layer_name:
return layer
def set_color_stack(self, color_type):
# select the bands for color stack for Landsat 4, 5 y 7
if self.dockwidget.landsat_version in [4, 5, 7]:
if color_type == "natural_color":
bands = [3, 2, 1]
if color_type == "false_color":
bands = [4, 3, 2]
if color_type == "infrareds":
bands = [4, 5, 7]
# select the bands for color stack for Landsat 8
if self.dockwidget.landsat_version in [8, 9]:
if color_type == "natural_color":
bands = [4, 3, 2]
if color_type == "false_color":
bands = [5, 4, 3]
if color_type == "infrareds":
bands = [5, 6, 7]
self.dockwidget.SelectBand_R.setCurrentIndex(self.dockwidget.reflectance_bands.index(bands[0]))
self.dockwidget.SelectBand_G.setCurrentIndex(self.dockwidget.reflectance_bands.index(bands[1]))
self.dockwidget.SelectBand_B.setCurrentIndex(self.dockwidget.reflectance_bands.index(bands[2]))
@wait_process
def load_stack(self):
update_process_bar(self.dockwidget.bar_progressLoadStack, 40,
self.dockwidget.status_processLoadStack, self.tr("Loading stack..."))
bands = []
try:
bands.append(int(self.dockwidget.SelectBand_R.currentText()))
bands.append(int(self.dockwidget.SelectBand_G.currentText()))
bands.append(int(self.dockwidget.SelectBand_B.currentText()))
except:
update_process_bar(self.dockwidget.bar_progressLoadStack, 0,
self.dockwidget.status_processLoadStack, self.tr("Error: first choose a color stack"))
return
self.color_stack_scene = color_stack.ColorStack(self.dockwidget.mtl_path,
self.dockwidget.mtl_file,
bands,
self.dockwidget.tmp_dir)
self.color_stack_scene.do_color_stack()
self.color_stack_scene.load_color_stack()
update_process_bar(self.dockwidget.bar_progressLoadStack, 100,
self.dockwidget.status_processLoadStack, self.tr("DONE"))
@pyqtSlot()
def browser_dialog_to_load_file(self, combo_box, dialog_title, file_filters, suggested_path=""):
file_path, _ = QFileDialog.getOpenFileName(self.dockwidget, dialog_title, suggested_path, file_filters)
if file_path != '' and os.path.isfile(file_path):
# load to qgis and update combobox list
load_and_select_filepath_in(combo_box, file_path)
@wait_process
def process_mask(self):
"""Make the process
"""
# initialize the symbology
enable_symbology = [False, False, False, False, False, False, False, False, False]
# check if any filters has been enabled before process
if (not self.dockwidget.checkBox_FMask.isChecked() and
not self.dockwidget.checkBox_BlueBand.isChecked() and
not self.dockwidget.checkBox_CloudQA.isChecked() and
not self.dockwidget.checkBox_Aerosol.isChecked() and
not self.dockwidget.checkBox_PixelQA.isChecked() and
not self.dockwidget.checkBox_QABandC1L457.isChecked() and
not self.dockwidget.checkBox_QABandC1L89.isChecked() and
not self.dockwidget.checkBox_QABandC2.isChecked()):
self.dockwidget.status_processMask.setText(
self.tr("Error: no filters enabled for apply"))
return
# create the masking result instance if not exist
if (not isinstance(self.masking_result, cloud_filters.CloudMaskingResult) or
not self.masking_result.landsat_scene == self.dockwidget.mtl_file['LANDSAT_SCENE_ID'] or
not self.masking_result.collection == int(self.dockwidget.mtl_file['COLLECTION_NUMBER'])):
# create a new instance of cloud masking result
self.masking_result = cloud_filters.CloudMaskingResult(self.dockwidget.mtl_path,
self.dockwidget.mtl_file,
self.dockwidget.tmp_dir)
self.masking_result.process_status = self.dockwidget.status_processMask
self.masking_result.process_bar = self.dockwidget.bar_processMask
# re-init the result masking files
self.masking_result.cloud_masking_files = []
########################################
## Set the extent selector
# set for rectangular region
if self.dockwidget.checkBox_AOISelector.isChecked():
self.masking_result.clipping_with_aoi = True
self.masking_result.aoi_features = self.dockwidget.aoi_features
else:
self.masking_result.clipping_with_aoi = False
# set for the shape selector
if self.dockwidget.checkBox_ShapeSelector.isChecked():
self.masking_result.clipping_with_shape = True
self.masking_result.shape_layer = self.dockwidget.QCBox_MaskInShapeArea.currentLayer()
# get and save trim extent of shapefile for clip
canvas_crs = self.canvas.mapSettings().destinationCrs()
shape_crs = self.masking_result.shape_layer.crs()
shape_canvas_transform = QgsCoordinateTransform(shape_crs, canvas_crs, QgsProject.instance())
self.masking_result.shape_extent = shape_canvas_transform.transform(
self.masking_result.shape_layer.extent())
self.masking_result.shape_path = get_file_path_of_layer(
self.dockwidget.QCBox_MaskInShapeArea.currentLayer())
# check if the shape is a memory layer, then save and used it
if not os.path.isfile(self.masking_result.shape_path):
mem_layer = self.dockwidget.QCBox_MaskInShapeArea.currentLayer()
tmp_memory_fd, tmp_memory_file = tempfile.mkstemp(prefix='memory_layer_', suffix='.gpkg', dir=self.dockwidget.tmp_dir)
QgsVectorFileWriter.writeAsVectorFormat(mem_layer, tmp_memory_file, "UTF-8", mem_layer.crs(), "GPKG")
os.close(tmp_memory_fd)
self.masking_result.shape_path = tmp_memory_file
if self.dockwidget.shapeSelector_CutWithShape.isChecked():
self.masking_result.crop_to_cutline = True
else:
self.masking_result.crop_to_cutline = False
else:
self.masking_result.clipping_with_shape = False
# check extent area selector and shape file
if self.dockwidget.checkBox_AOISelector.isChecked() and (self.dockwidget.aoi_features is None \
or not self.dockwidget.aoi_features.hasFeatures()):
self.dockwidget.status_processMask.setText(
self.tr("Error: no AOI drawn in canvas"))
return
if self.dockwidget.checkBox_ShapeSelector.isChecked():
if not self.masking_result.shape_path:
self.dockwidget.status_processMask.setText(
self.tr("Error: not shape file defined"))
return
if not os.path.isfile(self.masking_result.shape_path):
self.dockwidget.status_processMask.setText(
self.tr("Error: shape file not exists"))
return
########################################
# FMask filter
if self.dockwidget.checkBox_FMask.isChecked():
# get enabled Fmask filters from UI and set symbology
enable_symbology[0:4] = [False, False, False, False]
filters_enabled = {"Fmask Cloud": False, "Fmask Shadow": False, "Fmask Snow": False, "Fmask Water": False, }
# Cloud
if self.dockwidget.checkBox_FMask_Cloud.isChecked():
enable_symbology[0] = True
filters_enabled["Fmask Cloud"] = True
# Shadow
if self.dockwidget.checkBox_FMask_Shadow.isChecked():
enable_symbology[1] = True
filters_enabled["Fmask Shadow"] = True
# Snow
if self.dockwidget.checkBox_FMask_Snow.isChecked():
enable_symbology[2] = True
filters_enabled["Fmask Snow"] = True
# Water
if self.dockwidget.checkBox_FMask_Water.isChecked():
enable_symbology[3] = True
filters_enabled["Fmask Water"] = True
# fmask filter
self.masking_result.do_fmask(
filters_enabled=filters_enabled,
cloud_prob_thresh=float(self.dockwidget.doubleSpinBox_CPT.value()),
cirrus_prob_ratio=float(self.dockwidget.doubleSpinBox_CPR.value()),
cloud_buffer_size=float(self.dockwidget.doubleSpinBox_CB.value()),
shadow_buffer_size=float(self.dockwidget.doubleSpinBox_SB.value()),
nir_fill_thresh=float(self.dockwidget.doubleSpinBox_NFT.value()),
swir2_thresh=float(self.dockwidget.doubleSpinBox_S2T.value()),
whiteness_thresh=float(self.dockwidget.doubleSpinBox_WT.value()),
swir2_water_test=float(self.dockwidget.doubleSpinBox_S2WT.value()),
nir_snow_thresh=float(self.dockwidget.doubleSpinBox_NST.value()),
green_snow_thresh=float(self.dockwidget.doubleSpinBox_GST.value()),
)
########################################
# Blue Band filter
if self.dockwidget.checkBox_BlueBand.isChecked():
self.masking_result.do_blue_band(int(self.dockwidget.doubleSpinBox_BB.value()))
enable_symbology[4] = True
########################################
# Cloud QA L457 filter
if self.dockwidget.checkBox_CloudQA.isChecked():
if self.dockwidget.landsat_version in [4, 5, 7]:
checked_items = {}
# one bit items selected
cloud_qa_items_1b = ["Dark Dense Vegetation (bit 0)", "Cloud (bit 1)", "Cloud Shadow (bit 2)",
"Adjacent to cloud (bit 3)", "Snow (bit 4)", "Water (bit 5)"]
for checkbox in self.dockwidget.widget_CloudQA_L457_bits.findChildren(QCheckBox):
item = checkbox.text().replace("&", "")
if item in cloud_qa_items_1b:
checked_items[item] = checkbox.isChecked()
# set and check the specific decimal values
try:
cloud_qa_svalues = self.dockwidget.CloudQA_L457_svalues.text()
if cloud_qa_svalues:
cloud_qa_svalues = [int(sv) for sv in cloud_qa_svalues.split(",")]
else:
cloud_qa_svalues = []
except:
self.dockwidget.status_processMask.setText(
self.tr("Error: setting the specific values in Cloud QA"))
return
# check is not selected any Cloud QA filter
if not any(checked_items.values()) and not cloud_qa_svalues:
self.dockwidget.status_processMask.setText(
self.tr("Error: no filters selected in Cloud QA"))
return
self.masking_result.do_cloud_qa_l457(self.dockwidget.cloud_qa_file, checked_items, cloud_qa_svalues)
enable_symbology[5] = True
########################################
# Aerosol L89 filter
if self.dockwidget.checkBox_Aerosol.isChecked():
if self.dockwidget.landsat_version in [8, 9]:
checked_items = {}
# one bit items selected
aerosol_items_1b = ["Aerosol Retrieval - Valid (bit 1)", "Aerosol Retrieval - Interpolated (bit 2)",
"Water Pixel (bit 3)"]
for checkbox in self.dockwidget.widget_Aerosol_bits.findChildren(QCheckBox):
item = checkbox.text().replace("&", "")
if item in aerosol_items_1b:
checked_items[item] = checkbox.isChecked()
# two bits items selected
aerosol_items_2b = ["Aerosol Content (bits 6-7)"]
levels = ["Climatology content", "Low content", "Average content", "High content"]
for groupbox in self.dockwidget.widget_Aerosol_bits.findChildren(QGroupBox):
group_item = groupbox.title().replace("&", "")
if group_item in aerosol_items_2b and groupbox.isChecked():
levels_selected = []
for radiobutton in groupbox.findChildren(QRadioButton):
check_item = radiobutton.text().replace("&", "")
if check_item in levels and radiobutton.isChecked():
levels_selected.append(check_item)
if levels_selected:
checked_items[group_item] = levels_selected
# set and check the specific decimal values
try:
aerosol_svalues = self.dockwidget.Aerosol_L89_svalues.text()
if aerosol_svalues:
aerosol_svalues = [int(sv) for sv in aerosol_svalues.split(",")]
else:
aerosol_svalues = []
except:
self.dockwidget.status_processMask.setText(
self.tr("Error: setting the specific values in Aerosol"))
return
# check is not selected any Aerosol filter
if not any(checked_items.values()) and not aerosol_svalues:
self.dockwidget.status_processMask.setText(
self.tr("Error: no filters selected in Aerosol"))
return
self.masking_result.do_aerosol_l89(self.dockwidget.aerosol_file, checked_items, aerosol_svalues)
enable_symbology[6] = True
########################################
# Pixel QA filter
if self.dockwidget.checkBox_PixelQA.isChecked():
checked_items = {}
# one bit items selected
if self.dockwidget.landsat_version in [4, 5, 7]:
pixel_qa_items_1b = ["Fill-nodata (bit 0)", "Water (bit 2)", "Cloud Shadow (bit 3)",
"Snow (bit 4)", "Cloud (bit 5)"]
if self.dockwidget.landsat_version in [8, 9]:
pixel_qa_items_1b = ["Fill-nodata (bit 0)", "Water (bit 2)", "Cloud Shadow (bit 3)",
"Snow (bit 4)", "Cloud (bit 5)", "Terrain Occlusion (bit 10)"]
for checkbox in self.dockwidget.widget_PixelQA_bits.findChildren(QCheckBox):
item = checkbox.text().replace("&", "")
if item in pixel_qa_items_1b:
checked_items[item] = checkbox.isChecked()
# two bits items selected
if self.dockwidget.landsat_version in [4, 5, 7]:
pixel_qa_items_2b = ["Cloud Confidence (bits 6-7)"]
if self.dockwidget.landsat_version in [8, 9]:
pixel_qa_items_2b = ["Cloud Confidence (bits 6-7)", "Cirrus Confidence (bits 8-9)"]
levels = ["0% None", "0-33% Low", "34-66% Medium", "67-100% High"]
for groupbox in self.dockwidget.widget_PixelQA_bits.findChildren(QGroupBox):
group_item = groupbox.title().replace("&", "")
if group_item in pixel_qa_items_2b and groupbox.isChecked():
levels_selected = []
for radiobutton in groupbox.findChildren(QRadioButton):
check_item = radiobutton.text().replace("&", "")
if check_item in levels and radiobutton.isChecked():
levels_selected.append(check_item)
if levels_selected:
checked_items[group_item] = levels_selected
# set and check the specific decimal values
try:
pixel_qa_svalues = self.dockwidget.PixelQA_svalues.text()
if pixel_qa_svalues:
pixel_qa_svalues = [int(sv) for sv in pixel_qa_svalues.split(",")]
else:
pixel_qa_svalues = []
except:
self.dockwidget.status_processMask.setText(
self.tr("Error: setting the specific values in Pixel QA"))
return
# check is not selected any Pixel QA filter
if not any(checked_items.values()) and not pixel_qa_svalues:
self.dockwidget.status_processMask.setText(
self.tr("Error: no filters selected in Pixel QA"))
return
self.masking_result.do_pixel_qa(self.dockwidget.pixel_qa_file, checked_items, pixel_qa_svalues)
enable_symbology[7] = True
########################################
# QA Band C1 filter L457
if self.dockwidget.checkBox_QABandC1L457.isChecked():
if self.dockwidget.landsat_version in [4, 5, 7]:
checked_items = {}
# one bit items selected
qaband_items_1b = ["Dropped Pixel (bit 1)", "Cloud (bit 4)"]
for checkbox in self.dockwidget.widget_QABandC1L457_bits.findChildren(QCheckBox):
item = checkbox.text().replace("&", "")
if item in qaband_items_1b:
checked_items[item] = checkbox.isChecked()
# two bits items selected
qaband_items_2b = ["Radiometric Saturation (bits 2-3)"]
levels = ["No bands saturated", "1 to 2 bands saturated", "3 to 4 bands saturated", "> 4 bands saturated"]
for groupbox in self.dockwidget.widget_QABandC1L457_bits.findChildren(QGroupBox):
group_item = groupbox.title().replace("&", "")
if group_item in qaband_items_2b and groupbox.isChecked():
levels_selected = []
for radiobutton in groupbox.findChildren(QRadioButton):
check_item = radiobutton.text().replace("&", "")
if check_item in levels and radiobutton.isChecked():
levels_selected.append(check_item)
if levels_selected:
checked_items[group_item] = levels_selected
# two bits items selected
qaband_items_2b = ["Cloud Confidence (bits 5-6)", "Cloud Shadow (bits 7-8)",
"Snow/Ice (bits 9-10)"]
levels = ["0% None", "0-33% Low", "34-66% Medium", "67-100% High"]
for groupbox in self.dockwidget.widget_QABandC1L457_bits.findChildren(QGroupBox):
group_item = groupbox.title().replace("&", "")
if group_item in qaband_items_2b and groupbox.isChecked():
levels_selected = []
for radiobutton in groupbox.findChildren(QRadioButton):
check_item = radiobutton.text().replace("&", "")
if check_item in levels and radiobutton.isChecked():
levels_selected.append(check_item)
if levels_selected:
checked_items[group_item] = levels_selected
# set and check the specific decimal values
try:
qaband_svalues = self.dockwidget.QABandC1L457_svalues.text()
if qaband_svalues:
qaband_svalues = [int(sv) for sv in qaband_svalues.split(",")]
else:
qaband_svalues = []
except:
self.dockwidget.status_processMask.setText(
self.tr("Error: setting the specific values in QA Band"))
return
# check is not selected any QA Band filter
if not any(checked_items.values()) and not qaband_svalues:
self.dockwidget.status_processMask.setText(
self.tr("Error: no filters selected in QA Band"))
return
self.masking_result.do_qaband_c1_l457(self.dockwidget.qabandc1_file_l457, checked_items, qaband_svalues)
enable_symbology[8] = True
########################################
# QA Band C1 filter L89
if self.dockwidget.checkBox_QABandC1L89.isChecked():
if self.dockwidget.landsat_version in [8, 9]:
checked_items = {}
# one bit items selected
qaband_items_1b = ["Terrain Occlusion (bit 1)", "Cloud (bit 4)"]
for checkbox in self.dockwidget.widget_QABandC1L89_bits.findChildren(QCheckBox):
item = checkbox.text().replace("&", "")
if item in qaband_items_1b:
checked_items[item] = checkbox.isChecked()
# two bits items selected
qaband_items_2b = ["Radiometric Saturation (bits 2-3)"]
levels = ["No bands saturated", "1 to 2 bands saturated", "3 to 4 bands saturated", "> 4 bands saturated"]
for groupbox in self.dockwidget.widget_QABandC1L89_bits.findChildren(QGroupBox):
group_item = groupbox.title().replace("&", "")
if group_item in qaband_items_2b and groupbox.isChecked():
levels_selected = []
for radiobutton in groupbox.findChildren(QRadioButton):
check_item = radiobutton.text().replace("&", "")
if check_item in levels and radiobutton.isChecked():
levels_selected.append(check_item)
if levels_selected:
checked_items[group_item] = levels_selected
# two bits items selected
qaband_items_2b = ["Cloud Confidence (bits 5-6)", "Cloud Shadow (bits 7-8)",
"Snow/Ice (bits 9-10)", "Cirrus Confidence (bits 11-12)"]
levels = ["0% None", "0-33% Low", "34-66% Medium", "67-100% High"]
for groupbox in self.dockwidget.widget_QABandC1L89_bits.findChildren(QGroupBox):
group_item = groupbox.title().replace("&", "")
if group_item in qaband_items_2b and groupbox.isChecked():
levels_selected = []
for radiobutton in groupbox.findChildren(QRadioButton):
check_item = radiobutton.text().replace("&", "")
if check_item in levels and radiobutton.isChecked():
levels_selected.append(check_item)
if levels_selected:
checked_items[group_item] = levels_selected
# set and check the specific decimal values
try:
qaband_svalues = self.dockwidget.QABandC1L89_svalues.text()
if qaband_svalues:
qaband_svalues = [int(sv) for sv in qaband_svalues.split(",")]
else:
qaband_svalues = []
except:
self.dockwidget.status_processMask.setText(
self.tr("Error: setting the specific values in QA Band"))
return
# check is not selected any QA Band filter
if not any(checked_items.values()) and not qaband_svalues:
self.dockwidget.status_processMask.setText(
self.tr("Error: no filters selected in QA Band"))
return
self.masking_result.do_qaband_c1_l89(self.dockwidget.qabandc1_file_l89, checked_items, qaband_svalues)
enable_symbology[8] = True
########################################
# QA Band C2 filter
if self.dockwidget.checkBox_QABandC2.isChecked():
if self.dockwidget.landsat_version in [4, 5, 7, 8, 9]:
checked_items = {}
# one bit items selected
qaband_items_1b = ["Dilated Cloud (bit 1)", "Cirrus (bit 2)", "Cloud (bit 3)",
"Cloud Shadow (bit 4)", "Snow (bit 5)", "Water (bit 7)"]
for checkbox in self.dockwidget.widget_QABandC2_bits.findChildren(QCheckBox):
item = checkbox.text().replace("&", "")
if item in qaband_items_1b:
checked_items[item] = checkbox.isChecked()
# two bits items selected
qaband_items_2b = ["Cloud Confidence (bits 8-9)", "Cloud Shadow Confidence (bits 10-11)",
"Snow/Ice Confidence (bits 12-13)", "Cirrus Confidence (bits 14-15)"]
levels = ["Low", "Medium", "High"]
for groupbox in self.dockwidget.widget_QABandC2_bits.findChildren(QGroupBox):
group_item = groupbox.title().replace("&", "")
if group_item in qaband_items_2b and groupbox.isChecked():
levels_selected = []
for radiobutton in groupbox.findChildren(QRadioButton):
check_item = radiobutton.text().replace("&", "")
if check_item in levels and radiobutton.isChecked():
levels_selected.append(check_item)
if levels_selected:
checked_items[group_item] = levels_selected
# set and check the specific decimal values
try:
qaband_svalues = self.dockwidget.QABandC2_svalues.text()
if qaband_svalues:
qaband_svalues = [int(sv) for sv in qaband_svalues.split(",")]
else:
qaband_svalues = []
except:
self.dockwidget.status_processMask.setText(
self.tr("Error: setting the specific values in QA Band"))
return
# check is not selected any QA Band filter
if not any(checked_items.values()) and not qaband_svalues:
self.dockwidget.status_processMask.setText(
self.tr("Error: no filters selected in QA Band"))
return
self.masking_result.do_qaband_c2(self.dockwidget.qabandc2_file, checked_items, qaband_svalues)
enable_symbology[8] = True
########################################
# Blended cloud masking files
# only one filter is activated
if len(self.masking_result.cloud_masking_files) == 1:
self.final_cloud_mask_file = self.masking_result.cloud_masking_files[0]
# two filters are activated
if len(self.masking_result.cloud_masking_files) == 2:
self.final_cloud_mask_file = os.path.join(self.dockwidget.tmp_dir,
"cloud_blended_{}.tif".format(datetime.now().strftime('%H%M%S')))
gdal_calc.Calc(calc="A*(A>1)+B*(A==1)", outfile=self.final_cloud_mask_file,
A=self.masking_result.cloud_masking_files[0], B=self.masking_result.cloud_masking_files[1])
# three filters are activated
if len(self.masking_result.cloud_masking_files) == 3:
self.final_cloud_mask_file = os.path.join(self.dockwidget.tmp_dir,
"cloud_blended_{}.tif".format(datetime.now().strftime('%H%M%S')))
gdal_calc.Calc(calc="A*(A>1)+B*logical_and(A==1,B>1)+C*logical_and(A==1,B==1)",
outfile=self.final_cloud_mask_file,
A=self.masking_result.cloud_masking_files[0], B=self.masking_result.cloud_masking_files[1],
C=self.masking_result.cloud_masking_files[2])
# four filters are activated
if len(self.masking_result.cloud_masking_files) == 4:
self.final_cloud_mask_file = os.path.join(self.dockwidget.tmp_dir,
"cloud_blended_{}.tif".format(datetime.now().strftime('%H%M%S')))
gdal_calc.Calc(calc="A*(A>1)+B*logical_and(A==1,B>1)+C*logical_and(logical_and(A==1,B==1),C>1)"
"+D*logical_and(logical_and(A==1,B==1),C==1)",
outfile=self.final_cloud_mask_file,
A=self.masking_result.cloud_masking_files[0], B=self.masking_result.cloud_masking_files[1],
C=self.masking_result.cloud_masking_files[2], D=self.masking_result.cloud_masking_files[3])
# five filters are activated
if len(self.masking_result.cloud_masking_files) == 5:
self.final_cloud_mask_file = os.path.join(self.dockwidget.tmp_dir,
"cloud_blended_{}.tif".format(datetime.now().strftime('%H%M%S')))
gdal_calc.Calc(calc="A*(A>1)+B*logical_and(A==1,B>1)+C*logical_and(logical_and(A==1,B==1),C>1)"
"+D*logical_and(logical_and(A==1,B==1,C==1),D>1)+E*logical_and(logical_and(A==1,B==1,C==1),D==1)",
outfile=self.final_cloud_mask_file,
A=self.masking_result.cloud_masking_files[0], B=self.masking_result.cloud_masking_files[1],
C=self.masking_result.cloud_masking_files[2], D=self.masking_result.cloud_masking_files[3],
E=self.masking_result.cloud_masking_files[4])
########################################
# keep the data outside the shape area as valid data (=1), important for apply several mask
if (self.dockwidget.checkBox_ShapeSelector.isChecked() and not self.dockwidget.shapeSelector_CutWithShape.isChecked()) or \
self.dockwidget.checkBox_AOISelector.isChecked():
self.masking_result.clip(self.final_cloud_mask_file, self.final_cloud_mask_file.replace(".tif", "1.tif"),
nodata=1, process_bar=False)
os.remove(self.final_cloud_mask_file)
# expand to original extent
img_path = get_prefer_name(os.path.join(os.path.dirname(self.dockwidget.mtl_path),
self.dockwidget.mtl_file['FILE_NAME_BAND_1']))
if not os.path.exists(img_path):
img_path = get_prefer_name(os.path.join(os.path.dirname(self.dockwidget.mtl_path),
self.dockwidget.mtl_file['FILE_NAME_BAND_SR_1']))
extent = get_extent(img_path)
gdal.Translate(self.final_cloud_mask_file,
self.final_cloud_mask_file.replace(".tif", "1.tif"),
projWin=extent, noData=1)
os.remove(self.final_cloud_mask_file.replace(".tif", "1.tif"))
# unset nodata
cmd = ['gdal_edit' if platform.system() == 'Windows' else 'gdal_edit.py',
'"{}"'.format(self.final_cloud_mask_file), '-unsetnodata']
call(" ".join(cmd), shell=True)
else:
# mask the nodata value as 255 value
self.masking_result.do_nodata_mask(self.final_cloud_mask_file)
########################################
# Delete data outside the shapefile or selected area, as 255 value
if self.dockwidget.checkBox_ShapeSelector.isChecked() and self.dockwidget.shapeSelector_CutWithShape.isChecked():
# expand to original extent
img_path = get_prefer_name(os.path.join(os.path.dirname(self.dockwidget.mtl_path),
self.dockwidget.mtl_file['FILE_NAME_BAND_1']))
if not os.path.exists(img_path):
img_path = get_prefer_name(os.path.join(os.path.dirname(self.dockwidget.mtl_path),
self.dockwidget.mtl_file['FILE_NAME_BAND_SR_1']))
extent = get_extent(img_path)
gdal.Translate(self.final_cloud_mask_file.replace(".tif", "1.tif"), self.final_cloud_mask_file,
projWin=extent, noData=255)
os.remove(self.final_cloud_mask_file)
os.rename(self.final_cloud_mask_file.replace(".tif", "1.tif"), self.final_cloud_mask_file)
# unset nodata
cmd = ['gdal_edit' if platform.system() == 'Windows' else 'gdal_edit.py',
'"{}"'.format(self.final_cloud_mask_file), '-unsetnodata']
call(" ".join(cmd), shell=True)
########################################
# Post process mask
# delete unused output
# if memory/scratch layer was used
if self.dockwidget.checkBox_ShapeSelector.isChecked() and \
not os.path.isfile(get_file_path_of_layer(self.dockwidget.QCBox_MaskInShapeArea.currentLayer())):
if os.path.isfile(tmp_memory_file):
os.remove(tmp_memory_file)
# from fmask
if self.dockwidget.checkBox_FMask.isChecked():
os.remove(self.masking_result.angles_file)
os.remove(self.masking_result.saturationmask_file)
os.remove(self.masking_result.toa_file)
if os.path.isfile(self.masking_result.reflective_stack_clip_file):
os.remove(self.masking_result.reflective_stack_clip_file)
if os.path.isfile(self.masking_result.thermal_stack_clip_file):
os.remove(self.masking_result.thermal_stack_clip_file)
# from blue band
if self.dockwidget.checkBox_BlueBand.isChecked():
if os.path.isfile(self.masking_result.blue_band_clip_file):
os.remove(self.masking_result.blue_band_clip_file)
# from cloud QA
if self.dockwidget.checkBox_CloudQA.isChecked():
if os.path.isfile(self.masking_result.cloud_qa_clip_file):
os.remove(self.masking_result.cloud_qa_clip_file)
# from aerosol
if self.dockwidget.checkBox_Aerosol.isChecked():
if os.path.isfile(self.masking_result.aerosol_clip_file):
os.remove(self.masking_result.aerosol_clip_file)
# from Pixel QA
if self.dockwidget.checkBox_PixelQA.isChecked():
if os.path.isfile(self.masking_result.pixel_qa_clip_file):
os.remove(self.masking_result.pixel_qa_clip_file)
# from QA Band
if self.dockwidget.checkBox_QABandC1L457.isChecked() or \
self.dockwidget.checkBox_QABandC1L89.isChecked() or \
self.dockwidget.checkBox_QABandC2.isChecked():
if os.path.isfile(self.masking_result.qaband_clip_file):
os.remove(self.masking_result.qaband_clip_file)
# from original blended files
for cloud_masking_file in self.masking_result.cloud_masking_files:
if cloud_masking_file != self.final_cloud_mask_file:
os.remove(cloud_masking_file)
# Add to QGIS the reflectance stack file and cloud file
if self.masking_result.clipping_with_aoi:
masking_result_name = self.tr("Cloud Mask in area ({})".format(datetime.now().strftime('%H:%M:%S')))
elif self.dockwidget.checkBox_ShapeSelector.isChecked():
masking_result_name = self.tr("Cloud Mask in shape ({})".format(datetime.now().strftime('%H:%M:%S')))
else:
masking_result_name = self.tr("Cloud Mask ({})".format(datetime.now().strftime('%H:%M:%S')))
self.cloud_mask_rlayer = QgsRasterLayer(self.final_cloud_mask_file, masking_result_name)
QgsProject.instance().addMapLayer(self.cloud_mask_rlayer)
# Set symbology (thematic color and name) for new raster layer
symbology = {
'Fmask Cloud': (255, 0, 255, 255),
'Fmask Shadow': (255, 255, 0, 255),
'Fmask Snow': (85, 255, 255, 255),
'Fmask Water': (0, 0, 200, 255),
'Blue Band': (120, 212, 245, 255),
'Cloud QA': (255, 170, 0, 255),
'Aerosol': (255, 170, 0, 255),
'Pixel QA': (20, 180, 140, 255),
'QA Band': (170, 85, 255, 255),
}
# apply
apply_symbology(self.cloud_mask_rlayer,
symbology,
enable_symbology,
transparent=[])
# Refresh layer symbology
layer_node = QgsProject.instance().layerTreeRoot().findLayer(self.cloud_mask_rlayer)
self.iface.layerTreeView().layerTreeModel().refreshLayerLegend(layer_node)
# unselect AOI
self.dockwidget.checkBox_AOISelector.setChecked(False)
@wait_process
def fileDialog_exportSimpleMask(self):
"""Open QFileDialog for save mask file
"""
suggested_filename_mask = self.dockwidget.mtl_file['LANDSAT_SCENE_ID'] + "_Mask.tif"
mask_outpath, _ = QFileDialog.getSaveFileName(self.dockwidget, self.tr("Export the mask file"),
os.path.join(os.path.dirname(self.dockwidget.mtl_path),
suggested_filename_mask),
self.tr("GeoTiff files (*.tif);;All files (*.*)"))
mask_inpath = get_file_path_of_layer(self.getLayerByName(self.dockwidget.select_SingleLayerMask.currentText()))
if mask_outpath != '' and mask_inpath != '':
# unset nodata
cmd = ['gdal_edit' if platform.system() == 'Windows' else 'gdal_edit.py',
'"{}"'.format(mask_inpath), '-unsetnodata']
call(" ".join(cmd), shell=True)
cmd = ['gdal_calc' if platform.system() == 'Windows' else 'gdal_calc.py', '--quiet', '--overwrite',
'--calc "1*(A==1)+0*(A!=1)"', '-A "{}"'.format(mask_inpath), '--outfile "{}"'.format(mask_outpath),
'--type="Byte"', '--co COMPRESS=PACKBITS']