0

How can I resample a single band GeoTIFF using Bilinear interpolation?

import os

import rasterio
from rasterio.enums import Resampling
from rasterio.plot import show,show_hist
import numpy as np

if __name__ == "__main__":
    

    input_Dir = 'sample.tif'
    #src = rasterio.open(input_Dir) 
    #show(src,cmap="magma")

    upscale_factor = 2

    with rasterio.open(input_Dir) as dataset:
        
        # resample data to target shape
        data = dataset.read(
            out_shape=(
                dataset.count,
                int(dataset.height * upscale_factor),
                int(dataset.width * upscale_factor)
            ),
            resampling=Resampling.bilinear
        )

        # scale image transform
        transform = dataset.transform * dataset.transform.scale(
            (dataset.width / data.shape[-1]),
            (dataset.height / data.shape[-2])
        )
        show(dataset,cmap="magma",transform=transform)

I have tried the following code and my output is as follows:

enter image description here

I am trying to achieve the following output:

enter image description here

azis511
  • 13
  • 1
  • 4
  • if you don't need to save the resampled tif, matplotlib will do that interpolation for you https://matplotlib.org/3.1.1/gallery/images_contours_and_fields/interpolation_methods.html – Paul H Jul 20 '20 at 04:52
  • but, you're not displaying your resampled `data`. you're showing the original `dataset` – Paul H Jul 20 '20 at 04:53
  • would I have to create a new raster? cause simply passing the data variable in the show method wont do the job. – azis511 Jul 20 '20 at 05:24
  • if you use matplotlib directly, no, you wouldn't need to create a new tiff, as I said – Paul H Jul 20 '20 at 05:43
  • Thank you. It worked – azis511 Jul 21 '20 at 01:56

1 Answers1

0

One option would be to use the GDAL python bindings. Then you can perform the resample in memory (or you can save the image if you want). Assuming the old raster resolution was 0.25x0.25 and you're resampling to 0.10x0.10:

from osgeo import gdal

input_Dir = 'sample.tif'

ds = gdal.Translate('', input_Dir, xres=0.1, yres=0.1, resampleAlg="bilinear", format='vrt')

If you want to save the image put output filepath instead of the empty string for the first argument and change the format to 'tif'!

JWB
  • 238
  • 1
  • 4
  • 12