0

I want to co-register a DEM file with 100m resolution with a landcover file of 10m resolution. Thereby the DEM file is resampled to match the 10m resolution. Obviously, the file size of the new DEM will be bigger but in my case it is huge! (from 58kB to 136'927 kB). This seems not reasonably for me.

This is the code I use:

infile=os.path.join(f'DEM.tif')
match=os.path.join('landcover.tif') #co-register the infile to the match file
outfile=os.path.join('DEM_resampled.tif')

# open input
with rasterio.open(infile) as src:
    src_transform = src.transform
    
    # open input to match
    with rasterio.open(match) as match:
        dst_crs = match.crs
        
        # calculate the output transform matrix
        dst_transform, dst_width, dst_height = calculate_default_transform(
            src.crs,     # input CRS
            dst_crs,     # output CRS
            match.width,   # input width
            match.height,  # input height 
            *match.bounds,  # unpacks input outer boundaries (left, bottom, right, top)
        )

    # set properties for output
    dst_kwargs = src.meta.copy()
    dst_kwargs.update({"crs": dst_crs,
                        "transform": dst_transform,
                        "width": dst_width,
                        "height": dst_height,
                        "nodata": 0})
    print("Coregistered to shape:", dst_height,dst_width,'\n Affine',dst_transform)
    # open output
    with rasterio.open(outfile, "w", **dst_kwargs) as dst:
        # iterate through bands and write using reproject function
        for i in range(1, src.count + 1):
            reproject(
                source=rasterio.band(src, i),
                destination=rasterio.band(dst, i),
                src_transform=src.transform,
                src_crs=src.crs,
                dst_transform=dst_transform,
                dst_crs=dst_crs,
                resampling=Resampling.bilinear)

Does somebody have any tips?

3
  • 4
    Going from 100m pixel size into 10m pixel size means 100x more pixels. And if the source file was compressed and the target file not it will add more to the file size.
    – user30184
    Commented Oct 9 at 15:40
  • Is it good practice to just compress the target file with rasterio while saving it? Is there any downside by compressing it?
    – tofri
    Commented Oct 9 at 16:33
  • 3
    If you use lossless compression I would say that usually there are no issues. Lossy compression will change some height values slightly that may not be desired. For lossless try LZW or deflate, and test what effect the predictor has for the compression ratio with your data.
    – user30184
    Commented Oct 9 at 18:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.