accessmod/srtm.py
def process(
    extent: str,
    epsg: int,
    resolution: float,
    input_dir: str,
    output_dir: str,
    overwrite: bool,
):
    """Process SRTM tiles.

    Tiles are merged into a single mosaic, reprojected and masked according to
    the area of interest. In addition, a slope raster (in degrees) is computed.
    Raster grid is created from country, epsg and spatial resolution parameters.
    """
    dst_dem = os.path.join(output_dir, "dem.tif")
    dst_slope = os.path.join(output_dir, "slope.tif")
    fs = utils.filesystem(dst_dem)

    fs.makedirs(output_dir, exist_ok=True)

    # get raster metadata from country boundaries, spatial resolution and EPSG
    geom = utils.parse_extent(extent)
    dst_crs = CRS.from_epsg(int(epsg))
    _, shape, bounds = processing.create_grid(
        geom=geom, dst_crs=dst_crs, dst_res=resolution
    )

    for fp, label in zip((dst_dem, dst_slope), ("DEM", "Slope")):
        if fs.isfile(fp):
            if overwrite:
                fs.rm(fp)
                logger.info(f"Deleted old {label} file at {fp}.")
            else:
                raise FileExistsError(f"{label} already exists at {fp}.")

    with tempfile.TemporaryDirectory(prefix="AccessMod_") as tmp_dir:
        tiles = utils.unzip_all(input_dir, tmp_dir)
        if len(tiles) == 0:
            return FileNotFoundError(f"No SRTM tile found at {input_dir}.")

        mosaic = merge_tiles(
            tiles, os.path.join(tmp_dir, "mosaic.tif"), overwrite=overwrite
        )

        # slope is computed from DEM before reprojection and masking
        slope = compute_slope(
            mosaic, os.path.join(tmp_dir, "slope.tif"), overwrite=overwrite
        )

        for src_fp, dst_fp in zip((mosaic, slope), (dst_dem, dst_slope)):

            tmp_fp = src_fp.replace(".tif", "_reproj.tif")
            tmp_fp = processing.reproject(
                src_fp,
                tmp_fp,
                dst_crs=dst_crs,
                dtype="int16",
                bounds=bounds,
                height=shape[0],
                width=shape[1],
                resampling_alg="bilinear",
            )
            processing.mask(tmp_fp, dst_fp, geom)
Process SRTM tiles.

    Tiles are merged into a single mosaic, reprojected and masked according to
    the area of interest. In addition, a slope raster (in degrees) is computed.
    Raster grid is created from country, epsg and spatial resolution parameters.
    
Process SRTM tiles.
Tiles are merged into a single mosaic, reprojected and masked according to
the area of interest. In addition, a slope raster (in degrees) is computed.
Raster grid is created from country, epsg and spatial resolution parameters.
Process SRTM tiles . Tiles are merged into a single mosaic reprojected and masked according to the area of interest . In addition a slope raster ( in degrees ) is computed . Raster grid is created from country epsg and spatial resolution parameters .
def process(
    extent: str,
    epsg: int,
    resolution: float,
    input_dir: str,
    output_dir: str,
    overwrite: bool,
):
    dst_dem = os.path.join(output_dir, "dem.tif")
    dst_slope = os.path.join(output_dir, "slope.tif")
    fs = utils.filesystem(dst_dem)
    fs.makedirs(output_dir, exist_ok=True)
    geom = utils.parse_extent(extent)
    dst_crs = CRS.from_epsg(int(epsg))
    _, shape, bounds = processing.create_grid(
        geom=geom, dst_crs=dst_crs, dst_res=resolution
    )
    for fp, label in zip((dst_dem, dst_slope), ("DEM", "Slope")):
        if fs.isfile(fp):
            if overwrite:
                fs.rm(fp)
                logger.info(f"Deleted old {label} file at {fp}.")
            else:
                raise FileExistsError(f"{label} already exists at {fp}.")
    with tempfile.TemporaryDirectory(prefix="AccessMod_") as tmp_dir:
        tiles = utils.unzip_all(input_dir, tmp_dir)
        if len(tiles) == 0:
            return FileNotFoundError(f"No SRTM tile found at {input_dir}.")
        mosaic = merge_tiles(
            tiles, os.path.join(tmp_dir, "mosaic.tif"), overwrite=overwrite
        )
        slope = compute_slope(
            mosaic, os.path.join(tmp_dir, "slope.tif"), overwrite=overwrite
        )
        for src_fp, dst_fp in zip((mosaic, slope), (dst_dem, dst_slope)):
            tmp_fp = src_fp.replace(".tif", "_reproj.tif")
            tmp_fp = processing.reproject(
                src_fp,
                tmp_fp,
                dst_crs=dst_crs,
                dtype="int16",
                bounds=bounds,
                height=shape[0],
                width=shape[1],
                resampling_alg="bilinear",
            )
            processing.mask(tmp_fp, dst_fp, geom)
def process ( extent : str , epsg : int , resolution : float , input_dir : str , output_dir : str , overwrite : bool , ) : dst_dem = os . path . join ( output_dir , "dem.tif" ) dst_slope = os . path . join ( output_dir , "slope.tif" ) fs = utils . filesystem ( dst_dem ) fs . makedirs ( output_dir , exist_ok = True ) geom = utils . parse_extent ( extent ) dst_crs = CRS . from_epsg ( int ( epsg ) ) _ , shape , bounds = processing . create_grid ( geom = geom , dst_crs = dst_crs , dst_res = resolution ) for fp , label in zip ( ( dst_dem , dst_slope ) , ( "DEM" , "Slope" ) ) : if fs . isfile ( fp ) : if overwrite : fs . rm ( fp ) logger . info ( f"Deleted old {label} file at {fp}." ) else : raise FileExistsError ( f"{label} already exists at {fp}." ) with tempfile . TemporaryDirectory ( prefix = "AccessMod_" ) as tmp_dir : tiles = utils . unzip_all ( input_dir , tmp_dir ) if len ( tiles ) == 0 : return FileNotFoundError ( f"No SRTM tile found at {input_dir}." ) mosaic = merge_tiles ( tiles , os . path . join ( tmp_dir , "mosaic.tif" ) , overwrite = overwrite ) slope = compute_slope ( mosaic , os . path . join ( tmp_dir , "slope.tif" ) , overwrite = overwrite ) for src_fp , dst_fp in zip ( ( mosaic , slope ) , ( dst_dem , dst_slope ) ) : tmp_fp = src_fp . replace ( ".tif" , "_reproj.tif" ) tmp_fp = processing . reproject ( src_fp , tmp_fp , dst_crs = dst_crs , dtype = "int16" , bounds = bounds , height = shape [ 0 ] , width = shape [ 1 ] , resampling_alg = "bilinear" , ) processing . mask ( tmp_fp , dst_fp , geom )
Process SRTM tiles.
Process SRTM tiles .
"""Process SRTM tiles.

    Tiles are merged into a single mosaic, reprojected and masked according to
    the area of interest. In addition, a slope raster (in degrees) is computed.
    Raster grid is created from country, epsg and spatial resolution parameters.
    """ # get raster metadata from country boundaries, spatial resolution and EPSG # slope is computed from DEM before reprojection and masking
extent
str
epsg
int
resolution
float
input_dir
str
output_dir
str
overwrite
bool