Пример #1
0
 def get_synthetic_band(self, synthetic_band, **kwargs):
     wdir = kwargs.get("wdir", self.fpath)
     output_folder = os.path.join(wdir, self.base)
     output_bname = "_".join(
         [self.base.split(".")[0],
          synthetic_band.upper() + ".tif"])
     output_filename = kwargs.get("output_filename",
                                  os.path.join(output_folder, output_bname))
     max_value = kwargs.get("max_value", 10000.)
     # Skip existing:
     if os.path.exists(output_filename):
         return output_filename
     if synthetic_band.lower() == "ndvi":
         FileSystem.create_directory(output_folder)
         b4 = self.find_file(pattern=r"*B0?4(_10m)?.jp2$")[0]
         b8 = self.find_file(pattern=r"*B0?8(_10m)?.jp2$")[0]
         ds_red = GDalDatasetWrapper.from_file(b4)
         ds_nir = GDalDatasetWrapper.from_file(b8)
         ds_ndvi = ImageApps.get_ndvi(ds_red,
                                      ds_nir,
                                      vrange=(0, max_value),
                                      dtype=np.int16)
         ds_ndvi.write(output_filename, options=["COMPRESS=DEFLATE"])
     elif synthetic_band.lower() == "ndsi":
         FileSystem.create_directory(output_folder)
         b3 = self.find_file(pattern=r"*B0?3(_10m)?.jp2$")[0]
         b11 = self.find_file(pattern=r"*B11(_20m)?.jp2$")[0]
         ds_green = ImageTools.gdal_translate(b3, tr="20 20", r="cubic")
         ds_swir = GDalDatasetWrapper.from_file(b11)
         ds_ndsi = ImageApps.get_ndsi(ds_green,
                                      ds_swir,
                                      vrange=(0, max_value),
                                      dtype=np.int16)
         ds_ndsi.write(output_filename, options=["COMPRESS=DEFLATE"])
     elif synthetic_band.lower() == "mca_sim":
         FileSystem.create_directory(output_folder)
         b4 = self.find_file(pattern=r"*B0?4(_10m)?.jp2$")[0]
         b3 = self.find_file(pattern=r"*B0?3(_10m)?.jp2$")[0]
         img_red, drv = ImageIO.tiff_to_array(b4, array_only=False)
         img_green = ImageIO.tiff_to_array(b3)
         img_mcasim = (img_red + img_green) / 2
         ImageIO.write_geotiff_existing(img_mcasim,
                                        output_filename,
                                        drv,
                                        options=["COMPRESS=DEFLATE"])
     else:
         raise ValueError("Unknown synthetic band %s" % synthetic_band)
     return output_filename
 def test_get_s2_epsg_code(self):
     epsg_ref = 32631
     projection = 'PROJCS["WGS 84 / UTM zone 31N",\
                     GEOGCS["WGS 84",DATUM["WGS_1984",\
                     SPHEROID["WGS 84",6378137,298.257223563,\
                     AUTHORITY["EPSG","7030"]],\
                     AUTHORITY["EPSG","6326"]],\
                     PRIMEM["Greenwich",0,\
                     AUTHORITY["EPSG","8901"]],\
                     UNIT["degree",0.0174532925199433,\
                     AUTHORITY["EPSG","9122"]],\
                     AUTHORITY["EPSG","4326"]],\
                     PROJECTION["Transverse_Mercator"],\
                     PARAMETER["latitude_of_origin",0],\
                     PARAMETER["central_meridian",3],\
                     PARAMETER["scale_factor",0.9996],\
                     PARAMETER["false_easting",500000],\
                     PARAMETER["false_northing",0],\
                     UNIT["metre",1,AUTHORITY["EPSG","9001"]],\
                     AXIS["Easting",EAST],AXIS["Northing",NORTH],\
                     AUTHORITY["EPSG","%s"]]' % epsg_ref
     img = np.ones((self.height, self.width), np.int16)
     path = os.path.join(os.getcwd(), "test_epsg.tif")
     ImageIO.write_geotiff(img, path, projection, self.coordinates)
     self.assertTrue(os.path.exists(path))
     ds = GDalDatasetWrapper.from_file(path)
     epsg_new = ds.epsg
     self.assertEqual(epsg_new, epsg_ref)
     FileSystem.remove_file(path)
     self.assertFalse(os.path.exists(path))
    def test_override_array(self):
        img = np.ones((self.height, self.width), np.int16)
        img_new = np.array(np.arange(0, 40200).reshape(201, 200),
                           dtype=np.uint8)
        path = os.path.join(os.getcwd(), "test_write_read_geotiff.tif")
        ImageIO.write_geotiff(img, path, self.projection, self.coordinates)
        self.assertTrue(os.path.exists(path))

        ds = GDalDatasetWrapper.from_file(path)
        self.assertTrue((ds.array == img).all())
        self.assertEqual(ds.geotransform, self.coordinates)
        self.assertEqual(ds.projection.replace(" ", ""),
                         self.projection.replace(" ", ""))
        self.assertIsNone(ds.nodata_value)
        self.assertEqual(ds.epsg, 32631)
        ds_new = GDalDatasetWrapper(ds=ds.get_ds(),
                                    array=img_new,
                                    nodata_value=123)
        FileSystem.remove_file(path)
        np.testing.assert_almost_equal(img_new, ds_new.array)
        self.assertEqual(ds_new.geotransform, self.coordinates)
        self.assertEqual(ds_new.projection.replace(" ", ""),
                         self.projection.replace(" ", ""))
        self.assertEqual(ds_new.nodata_value, 123)
        self.assertEqual(ds_new.epsg, 32631)
        self.assertFalse(os.path.exists(path))
    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)
