Exemplo n.º 1
0
    def __init__(self, mtl_path, mtl_file, tmp_dir=None):
        self.mtl_path = mtl_path
        self.mtl_file = mtl_file
        # dir to input landsat files
        self.input_dir = os.path.dirname(mtl_path)
        # tmp dir for process
        if tmp_dir:
            self.tmp_dir = tmp_dir
        else:
            self.tmp_dir = tempfile.mkdtemp()
        # bar and status progress
        self.process_status = None
        self.process_bar = None
        # set initial clipping status
        self.clipping_with_aoi = False
        self.clipping_with_shape = False
        # save all result files of cloud masking
        self.cloud_masking_files = []

        # get_metadata
        self.landsat_version = int(self.mtl_file['SPACECRAFT_ID'][-1])
        self.landsat_scene = self.mtl_file['LANDSAT_SCENE_ID']

        # set bands for reflective and thermal
        if self.landsat_version in [4, 5]:
            # get the reflective file names bands
            self.reflective_bands = [
                os.path.join(self.input_dir, self.mtl_file['FILE_NAME_BAND_'+str(N)])
                for N in [1, 2, 3, 4, 5, 7]]
            # get the thermal file names bands
            self.thermal_bands = [
                os.path.join(self.input_dir, self.mtl_file['FILE_NAME_BAND_' + str(N)])
                for N in [6]]

        # set bands for reflective and thermal
        if self.landsat_version == 7:
            # get the reflective file names bands
            self.reflective_bands = [
                os.path.join(self.input_dir, self.mtl_file['FILE_NAME_BAND_'+str(N)])
                for N in [1, 2, 3, 4, 5, 7]]
            # get the thermal file names bands
            self.thermal_bands = [
                os.path.join(self.input_dir, self.mtl_file['FILE_NAME_BAND_6_VCID_' + str(N)])
                for N in [1, 2]]

        # set bands for reflective and thermal
        if self.landsat_version == 8:
            # get the reflective file names bands
            self.reflective_bands = [
                os.path.join(self.input_dir, self.mtl_file['FILE_NAME_BAND_'+str(N)])
                for N in [1, 2, 3, 4, 5, 6, 7, 9]]
            # get the thermal file names bands
            self.thermal_bands = [
                os.path.join(self.input_dir, self.mtl_file['FILE_NAME_BAND_' + str(N)])
                for N in [10, 11]]

        # set the prefer file name band for process
        self.reflective_bands = [get_prefer_name(file_path) for file_path in self.reflective_bands]
        self.thermal_bands = [get_prefer_name(file_path) for file_path in self.thermal_bands]
Exemplo n.º 2
0
    def __init__(self, mtl_path, mtl_file, bands, tmp_dir=None):
        self.mtl_path = mtl_path
        self.mtl_file = mtl_file
        # dir to input landsat files
        self.input_dir = os.path.dirname(mtl_path)
        # tmp dir for process
        if tmp_dir:
            self.tmp_dir = tmp_dir
        else:
            self.tmp_dir = tempfile.mkdtemp()
        # bar and status progress
        self.process_status = None
        self.process_bar = None
        # set base name
        self.base_name = "RGB ({}-{}-{})".format(*bands)

        # get file names
        self.color_bands = [
            os.path.join(self.input_dir,
                         self.mtl_file['FILE_NAME_BAND_' + str(N)])
            for N in bands
        ]

        # set the prefer file name band for process
        self.color_bands = [
            get_prefer_name(file_path) for file_path in self.color_bands
        ]
Exemplo n.º 3
0
    def do_nodata_mask(self, img_to_mask):
        band_1 = get_prefer_name(os.path.join(self.input_dir, self.mtl_file['FILE_NAME_BAND_1']))

        band_from_mask = self.clip(band_1, os.path.join(self.tmp_dir, "band_from_mask.tif"), process_bar=False)

        cmd = ['gdal_calc' if platform.system() == 'Windows' else 'gdal_calc.py', '--quiet', '--overwrite',
               '--calc "A*(B>0)+255*logical_or(B==0,A==0)"', '-A {}'.format(img_to_mask), '-B {}'.format(band_from_mask),
               '--outfile "{}"'.format(img_to_mask)]
        call(" ".join(cmd), shell=True)

        # unset nodata
        cmd = ['gdal_edit' if platform.system() == 'Windows' else 'gdal_edit.py', '"{}"'.format(img_to_mask), '-unsetnodata']
        call(" ".join(cmd), shell=True)
