Example #1
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
 def test_gdal_warp(self):
     img = np.ones((self.height, self.width, 2), np.int16)
     img_rescaled = np.ones((int(self.height/2), int(self.width/2), 2), np.int16)
     out_resolution = (20, -20)
     path = os.path.join(os.getcwd(), "test_gdal_warp.tif")
     scaled = os.path.join(os.getcwd(), "res_changed.tif")
     ImageIO.write_geotiff(img, path, self.projection, self.coordinates)
     self.assertTrue(os.path.exists(path))
     ds = ImageTools.gdal_warp(path, scaled,
                               tr=" ".join(str(e) for e in out_resolution),
                               r="max", q=True)
     self.assertTrue(os.path.isfile(scaled))
     self.assertEqual(ds.resolution, out_resolution)
     self.assertEqual(ds.array.shape, (self.height // 2, self.width // 2, 2))
     np.testing.assert_almost_equal(ds.array, img_rescaled)
     FileSystem.remove_file(path)
     FileSystem.remove_file(scaled)
Example #3
0
    def prepare_mnt(self):
        """
        Prepare the eudem files.

        :return: Path to the full resolution DEM file.gsw
        :rtype: str
        """
        # Find/Download EuDEM archives:
        eudem_files = self.get_raw_data()
        # Unzip the downloaded/found EuDEM zip files:
        unzipped = []
        for arch in eudem_files:
            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 EuDEM files
        ds_cropped = []
        for fn in unzipped:
            ds = ImageTools.gdal_warp(fn,
                                      of="GTiff",
                                      ot="Int16",
                                      r="cubic",
                                      te=self.site.te_str,
                                      t_srs=self.site.epsg_str,
                                      tr=self.site.tr_str,
                                      multi=True)
            ds.array[ds.array < self.lowest_allowed_height] = -32767
            ds_cropped.append(ds)
        eudem_full_res = os.path.join(self.wdir,
                                      "eudem_%sm.tif" % int(self.site.res_x))
        ImageTools.gdal_merge(*ds_cropped,
                              dst=eudem_full_res,
                              n=-32767,
                              a_nodata=0,
                              q=True)
        return eudem_full_res
Example #4
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 #5
0
 def _reproject_to_epsg(self, img, outpath, epsg):
     tmpfile = tempfile.mktemp(prefix="reproject_", suffix=".tif")
     ImageTools.gdal_warp(tmpfile, img, t_srs="EPSG:%s" % epsg,
                          tr=" ".join(map(str, self.base_resolution)),
                          q=True)
     shutil.move(tmpfile, outpath)
Example #6
0
    def to_maja_format(self,
                       platform_id,
                       mission_field,
                       mnt_resolutions,
                       coarse_res,
                       full_res_only=False):
        """
        Writes an MNT in Maja (=EarthExplorer) format: A folder .DBL.DIR containing the rasters and an
        accompanying .HDR xml-file.
        The two files follow the maja syntax::
            *AUX_REFDE2*.(HDR|DBL.DIR)
        :param platform_id: The platform ID of two digits (e.g. S2_ for Sentinel2A/B; VS for Venus)
        :param mission_field: Similar to the platform ID, this is used in the <Mission>-field for the HDR file.
                              e.g. SENTINEL-2 for S2
        :param mnt_resolutions: A dict containing the resolutions for the given sensor. E.g.::
            {"XS": (10, -10)}
        :param coarse_res: A tuple of int describing the coarse resolution. E.g.::
            (240, -240).
        :param full_res_only:  If True, no coarse_res rasters will be created.
        :return: Writes the .DBL.DIR and .HDR into the specified self.dem_dir
        """
        assert len(mnt_resolutions) >= 1
        basename = str(
            "%s_TEST_AUX_REFDE2_%s_%s" %
            (platform_id, self.site.nom, str(self.dem_version).zfill(4)))

        # Get mnt data
        mnt_max_res = self.prepare_mnt()

        # Water mask not needed with optional coarse_res writing:
        if coarse_res and not full_res_only:
            # Get water data
            self.prepare_water_data()
        mnt_res = (self.site.res_x, self.site.res_y)
        dbl_base = basename + ".DBL.DIR"
        dbl_dir = os.path.join(self.dem_dir, dbl_base)
        FileSystem.create_directory(dbl_dir)
        hdr = os.path.join(self.dem_dir, basename + ".HDR")

        # Calulate gradient mask at MNT resolution:
        mnt_in, drv = ImageIO.tiff_to_array(mnt_max_res, array_only=False)
        grad_y_mnt, grad_x_mnt = self.calc_gradient(mnt_in, self.site.res_x,
                                                    self.site.res_y)

        full_res = (int(mnt_resolutions[0]["val"].split(" ")[0]),
                    int(mnt_resolutions[0]["val"].split(" ")[1]))

        grad_x = self.resample_to_full_resolution(grad_x_mnt,
                                                  mnt_resolution=mnt_res,
                                                  full_resolution=full_res,
                                                  order=3)
        grad_y = self.resample_to_full_resolution(grad_y_mnt,
                                                  mnt_resolution=mnt_res,
                                                  full_resolution=full_res,
                                                  order=3)

        slope, aspect = self.calc_slope_aspect(grad_y, grad_x)

        # Write full res slope and aspect
        geotransform = list(drv.GetGeoTransform())
        geotransform[1] = float(full_res[0])
        geotransform[-1] = float(full_res[1])
        projection = drv.GetProjection()
        tmp_asp = tempfile.mktemp(dir=self.wdir, suffix="_asp.tif")
        ImageIO.write_geotiff(aspect, tmp_asp, projection, tuple(geotransform))
        tmp_slp = tempfile.mktemp(dir=self.wdir, suffix="_slp.tif")
        ImageIO.write_geotiff(slope, tmp_slp, projection, tuple(geotransform))

        # Full resolution:
        write_resolution_name = True if len(mnt_resolutions) > 1 else False
        # Names for R1, R2 etc.
        rasters_written = []
        path_alt, path_asp, path_slp = "", "", ""
        all_paths_alt = []
        for res in mnt_resolutions:
            # ALT:
            bname_alt = basename + "_ALT"
            bname_alt += "_" + str(
                res["name"]) if write_resolution_name else ""
            bname_alt += ".TIF"
            rel_alt = os.path.join(dbl_base, bname_alt)
            path_alt = os.path.join(self.dem_dir, rel_alt)
            all_paths_alt.append(path_alt)
            ImageTools.gdal_warp(mnt_max_res,
                                 dst=path_alt,
                                 tr=res["val"],
                                 r="cubic",
                                 multi=True)
            rasters_written.append(rel_alt)
            # ASP:
            bname_asp = basename + "_ASP"
            bname_asp += "_" + res["name"] if write_resolution_name else ""
            bname_asp += ".TIF"
            rel_asp = os.path.join(dbl_base, bname_asp)
            path_asp = os.path.join(self.dem_dir, rel_asp)
            ImageTools.gdal_warp(tmp_asp,
                                 dst=path_asp,
                                 tr=res["val"],
                                 r="cubic",
                                 multi=True)
            rasters_written.append(rel_asp)
            # SLP:
            bname_slp = basename + "_SLP"
            bname_slp += "_" + res["name"] if write_resolution_name else ""
            bname_slp += ".TIF"
            rel_slp = os.path.join(dbl_base, bname_slp)
            path_slp = os.path.join(self.dem_dir, rel_slp)
            ImageTools.gdal_warp(tmp_slp,
                                 dst=path_slp,
                                 tr=res["val"],
                                 r="cubic",
                                 multi=True)
            rasters_written.append(rel_slp)

        # Optional coarse_res writing:
        if coarse_res and not full_res_only:
            # Resize all rasters for coarse res.
            coarse_res_str = str(coarse_res[0]) + " " + str(coarse_res[1])
            # ALC:
            bname_alc = basename + "_ALC.TIF"
            rel_alc = os.path.join(dbl_base, bname_alc)
            path_alc = os.path.join(self.dem_dir, rel_alc)
            ImageTools.gdal_warp(path_alt,
                                 dst=path_alc,
                                 tr=coarse_res_str,
                                 multi=True)
            rasters_written.append(rel_alc)
            # ALC:
            bname_asc = basename + "_ASC.TIF"
            rel_asc = os.path.join(dbl_base, bname_asc)
            path_asc = os.path.join(self.dem_dir, rel_asc)
            ImageTools.gdal_warp(path_asp,
                                 dst=path_asc,
                                 tr=coarse_res_str,
                                 multi=True)
            rasters_written.append(rel_asc)
            # ALC:
            bname_slc = basename + "_SLC.TIF"
            rel_slc = os.path.join(dbl_base, bname_slc)
            path_slc = os.path.join(self.dem_dir, rel_slc)
            ImageTools.gdal_warp(path_slp,
                                 dst=path_slc,
                                 tr=coarse_res_str,
                                 multi=True)
            rasters_written.append(rel_slc)
            # Water mask:
            bname_msk = basename + "_MSK.TIF"
            rel_msk = os.path.join(dbl_base, bname_msk)
            path_msk = os.path.join(self.dem_dir, rel_msk)
            ImageTools.gdal_warp(self.gsw_dst,
                                 dst=path_msk,
                                 tr=coarse_res_str,
                                 multi=True)
            rasters_written.append(rel_msk)

        # Write HDR Metadata:

        date_start = datetime(1970, 1, 1)
        date_end = datetime(2100, 1, 1)
        dem_info = DEMInfo(self.site, all_paths_alt[0])
        root = self._get_root()
        self._create_hdr(root, mission_field, basename, rasters_written,
                         dem_info, date_start, date_end, self.dem_version)
        XMLTools.write_xml(root, hdr)

        # Remove temp files:
        FileSystem.remove_file(tmp_asp)
        FileSystem.remove_file(tmp_slp)
        FileSystem.remove_file(mnt_max_res)
        return hdr, dbl_dir