Пример #5
0
 def from_raster(name, raster, **kwargs):
     """
     Create site from a raster on disk
     :param name: The name of the site
     :param raster: The gdal raster
     Optional arguments:
     - shape_index_y: Select the band index for the Y-size
     - shape_index_x: Select the band index for the X-size
     :return: A site class given the infos from the raster.
     """
     driver = GDalDatasetWrapper.from_file(raster)
     shape_index_y = kwargs.get("shape_index_y", 0)
     shape_index_x = kwargs.get("shape_index_x", 1)
     ny, nx = driver.array.shape[shape_index_y], driver.array.shape[
         shape_index_x]
     epsg = driver.epsg
     ulx, uly, lrx, lry = driver.ul_lr
     ul = (ulx, uly)
     lr = (lrx, lry)
     xmin, xres, skx, ymax, sky, yres = driver.geotransform
     return Site(name,
                 epsg,
                 ul=ul,
                 lr=lr,
                 px=nx,
                 py=ny,
                 res_x=xres,
                 res_y=yres)
 def test_get_utm_description(self):
     img = np.ones((self.height, self.width), np.int16)
     path = os.path.join(os.getcwd(), "test_get_utm_description.tif")
     ImageIO.write_geotiff(img, path, self.projection, self.coordinates)
     self.assertTrue(os.path.exists(path))
     ds = GDalDatasetWrapper.from_file(path)
     utm = ds.utm_description
     utm_expected = "WGS 84 / UTM zone 31N"
     self.assertEqual(utm_expected, utm)
     FileSystem.remove_file(path)
     self.assertFalse(os.path.exists(path))
    def test_get_resolution(self):
        img = np.ones((self.height, self.width), np.int16)
        path = os.path.join(os.getcwd(), "test_get_resolution.tif")
        ImageIO.write_geotiff(img, path, self.projection, self.coordinates)
        self.assertTrue(os.path.exists(path))

        ds = GDalDatasetWrapper.from_file(path)
        res_expected = (self.coordinates[1], self.coordinates[-1])
        self.assertEqual(res_expected, ds.resolution)

        FileSystem.remove_file(path)
        self.assertFalse(os.path.exists(path))
    def test_get_ul_lr(self):
        img = np.ones((1000, 1000), np.int16)
        path = os.path.join(os.getcwd(), "test_get_ul_lr.tif")
        ImageIO.write_geotiff(img, path, self.projection, self.coordinates)
        self.assertTrue(os.path.exists(path))

        ds = GDalDatasetWrapper.from_file(path)
        ulx, uly, lrx, lry = ds.ul_lr
        self.assertEqual((ulx, uly), (300000.0, 4900020.0))
        self.assertEqual((lrx, lry), (310000.0, 4890020.0))
        self.assertEqual(ds.extent, (300000.0, 4890020.0, 310000.0, 4900020.0))
        FileSystem.remove_file(path)
        self.assertFalse(os.path.exists(path))
    def test_write_read_geotiff(self):
        img = np.ones((self.height, self.width), np.int16)
        path = os.path.join(os.getcwd(), "test_write_read_geotiff.tif")
        ImageIO.write_geotiff(img, path, self.projection, self.coordinates)
        self.assertTrue(os.path.exists(path))

        ds = GDalDatasetWrapper.from_file(path)
        self.assertTrue((ds.array == img).all())
        self.assertEqual(ds.geotransform, self.coordinates)
        # Compare projections by removing all spaces cause of multiline string
        self.assertEqual(ds.projection.replace(" ", ""),
                         self.projection.replace(" ", ""))
        self.assertIsNone(ds.nodata_value)
        self.assertEqual(ds.epsg, 32631)
        FileSystem.remove_file(path)
        self.assertFalse(os.path.exists(path))
 def test_write_nodata(self):
     fname = "./test_write_nodata.tif"
     img = np.ones((self.height, self.width), np.int16)
     ds = GDalDatasetWrapper(array=img,
                             projection=self.projection,
                             geotransform=self.coordinates,
                             nodata_value=42)
     ds.write(fname, options=["COMPRESS=DEFLATE"])
     self.assertTrue(os.path.exists(fname))
     ds_read = GDalDatasetWrapper.from_file(fname)
     self.assertEqual(42, ds_read.nodata_value)
     self.assertEqual(
         "DEFLATE",
         ds_read._info["metadata"]["IMAGE_STRUCTURE"]["COMPRESSION"])
     np.testing.assert_almost_equal(ds_read.array, ds.array)
     os.remove(fname)
     self.assertFalse(os.path.exists(fname))