Exemplo n.º 4
0
    def do_blue_band(self, bb_threshold):
        # tmp file for cloud
        self.cloud_bb_file = os.path.join(
            self.tmp_dir,
            "cloud_bb_{}.tif".format(datetime.now().strftime('%H%M%S')))
        update_process_bar(self.process_bar, 50, self.process_status,
                           self.tr("Making the blue band filter..."))

        ########################################
        # select the Blue Band
        if self.landsat_version in [4, 5, 7]:
            # get the reflective file names bands
            self.blue_band_file = os.path.join(
                self.input_dir, self.mtl_file['FILE_NAME_BAND_1'])
        if self.landsat_version in [8]:
            # get the reflective file names bands
            self.blue_band_file = os.path.join(
                self.input_dir, self.mtl_file['FILE_NAME_BAND_2'])

        # fix file name
        self.blue_band_file = get_prefer_name(self.blue_band_file)

        ########################################
        # clipping the Blue Band (only if is activated selected area or shape area)
        self.blue_band_clip_file = os.path.join(self.tmp_dir,
                                                "blue_band_clip.tif")
        self.blue_band_for_process = self.clip(self.blue_band_file,
                                               self.blue_band_clip_file)

        ########################################
        # do blue band filter
        gdal_calc.Calc(calc="1*(A<{threshold})+6*(A>={threshold})".format(
            threshold=bb_threshold),
                       A=self.blue_band_for_process,
                       outfile=self.cloud_bb_file,
                       type="Byte")

        # save final result of masking
        self.cloud_masking_files.append(self.cloud_bb_file)

        ### ending process
        update_process_bar(self.process_bar, 100, self.process_status,
                           self.tr("DONE"))
Exemplo n.º 5
0
    def do_nodata_mask(self, img_to_mask):
        band_1 = get_prefer_name(
            os.path.join(self.input_dir, self.mtl_file['FILE_NAME_BAND_1']))

        band_from_mask = self.clip(band_1,
                                   os.path.join(self.tmp_dir,
                                                "band_from_mask.tif"),
                                   process_bar=False)

        gdal_calc.Calc(calc="A*(B>0)+255*logical_or(B==0,A==0)",
                       outfile=img_to_mask.replace(".tif", "1.tif"),
                       A=img_to_mask,
                       B=band_from_mask,
                       NoDataValue=255)

        # unset the nodata
        os.remove(img_to_mask)
        if os.path.isfile(os.path.join(self.tmp_dir, "band_from_mask.tif")):
            os.remove(os.path.join(self.tmp_dir, "band_from_mask.tif"))
        gdal.Translate(img_to_mask,
                       img_to_mask.replace(".tif", "1.tif"),
                       noData="none")
Exemplo n.º 6
0
    def do_blue_band(self, bb_threshold):
        # tmp file for cloud
        self.cloud_bb_file = os.path.join(self.tmp_dir, "cloud_bb_{}.tif".format(datetime.now().strftime('%H%M%S')))
        update_process_bar(self.process_bar, 50, self.process_status,
                           self.tr("Making the blue band filter..."))

        ########################################
        # select the Blue Band
        if self.landsat_version in [4, 5, 7]:
            # get the reflective file names bands
            self.blue_band_file = os.path.join(self.input_dir, self.mtl_file['FILE_NAME_BAND_1'])
        if self.landsat_version in [8]:
            # get the reflective file names bands
            self.blue_band_file = os.path.join(self.input_dir, self.mtl_file['FILE_NAME_BAND_2'])

        # fix file name
        self.blue_band_file = get_prefer_name(self.blue_band_file)

        ########################################
        # clipping the Blue Band (only if is activated selected area or shape area)
        self.blue_band_clip_file = os.path.join(self.tmp_dir, "blue_band_clip.tif")
        self.blue_band_for_process = self.clip(self.blue_band_file, self.blue_band_clip_file)

        ########################################
        # do blue band filter
        cmd = ['gdal_calc' if platform.system() == 'Windows' else 'gdal_calc.py', '--quiet', '--overwrite',
               '--calc "1*(A<{threshold})+6*(A>={threshold})"'.format(threshold=bb_threshold),
               '-A {}'.format(self.blue_band_for_process), '--outfile "{}"'.format(self.cloud_bb_file),
               '--type="Byte"', '--co COMPRESS=PACKBITS']
        call(" ".join(cmd), shell=True)

        # save final result of masking
        self.cloud_masking_files.append(self.cloud_bb_file)

        ### ending process
        update_process_bar(self.process_bar, 100, self.process_status,
                           self.tr("DONE"))
