コード例 #1
0
ファイル: cloud_filters.py プロジェクト: SMByC/CloudMasking
    def do_cloud_qa_l457(self, cloud_qa_file, checked_items, specific_values=[]):
        # tmp file for cloud
        self.cloud_qa = os.path.join(self.tmp_dir, "cloud_qa_{}.tif".format(datetime.now().strftime('%H%M%S')))
        update_process_bar(self.process_bar, 50, self.process_status,
                           self.tr("Making the Cloud QA filter..."))

        ########################################
        # clipping the QA Mask (only if is activated selected area or shape area)
        self.cloud_qa_clip_file = os.path.join(self.tmp_dir, "cloud_qa_clip.tif")
        self.cloud_qa_for_process = self.clip(cloud_qa_file, self.cloud_qa_clip_file)

        ########################################
        # convert selected items to binary and decimal values
        values_combinations = []
        # bits not used or not fill
        static_bits = [6, 7]

        # generate the values combinations for one bit items selected
        cloud_qa_items_1b = {"Dark Dense Vegetation (bit 0)": [0], "Cloud (bit 1)": [1], "Cloud Shadow (bit 2)": [2],
                             "Adjacent to cloud (bit 3)": [3], "Snow (bit 4)": [4], "Water (bit 5)": [5]}

        for item, bits in cloud_qa_items_1b.items():
            binary = [0] * 8
            if checked_items[item]:
                binary[(len(binary) - 1) - bits[0]] = 1
                values_combinations += list(binary_combination(binary, static_bits + bits))

        # add the specific values
        if specific_values:
            values_combinations += specific_values

        # delete duplicates
        values_combinations = list(set(values_combinations))

        # only left the values inside the image
        values_combinations = check_values_in_image(self.cloud_qa_for_process, values_combinations)

        filter_values = ",".join(["A==" + str(x) for x in values_combinations])
        not_filter_values = ",".join(["A!=" + str(x) for x in values_combinations])

        ########################################
        # do QA Mask filter
        gdal_calc.Calc(calc="1*(numpy.all([{nfv}], axis=0)) + 7*(numpy.any([{fv}], axis=0))".format(fv=filter_values,
                                                                                                    nfv=not_filter_values),
                       A=self.cloud_qa_for_process, outfile=self.cloud_qa, type="Byte", NoDataValue=1)
        # unset the nodata, leave the 1 (valid fields)
        cmd = ['gdal_edit' if platform.system() == 'Windows' else 'gdal_edit.py', '"{}"'.format(self.cloud_qa), '-unsetnodata']
        call(" ".join(cmd), shell=True)

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

        ### ending process
        update_process_bar(self.process_bar, 100, self.process_status,
                           self.tr("DONE"))
コード例 #2
0
ファイル: cloud_filters.py プロジェクト: RSIP4SH/CloudMasking
    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"))
コード例 #3
0
ファイル: cloud_filters.py プロジェクト: RSIP4SH/CloudMasking
    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")
コード例 #4
0
ファイル: cloud_filters.py プロジェクト: SMByC/CloudMasking
    def do_pixel_qa(self, pixel_qa_file, checked_items, specific_values=[]):
        """
        http://landsat.usgs.gov/qualityband.php
        """
        # tmp file for Pixel QA
        self.pixel_qa = os.path.join(self.tmp_dir, "pixel_qa_{}.tif".format(datetime.now().strftime('%H%M%S')))
        update_process_bar(self.process_bar, 50, self.process_status,
                           self.tr("Making the Pixel QA filter..."))

        ########################################
        # clipping the QA Mask (only if is activated selected area or shape area)
        self.pixel_qa_clip_file = os.path.join(self.tmp_dir, "pixel_qa_clip.tif")
        self.pixel_qa_for_process = self.clip(pixel_qa_file, self.pixel_qa_clip_file)

        ########################################
        # convert selected items to binary and decimal values
        values_combinations = []
        # bits not used or not fill
        if self.landsat_version in [4, 5, 7]:
            static_bits = [0, 1, 8, 9, 10, 11, 12, 13, 14, 15]
        if self.landsat_version in [8]:
            static_bits = [0, 1, 11, 12, 13, 14, 15]

        # generate the values combinations for one bit items selected
        pixel_qa_items_1b = {"Water (bit 2)": [2], "Cloud Shadow (bit 3)": [3],
                             "Snow (bit 4)": [4], "Cloud (bit 5)": [5]}
        if self.landsat_version in [8]:  # add to L8
            pixel_qa_items_1b["Terrain Occlusion (bit 10)"] = [10]

        for item, bits in pixel_qa_items_1b.items():
            binary = [0]*16
            if checked_items[item]:
                binary[(len(binary) - 1) - bits[0]] = 1
                values_combinations += list(binary_combination(binary, static_bits + bits))

        # generate the values combinations for two bits items selected
        if self.landsat_version in [4, 5, 7]:
            pixel_qa_items_2b = {"Cloud Confidence (bits 6-7)": [6, 7]}
        if self.landsat_version in [8]:
            pixel_qa_items_2b = {"Cloud Confidence (bits 6-7)": [6, 7], "Cirrus Confidence (bits 8-9)": [8, 9]}
        levels = {"0% None": [0, 0], "0-33% Low": [0, 1],
                  "34-66% Medium": [1, 0], "67-100% High": [1, 1]}

        for item, bits in pixel_qa_items_2b.items():
            if item in checked_items.keys():
                for level in checked_items[item]:
                    binary = [0] * 16
                    binary[bits[0]:bits[1]+1] = (levels[level])[::-1]
                    binary.reverse()
                    values_combinations += list(binary_combination(binary, static_bits + bits))

        # add the specific values
        if specific_values:
            values_combinations += specific_values

        # delete duplicates
        values_combinations = list(set(values_combinations))

        # only left the values inside the image
        values_combinations = check_values_in_image(self.pixel_qa_for_process, values_combinations)

        filter_values = ",".join(["A==" + str(x) for x in values_combinations])
        not_filter_values = ",".join(["A!=" + str(x) for x in values_combinations])

        ########################################
        # do QA Mask filter
        gdal_calc.Calc(calc="1*(numpy.all([{nfv}], axis=0)) + 9*(numpy.any([{fv}], axis=0))".format(fv=filter_values, nfv=not_filter_values),
                       A=self.pixel_qa_for_process, outfile=self.pixel_qa, type="Byte", NoDataValue=1)

        # unset nodata
        cmd = ['gdal_edit' if platform.system() == 'Windows' else 'gdal_edit.py',
               '"{}"'.format(self.pixel_qa), '-unsetnodata']
        call(" ".join(cmd), shell=True)

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

        ### ending process
        update_process_bar(self.process_bar, 100, self.process_status,
                           self.tr("DONE"))