Пример #11
0
 def reproject(self, **kwargs):
     out_dir = kwargs.get("out_dir", self.fpath)
     assert os.path.isdir(out_dir)
     out_dir = os.path.join(out_dir, self.base)
     FileSystem.create_directory(out_dir)
     patterns = kwargs.get("patterns", [r".(tif|jp2)$"])
     imgs = [self.find_file(pattern=p) for p in patterns]
     epsg = kwargs.get("epsg", None)
     # Flatten
     imgs = [i for img in imgs for i in img]
     for img in imgs:
         if not epsg:
             drv = GDalDatasetWrapper.from_file(img)
             epsg = drv.epsg
         outpath = os.path.join(out_dir, os.path.basename(img))
         self._reproject_to_epsg(img, outpath, epsg)
     return out_dir
    def test_merge_then_translate(self):
        datasets = []
        init = np.zeros((2, 2), np.int16)
        path = os.path.join(os.getcwd(), "test_gdal_merge.tif")
        ImageIO.write_geotiff(init, path, self.projection, self.coordinates)
        ds_in = GDalDatasetWrapper.from_file(path)
        for i in range(1, 3, 1):
            img = np.ones((i*2, i*2), np.int16) * i
            ds_n = GDalDatasetWrapper(ds=ds_in.get_ds(), array=img)
            self.assertTrue(os.path.exists(path))
            datasets.append(ds_n)
        ds_merged = ImageTools.gdal_merge(*datasets, dst="out.tif",
                                          separate=True,
                                          q=True,
                                          a_nodata=0)
        # Array of shape (4, 4, 2):
        expected = np.array([[[1, 2],
                              [1, 2],
                              [0, 2],
                              [0, 2]],
                             [[1, 2],
                              [1, 2],
                              [0, 2],
                              [0, 2]],
                             [[0, 2],
                              [0, 2],
                              [0, 2],
                              [0, 2]],
                             [[0, 2],
                              [0, 2],
                              [0, 2],
                              [0, 2]]], dtype=np.int16)
        FileSystem.remove_file("out.tif")
        np.testing.assert_equal(expected.dtype, ds_merged.array.dtype)
        np.testing.assert_almost_equal(expected, ds_merged.array)
        self.assertEqual(ds_merged.nodata_value, 0)
        self.assertEqual(ds_merged.epsg, 32631)
        ds_translate = ImageTools.gdal_translate(ds_merged,
                                                 a_nodata=0)
        FileSystem.remove_file(path)

        np.testing.assert_equal(expected.dtype, ds_translate.array.dtype)
        np.testing.assert_almost_equal(ds_translate.array, expected)
        self.assertEqual(ds_translate.nodata_value, 0)
        self.assertEqual(ds_translate.epsg, 32631)