Exemplo n.º 7
0
    def load_MTL(self):
        """Load a new MTL file currently specified in QLineEdit"""

        self.mtl_path = self.lineEdit_PathMTL.text()

        # check if MTL exist
        if not os.path.isfile(self.mtl_path):
            self.status_LoadedMTL.setText(self.tr("Error: File not exist"))
            self.unload_MTL()
            return

        # parse the new MTL file
        try:
            self.mtl_file = cloud_masking_utils.mtl2dict(self.mtl_path)
            # get the landsat version
            self.landsat_version = int(self.mtl_file['SPACECRAFT_ID'][-1])
            # normalize metadata for old MLT format (old Landsat 4 and 5)
            if 'BAND1_FILE_NAME' in self.mtl_file:
                for N in [1, 2, 3, 4, 5, 7, 6]:
                    self.mtl_file['FILE_NAME_BAND_' +
                                  str(N)] = self.mtl_file['BAND' + str(N) +
                                                          '_FILE_NAME']
            if 'METADATA_L1_FILE_NAME' in self.mtl_file:
                self.mtl_file['LANDSAT_SCENE_ID'] = self.mtl_file[
                    'METADATA_L1_FILE_NAME'].split('_MTL.txt')[0]
        except:
            self.status_LoadedMTL.setText(
                self.tr("Error: Cannot parse MTL file"))
            self.unload_MTL()
            return

        # check if there are the basic images for process
        # these are: "_bandX.tif" or "_BX.TIF"
        #
        # set bands for reflective and thermal
        if self.landsat_version in [4, 5]:
            # get the reflective file names bands
            reflective_and_thermal_bands = [
                os.path.join(os.path.dirname(self.mtl_path),
                             self.mtl_file['FILE_NAME_BAND_' + str(N)])
                for N in [1, 2, 3, 4, 5, 7, 6]
            ]
        if self.landsat_version in [7]:
            # get the reflective file names bands
            reflective_and_thermal_bands = [
                os.path.join(os.path.dirname(self.mtl_path),
                             self.mtl_file['FILE_NAME_BAND_' + str(N)])
                for N in [1, 2, 3, 4, 5, 7]
            ]
            reflective_and_thermal_bands += [
                os.path.join(os.path.dirname(self.mtl_path),
                             self.mtl_file['FILE_NAME_BAND_6_VCID_' + str(N)])
                for N in [1, 2]
            ]
        if self.landsat_version in [8]:
            # get the reflective file names bands
            reflective_and_thermal_bands = [
                os.path.join(os.path.dirname(self.mtl_path),
                             self.mtl_file['FILE_NAME_BAND_' + str(N)])
                for N in [1, 2, 3, 4, 5, 6, 7, 9, 10, 11]
            ]

        # set the prefer file name band for process
        reflective_and_thermal_bands = [
            get_prefer_name(file_path)
            for file_path in reflective_and_thermal_bands
        ]

        # check if reflective_and_thermal_bands exists
        for file_path in reflective_and_thermal_bands:
            if not os.path.isfile(file_path):
                msg = "The file {} not exists, is necessary that the raw bands _bandN.tif or _BN.TIF of Landsat " \
                      "are in the same location as the MTL file.".format(os.path.basename(file_path))
                QMessageBox.question(None,
                                     'Problem while Loading the new MTL...',
                                     msg, QMessageBox.Ok)
                self.status_LoadedMTL.setText(
                    self.tr("Error: Not raw landsat files"))
                self.unload_MTL()
                return

        #### Process post MTL loaded (If we load it okay)
        # tmp dir for process this MTL
        self.tmp_dir = tempfile.mkdtemp()
        # MTL info
        self.status_LoadedMTL.setChecked(True)
        self.status_LoadedMTL.setText(self.mtl_file['LANDSAT_SCENE_ID'] +
                                      ' (L{})'.format(self.landsat_version))

        #### activate, load and adjust UI
        self.activate_UI()