def test_gdal_tile_untile(self):
        img = np.arange(0., 100.).reshape((10, 10))
        path = os.path.join(os.getcwd(), "test_gdal_retile.tif")
        tile_folder = os.path.join(os.getcwd(), "tiled")
        ImageIO.write_geotiff(img, path, self.projection, self.coordinates)
        # Add parasitic file - It should not cause problems
        path_parasite = os.path.join(tile_folder, "tile_01_01.tif")
        FileSystem.create_directory(tile_folder)
        ImageIO.write_geotiff(img, path_parasite, self.projection, self.coordinates)
        ds_in = GDalDatasetWrapper.from_file(path)
        self.assertTrue(os.path.exists(path))
        tiles = ImageTools.gdal_retile(ds_in, tile_folder,
                                       TileWidth=2,
                                       TileHeight=2,
                                       Overlap=1)
        self.assertTrue(os.path.isdir(tile_folder))
        self.assertEqual(len(tiles), 81)
        img_read = np.array(ImageIO.tiff_to_array(tiles[-1]))
        expected = np.array([[88, 89],
                             [98, 99]])
        # Some gdal_retile versions are producing the following image:
        # [[87, 89], [97, 99]].
        np.testing.assert_allclose(expected, img_read, atol=1)

        # Untile
        ds_untiled = ImageTools.gdal_buildvrt(*tiles)
        np.testing.assert_allclose(img, ds_untiled.array, atol=1)
        FileSystem.remove_file(path)
        FileSystem.remove_directory(tile_folder)
    def test_gdal_buildvrt_concatenate(self):
        from Common import FileSystem
        paths = []
        for i in range(1, 3, 1):
            img = np.ones((i, i, 2), np.int16) * i
            path = os.path.join(os.getcwd(), "test_gdal_merge_%s.tif" % i)
            ImageIO.write_geotiff(img, path, self.projection, self.coordinates)
            self.assertTrue(os.path.exists(path))
            paths.append(path)
        empty = os.path.join(os.getcwd(), "empty.vrt")
        driver = ImageTools.gdal_buildvrt(*paths, dst=empty,
                                          separate=True,
                                          srcnodata=0)
        expected = np.array([[[1, 0],
                              [0, 0]],
                             [[2, 2],
                              [2, 2]]], dtype=np.int16)

        np.testing.assert_almost_equal(driver.array, expected)
        self.assertEqual(driver.nodata_value, 0)
        self.assertEqual(driver.epsg, 32631)
        [FileSystem.remove_file(path) for path in paths]
        FileSystem.remove_file(empty)
        [self.assertFalse(os.path.exists(path)) for path in paths]
        self.assertFalse(os.path.exists(empty))
 def test_get_nodata(self):
     expected_nodata = 42.0
     img = np.ones((self.height, self.width), np.int16)
     path = os.path.join(os.getcwd(), "test_get_nodata_init.tif")
     ImageIO.write_geotiff(img, path, self.projection, self.coordinates)
     ds = ImageTools.gdal_buildvrt(path, VRTNodata=expected_nodata)
     self.assertEqual(expected_nodata, ds.nodata_value)
     np.testing.assert_almost_equal(ds.nodata_mask, np.ones_like(img))
     FileSystem.remove_file(path)
     self.assertFalse(os.path.exists(path))
    def test_gdal_buildvrt(self):
        path = os.path.join(os.getcwd(), "test_gdal_buildvrt.tif")
        vrt = os.path.join(os.getcwd(), "test_vrt.vrt")
        img = np.arange(-4, 5).reshape(3, 3) / 5

        ImageIO.write_geotiff(img, path, self.projection, self.coordinates)
        self.assertTrue(os.path.exists(path))
        driver = ImageTools.gdal_buildvrt(path, dst=vrt)
        self.assertTrue(os.path.exists(vrt))
        np.testing.assert_almost_equal(driver.array, img)
        FileSystem.remove_file(vrt)
        FileSystem.remove_file(path)
Example #5
0
    def prepare_water_data(self):
        """
        Prepare the water mask constituing of a set of gsw files.

        :return: Writes the tiles water_mask to the self.gsw_dst path.
        """
        occ_files = self.get_raw_water_data()
        vrt_path = os.path.join(self.wdir, "vrt_%s.vrt" % self.site.nom)
        ImageTools.gdal_buildvrt(*occ_files, dst=vrt_path)
        # Overlay occurrence image with same extent as the given site.
        # Should the occurrence files not be complete, this sets all areas not covered by the occurrence to 0.
        ds_warped = ImageTools.gdal_warp(vrt_path,
                                         r="near",
                                         te=self.site.te_str,
                                         t_srs=self.site.epsg_str,
                                         tr=self.site.tr_str,
                                         dstnodata=0,
                                         multi=True)
        # Threshold the final image and write to destination:
        image_bin = ds_warped.array > self.gsw_threshold
        FileSystem.remove_file(vrt_path)
        ImageIO.write_geotiff_existing(image_bin, self.gsw_dst,
                                       ds_warped.get_ds())
Example #6
0
    def prepare_mnt(self):
        """
        Prepare the srtm files.

        :return: Path to the full resolution DEM file.gsw
        :rtype: str
        """
        # Find/Download SRTM archives:
        srtm_archives = self.get_raw_data()
        # Unzip the downloaded/found srtm zip files:
        unzipped = []
        for arch in srtm_archives:
            basename = os.path.splitext(os.path.basename(arch))[0]
            FileSystem.unzip(arch, self.wdir)
            fn_unzipped = FileSystem.find_single(pattern=basename + ".tif",
                                                 path=self.wdir)
            unzipped.append(fn_unzipped)
        # Fusion of all SRTM files
        concat = ImageTools.gdal_buildvrt(*unzipped, vrtnodata=-32768)
        # Set nodata to 0
        nodata = ImageTools.gdal_warp(concat,
                                      srcnodata=-32768,
                                      dstnodata=0,
                                      multi=True)
        # Combine to image of fixed extent
        srtm_full_res = os.path.join(self.wdir,
                                     "srtm_%sm.tif" % int(self.site.res_x))
        ImageTools.gdal_warp(nodata,
                             dst=srtm_full_res,
                             r="cubic",
                             te=self.site.te_str,
                             t_srs=self.site.epsg_str,
                             tr=self.site.tr_str,
                             dstnodata=0,
                             srcnodata=0,
                             multi=True)
        return srtm_full_res