Пример #13
0
 def __init__(self, site, dem_full_res):
     self.name = site.nom
     self.epsg = site.epsg_str
     self.ulx = site.ul[0]
     self.uly = site.ul[1]
     self.resx = site.res_x
     self.resy = site.res_y
     self.lx = site.px
     self.ly = site.py
     self.alt = dem_full_res
     driver = GDalDatasetWrapper.from_file(dem_full_res)
     self.mean_alt = np.mean(driver.array)
     self.std_dev_alt = np.std(driver.array)
     self.short_description = driver.utm_description
     self.nodata = driver.nodata_value
     res_arr = driver.resolution
     self.dem_subsampling_ratio = str(
         int(float(res_arr[0]) / float(site.res_x)))
    def test_gdal_merge_optfile(self):
        datasets, written = [], []
        init = np.zeros((2, 2), np.int16)
        path = os.path.join(os.getcwd(), "test_gdal_merge_optfile.tif")
        ImageIO.write_geotiff(init, path, self.projection, self.coordinates)
        ds_in = GDalDatasetWrapper.from_file(path)
        for i in range(1, 3, 1):
            img = np.ones((i*2, i*2), np.int16) * i
            ds_n = GDalDatasetWrapper(ds=ds_in.get_ds(), array=img)
            p_out = "test_gdal_merge_optfile_%s.tif" % i
            ImageIO.write_geotiff_existing(img, p_out, ds_in.get_ds())
            self.assertTrue(os.path.exists(path))
            datasets.append(ds_n)
            written.append(p_out)
        optfile = "test_gdal_merge_optfile.txt"
        with open(optfile, 'w') as file_handler:
            for item in written:
                file_handler.write("{}\n".format(item))

        ds_merged = ImageTools.gdal_merge(*datasets,
                                          q=True,
                                          a_nodata=0)
        ds_optfile = ImageTools.gdal_merge(optfile=optfile,
                                           q=True,
                                           a_nodata=0)
        # Array of shape (4, 4):
        expected = np.array([[2, 2, 2, 2],
                             [2, 2, 2, 2],
                             [2, 2, 2, 2],
                             [2, 2, 2, 2]], dtype=np.int16)

        FileSystem.remove_file(path)
        [FileSystem.remove_file(p) for p in written]
        FileSystem.remove_file(optfile)
        np.testing.assert_equal(expected.dtype, ds_merged.array.dtype)
        np.testing.assert_almost_equal(expected, ds_merged.array)
        self.assertEqual(ds_merged.nodata_value, 0)
        self.assertEqual(ds_merged.epsg, 32631)
        np.testing.assert_equal(expected.dtype, ds_optfile.array.dtype)
        np.testing.assert_almost_equal(expected, ds_optfile.array)
        self.assertEqual(ds_optfile.nodata_value, 0)
        self.assertEqual(ds_optfile.epsg, 32631)
