예제 #1
0
    def open_gdal_masks(self, shape_file, temp_dir):

        gdal_masks = {}

        for mask_key, mask_name in self.TheiaMaskDict.items():
            mask_file = [file for file in self.masks_folder.glob(mask_name)][0]
            gdal_masks.update({mask_key: gdal.Open(mask_file.as_posix())})

        if shape_file:

            opt = gdal.WarpOptions(cutlineDSName=shape_file,
                                   cropToCutline=True,
                                   srcNodata=-9999,
                                   dstNodata=-9999,
                                   outputType=gdal.GDT_Int16)

            for mask_key, mask_ds in gdal_masks.items():
                dest_name = (temp_dir / mask_key).as_posix()
                clipped_mask_ds = gdal.Warp(destNameOrDestDS=dest_name,
                                            srcDSOrSrcDSTab=mask_ds,
                                            options=opt)
                clipped_mask_ds.FlushCache()
                gdal_masks.update({mask_key: clipped_mask_ds})

        return gdal_masks
예제 #2
0
    def open_gdal_image(bands_list, desired_band):
        """
        Get the image in the list corresponding to the informed Band.
        Return the image opened with GDAL as a RasterImage object
        If cant find the band return None
        If is more than 1 image, raise exception
        """
        # todo: the desired band depends on the product
        desired_band = '_' + desired_band + '.'
        image_band = list(
            filter(lambda x: desired_band in os.path.split(x)[-1], bands_list))

        if len(image_band) == 0:
            raise OSError('Band not found.')

        elif len(image_band) > 1:
            raise OSError(
                'More than one band {} in image list'.format(desired_band))

        gdal_ds = gdal.Open(image_band[0].as_posix())

        if not gdal_ds:
            raise OSError("Couldn't open band file {}".format(image_band[0]))

        return gdal_ds
예제 #3
0
    def test_pekel(self, image, dw_image, pdf_merger_image):

        water_mask = dw_image.water_mask.copy()
        pekel_mask = gdal.Open(self.pekel).ReadAsArray(buf_xsize=image.x_size,
                                                       buf_ysize=image.y_size)

        # join invalid mask with pekels invalid
        invalid_mask = image.invalid_mask.astype('bool') | (pekel_mask == 255)

        pekel_mask = pekel_mask[~invalid_mask]
        water_mask = water_mask[~invalid_mask]

        # accuracy_score(pekel_mask > pekel_threshold, water_mask == 1)
        result = jaccard_score(pekel_mask > self.config.pekel_water, water_mask
                               == 1) * 100

        # save result to the pdf_merger
        pdf_name = os.path.join(self.saver.output_folder, 'pekel_test.pdf')

        # write the resulting text
        if result > self.config.pekel_accuracy:
            text = f'PEKEL TEST - OK \nAccuracy Threshold: {self.config.pekel_accuracy}%\nJaccard Index = {result:.3f}%'
            color = (0, 0, 0)
        else:
            text = f'*** PEKEL TEST FAILED *** \nAccuracy Threshold: {self.config.pekel_accuracy}%\n' \
                   f'Jaccard Index = {result:.3f}%'
            color = (220, 0, 0)

        pdf_merger_image.append(
            DWutils.write_pdf(pdf_name,
                              text,
                              size=(300, 100),
                              position=(50, 15),
                              font_color=color))
        return result
예제 #4
0
    def open_gdal_masks(self, shape_file, temp_dir):

        mask_file = [file
                     for file in self.masks_folder.glob('*pixel_qa.tif')][0]
        gdal_mask = gdal.Open(mask_file.as_posix())

        if shape_file:

            opt = gdal.WarpOptions(cutlineDSName=shape_file,
                                   cropToCutline=True,
                                   srcNodata=-9999,
                                   dstNodata=-9999,
                                   outputType=gdal.GDT_Int16)

            dest_name = (temp_dir / 'qa_cliped').as_posix()
            clipped_mask_ds = gdal.Warp(destNameOrDestDS=dest_name,
                                        srcDSOrSrcDSTab=gdal_mask,
                                        options=opt)
            clipped_mask_ds.FlushCache()
            gdal_mask = clipped_mask_ds

        return gdal_mask
예제 #5
0
    def read_gdal_ds(file, shape_file, temp_dir):
        """
        Read a GDAL dataset clipping it with a given shapefile, if necessary
        :param file: Filepath of the GDAL file (.tif, etc.) as Pathlib
        :param shape_file: file path of the shapefile
        :param temp_dir: file path of the temporary directory
        :return: GDAL dataset
        """
        gdal_mask = gdal.Open(file.as_posix())

        if gdal_mask and shape_file:

            opt = gdal.WarpOptions(cutlineDSName=shape_file, cropToCutline=True,
                                   srcNodata=-9999, dstNodata=-9999, outputType=gdal.GDT_Int16)

            dest_name = (temp_dir / (file.stem + '_clipped')).as_posix()
            clipped_mask_ds = gdal.Warp(destNameOrDestDS=dest_name,
                                        srcDSOrSrcDSTab=gdal_mask,
                                        options=opt)
            clipped_mask_ds.FlushCache()
            gdal_mask = clipped_mask_ds

        return gdal_mask