コード例 #5
0
ファイル: cloud_filters.py プロジェクト: SMByC/CloudMasking
    def do_aerosol_l8(self, aerosol_file, checked_items, specific_values=[]):
        # tmp file for cloud
        self.aerosol = os.path.join(self.tmp_dir, "aerosol_{}.tif".format(datetime.now().strftime('%H%M%S')))
        update_process_bar(self.process_bar, 50, self.process_status,
                           self.tr("Making the Aerosol filter..."))

        ########################################
        # clipping the QA Mask (only if is activated selected area or shape area)
        self.aerosol_clip_file = os.path.join(self.tmp_dir, "aerosol_clip.tif")
        self.aerosol_for_process = self.clip(aerosol_file, self.aerosol_clip_file)

        ########################################
        # convert selected items to binary and decimal values
        values_combinations = []
        # bits not used or not fill
        static_bits = [0, 4, 5]

        # generate the values combinations for one bit items selected
        aerosol_items_1b = {"Aerosol Retrieval - Valid (bit 1)": [1],
                            "Aerosol Retrieval - Interpolated (bit 2)": [2],
                            "Water Pixel (bit 3)": [3]}

        for item, bits in aerosol_items_1b.items():
            binary = [0]*8
            if checked_items[item]:
                binary[(len(binary) - 1) - bits[0]] = 1
                values_combinations += list(binary_combination(binary, static_bits + bits))

        # generate the values combinations for two bits items selected
        aerosol_items_2b = {"Aerosol Content (bits 6-7)": [6, 7]}
        levels = {"Climatology content": [0, 0], "Low content": [0, 1],
                  "Average content": [1, 0], "High content": [1, 1]}

        for item, bits in aerosol_items_2b.items():
            if item in checked_items.keys():
                for level in checked_items[item]:
                    binary = [0]*8
                    binary[bits[0]:bits[1]+1] = (levels[level])[::-1]
                    binary.reverse()
                    values_combinations += list(binary_combination(binary, static_bits + bits))

        # add the specific values
        if specific_values:
            values_combinations += specific_values

        # delete duplicates
        values_combinations = list(set(values_combinations))

        # only left the values inside the image
        values_combinations = check_values_in_image(self.aerosol_for_process, values_combinations)

        filter_values = ",".join(["A=="+str(x) for x in values_combinations])
        not_filter_values = ",".join(["A!="+str(x) for x in values_combinations])

        ########################################
        # do QA Mask filter
        gdal_calc.Calc(calc="1*(numpy.all([{nfv}], axis=0)) + 8*(numpy.any([{fv}], axis=0))".format(fv=filter_values, nfv=not_filter_values),
                       A=self.aerosol_for_process, outfile=self.aerosol, type="Byte", NoDataValue=1)

        # unset nodata
        cmd = ['gdal_edit' if platform.system() == 'Windows' else 'gdal_edit.py',
               '"{}"'.format(self.aerosol), '-unsetnodata']
        call(" ".join(cmd), shell=True)

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

        ### ending process
        update_process_bar(self.process_bar, 100, self.process_status,
                           self.tr("DONE"))