Пример #15
0
 def test_srtm_prepare_mnt_vns_maccanw2(self):
     resx, resy = 5000, -5000
     px, py = 11, 14
     site = SiteInfo.Site("MACCANW2",
                          32633,
                          px=px,
                          py=py,
                          ul=(246439.500, 4672656.500),
                          lr=(299769.500, 4604231.500),
                          res_x=resx,
                          res_y=resy)
     dem_dir = os.path.join(os.getcwd(),
                            "test_srtm_prepare_mnt_vns_maccanw2")
     s = SRTM.SRTM(site,
                   dem_dir=dem_dir,
                   raw_dem=self.raw_srtm,
                   raw_gsw=self.raw_gsw,
                   wdir=dem_dir)
     self.assertTrue(os.path.isdir(dem_dir))
     srtm = s.prepare_mnt()
     self.assertTrue(os.path.isfile(srtm))
     driver = GDalDatasetWrapper.from_file(srtm)
     expected_img = [[
         293, 210, 337, 390, 238, 218, 237, 245, 270, 208, 132
     ], [302, 293, 277, 302, 182, 172, 237, 270, 262, 171, 60],
                     [155, 239, 239, 231, 238, 199, 164, 173, 137, 85, 33],
                     [0, 28, 94, 138, 150, 131, 116, 117, 72, 37, 73],
                     [0, 0, 18, 67, 81, 79, 84, 117, 72, 36, 59],
                     [0, 0, 0, 11, 38, 58, 68, 87, 68, 38, 33],
                     [0, 0, 0, 0, 6, 41, 52, 62, 52, 43, 41],
                     [0, 0, 0, 0, 1, 0, 30, 34, 30, 48, 68],
                     [0, 0, 0, 0, 0, -1, 12, 26, 42, 70, 101],
                     [0, 0, 0, 0, 0, 4, 11, 50, 59, 89, 136],
                     [0, 0, 0, 0, 0, 0, 0, 30, 76, 97, 130],
                     [0, 0, 0, 0, 0, 0, 0, 0, 44, 72, 86],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 59],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 54]]
     self.assertEqual(driver.resolution, (resx, resy))
     self.assertEqual(driver.array.shape, (site.py, site.px))
     self.assertEqual(driver.nodata_value, 0)
     np.testing.assert_allclose(expected_img, driver.array, atol=1.5)
     FileSystem.remove_directory(dem_dir)
Пример #16
0
 def test_get_water_data_tls_l8(self):
     resx, resy = 15000, -15000
     px, py = 15, 14
     site = SiteInfo.Site("19080",
                          32631,
                          px=px,
                          py=py,
                          ul=(285431.584, 4884950.507),
                          lr=(510124.885, 4680403.373),
                          res_x=resx,
                          res_y=resy)
     dem_dir = os.path.join(os.getcwd(), "test_get_water_data_tls_l8")
     mnt = MNTBase.MNT(site,
                       dem_dir=dem_dir,
                       raw_dem=self.raw_dem,
                       raw_gsw=self.raw_gsw)
     water_mask = mnt.gsw_dst
     self.assertTrue(os.path.exists(dem_dir))
     mnt.prepare_water_data()
     self.assertTrue(os.path.exists(water_mask))
     driver = GDalDatasetWrapper.from_file(water_mask)
     img_expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
     self.assertEqual(driver.resolution, (resx, resy))
     self.assertEqual(driver.epsg, 32631)
     self.assertEqual(driver.array.shape, (py, px))
     self.assertIsNone(driver.nodata_value)
     np.testing.assert_almost_equal(img_expected, driver.array)
     FileSystem.remove_directory(dem_dir)
Пример #17
0
 def test_get_water_data_maccanw2_vns(self):
     resx, resy = 5000, -5000
     px, py = 11, 14
     site = SiteInfo.Site("MACCANW2",
                          32633,
                          px=px,
                          py=py,
                          ul=(246439.500, 4672656.500),
                          lr=(299769.500, 4604231.500),
                          res_x=resx,
                          res_y=resy)
     dem_dir = os.path.join(os.getcwd(), "test_get_water_data_maccanw2_vns")
     mnt = MNTBase.MNT(site,
                       dem_dir=dem_dir,
                       raw_dem=self.raw_dem,
                       raw_gsw=self.raw_gsw)
     water_mask = mnt.gsw_dst
     self.assertTrue(os.path.exists(dem_dir))
     mnt.prepare_water_data()
     self.assertTrue(os.path.exists(water_mask))
     driver = GDalDatasetWrapper.from_file(water_mask)
     img_expected = [[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                     [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                     [1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                     [1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                     [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
                     [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
                     [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
                     [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
                     [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
                     [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]]
     self.assertEqual(driver.resolution, (resx, resy))
     self.assertEqual(driver.epsg, 32633)
     self.assertEqual(driver.array.shape, (py, px))
     np.testing.assert_almost_equal(img_expected, driver.array)
     self.assertIsNone(driver.nodata_value)
     FileSystem.remove_directory(dem_dir)
Пример #18
0
 def test_get_water_data_spain_s2(self):
     """
     Download the given gsw file and project it to a 10km x 10km resolution (11x11 image)
     """
     resx, resy = 10000, -10000
     px, py = 11, 11
     site = SiteInfo.Site("T30SYJ",
                          32630,
                          px=px,
                          py=py,
                          ul=(699960.000, 4400040.000),
                          lr=(809760.000, 4290240.000),
                          res_x=resx,
                          res_y=resy)
     dem_dir = os.path.join(os.getcwd(), "test_get_water_data_spain_s2")
     mnt = MNTBase.MNT(site,
                       dem_dir=dem_dir,
                       raw_dem=self.raw_dem,
                       raw_gsw=self.raw_gsw)
     water_mask = mnt.gsw_dst
     self.assertTrue(os.path.exists(dem_dir))
     mnt.prepare_water_data()
     self.assertTrue(os.path.exists(water_mask))
     driver = GDalDatasetWrapper.from_file(water_mask)
     img_expected = [[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
                     [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
                     [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
                     [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
                     [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
                     [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
                     [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
                     [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
                     [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
                     [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
                     [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1]]
     self.assertEqual(driver.resolution, (resx, resy))
     self.assertEqual(driver.epsg, 32630)
     self.assertEqual(driver.array.shape, (py, px))
     np.testing.assert_almost_equal(img_expected, driver.array)
     self.assertIsNone(driver.nodata_value)
     FileSystem.remove_directory(dem_dir)
Пример #19
0
 def test_srtm_prepare_mnt_s2_tls(self):
     resx, resy = 10000, -10000
     site = SiteInfo.Site("T31TCJ",
                          32631,
                          px=11,
                          py=11,
                          ul=(300000.000, 4900020.000),
                          lr=(409800.000, 4790220.000),
                          res_x=resx,
                          res_y=resy)
     dem_dir = os.path.join(os.getcwd(), "test_srtm_prepare_mnt_s2_tls")
     s = SRTM.SRTM(site,
                   dem_dir=dem_dir,
                   raw_dem=self.raw_srtm,
                   raw_gsw=self.raw_gsw,
                   wdir=dem_dir)
     self.assertTrue(os.path.isdir(dem_dir))
     srtm = s.prepare_mnt()
     self.assertTrue(os.path.isfile(srtm))
     driver = GDalDatasetWrapper.from_file(srtm)
     expected_img = [
         [85, 85, 115, 142, 138, 147, 151, 163, 162, 258, 282],
         [136, 98, 96, 90, 101, 91, 120, 118, 121, 243, 302],
         [137, 133, 135, 137, 101, 89, 92, 131, 162, 246, 294],
         [139, 144, 170, 184, 157, 115, 108, 132, 176, 210, 212],
         [148, 140, 161, 162, 206, 138, 133, 134, 150, 164, 160],
         [169, 173, 168, 171, 222, 168, 128, 158, 162, 151, 164],
         [193, 199, 181, 186, 186, 193, 158, 147, 189, 204, 199],
         [209, 223, 207, 195, 215, 215, 166, 161, 187, 214, 228],
         [240, 247, 225, 206, 268, 211, 172, 203, 195, 213, 242],
         [281, 268, 246, 272, 282, 216, 216, 208, 231, 211, 220],
         [335, 302, 319, 311, 265, 234, 262, 251, 236, 259, 250]
     ]
     self.assertEqual(driver.resolution, (resx, resy))
     self.assertEqual(driver.array.shape, (site.py, site.px))
     self.assertEqual(driver.nodata_value, 0)
     np.testing.assert_allclose(expected_img, driver.array, atol=1.5)
     FileSystem.remove_directory(dem_dir)
Пример #20
0
 def test_eudem_prepare_mnt_s2_tls(self):
     resx, resy = 10000, -10000
     site = SiteInfo.Site("T31TCJ",
                          32631,
                          ul=(300000.000, 4900020.000),
                          lr=(409800.000, 4790220.000),
                          px=11,
                          py=11,
                          res_x=resx,
                          res_y=resy)
     dem_dir = os.path.join(os.getcwd(), "test_eudem_prepare_mnt_s2_31ucr")
     s = EuDEM.EuDEM(site,
                     dem_dir=dem_dir,
                     raw_dem=self.raw_eudem,
                     raw_gsw=self.raw_gsw,
                     wdir=dem_dir)
     self.assertTrue(os.path.isdir(dem_dir))
     srtm = s.prepare_mnt()
     self.assertTrue(os.path.isfile(srtm))
     driver = GDalDatasetWrapper.from_file(srtm)
     expected_img = [[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                     [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]
     self.assertEqual(driver.resolution, (resx, resy))
     self.assertEqual(driver.array.shape, (site.py, site.px))
     self.assertEqual(driver.nodata_value, 0)
     np.testing.assert_allclose(expected_img, driver.array)
     FileSystem.remove_directory(dem_dir)
Пример #21
0
    def test_srtm_prepare_mnt_l8_tls(self):
        resx, resy = 15000, -15000
        px, py = 15, 14
        site = SiteInfo.Site("19080",
                             32631,
                             px=px,
                             py=py,
                             ul=(285431.584, 4884950.507),
                             lr=(510124.885, 4680403.373),
                             res_x=resx,
                             res_y=resy)
        dem_dir = os.path.join(os.getcwd(), "test_srtm_prepare_mnt_l8_tls")
        s = SRTM.SRTM(site,
                      dem_dir=dem_dir,
                      raw_dem=self.raw_srtm,
                      raw_gsw=self.raw_gsw,
                      wdir=dem_dir)
        self.assertTrue(os.path.isdir(dem_dir))
        srtm = s.prepare_mnt()
        self.assertTrue(os.path.isfile(srtm))
        driver = GDalDatasetWrapper.from_file(srtm)
        expected_img = np.array([[
            126, 134, 125, 114, 88, 104, 144, 265, 266, 283, 388, 487, 592,
            571, 617
        ],
                                 [
                                     144, 139, 161, 177, 134, 117, 161, 198,
                                     189, 205, 328, 487, 457, 444, 634
                                 ],
                                 [
                                     166, 163, 164, 189, 177, 137, 156, 153,
                                     198, 231, 384, 619, 698, 707, 638
                                 ],
                                 [
                                     194, 201, 194, 188, 198, 154, 178, 205,
                                     218, 196, 303, 634, 898, 925, 586
                                 ],
                                 [
                                     216, 235, 223, 223, 226, 175, 194, 220,
                                     234, 318, 462, 637, 752, 547, 275
                                 ],
                                 [
                                     273, 287, 268, 283, 234, 222, 226, 227,
                                     215, 423, 581, 548, 404, 246, 86
                                 ],
                                 [
                                     429, 383, 352, 315, 277, 283, 265, 274,
                                     269, 170, 148, 120, 72, 43, 27
                                 ],
                                 [
                                     598, 472, 402, 387, 427, 379, 362, 328,
                                     365, 291, 270, 328, 186, 114, 42
                                 ],
                                 [
                                     1151, 897, 977, 763, 647, 814, 708, 596,
                                     514, 458, 458, 576, 383, 231, 0
                                 ],
                                 [
                                     1591, 1303, 1697, 1728, 1229, 1342, 1085,
                                     1173, 1137, 829, 685, 513, 289, 158, 27
                                 ],
                                 [
                                     2261, 2075, 1964, 2052, 1916, 2087, 1923,
                                     1634, 1741, 1576, 1185, 490, 198, 36, -24
                                 ],
                                 [
                                     1671, 1898, 2127, 2148, 1475, 1992, 2126,
                                     2301, 2098, 1749, 1235, 1060, 448, 135,
                                     159
                                 ],
                                 [
                                     1264, 1283, 1496, 1520, 1529, 1398, 1684,
                                     1735, 1472, 2044, 1949, 1344, 684, 374,
                                     302
                                 ],
                                 [
                                     871, 980, 988, 1052, 1401, 1051, 1526,
                                     1479, 1487, 1457, 1253, 753, 567, 179, 32
                                 ]])
        self.assertEqual(driver.resolution, (resx, resy))
        self.assertEqual(driver.array.shape, (site.py, site.px))
        self.assertEqual(driver.nodata_value, 0)

        np.testing.assert_allclose(expected_img, driver.array, atol=1.5)
        FileSystem.remove_directory(dem_dir)