0

i have some trouble warping one raster to different resolution. My input is a one channel tiff with. I know the geo-coordinates of x_min and y_max. I also know that the pixel resolution is 5 meters. I now want to warp the same raster to pixel resolution of 6.5 meters.

Input in the function is the numpy array and the upper left corner coordinates with the src and dst pixel resolution.

My code is:

def resample_raster(raster, raster_x0, raster_ymax, src_xres, src_yres, dst_xres, dst_yres):


    dst_height=int((dst_yres/src_yres)*raster.shape[0])
    dst_width=int((dst_xres/src_xres)*raster.shape[1])

    dstAR=np.empty((dst_height, dst_width))

    srcDS=dstDS=gdal_array.OpenArray(raster)
    srcDS.SetGeoTransform((raster_x0,src_xres,0, raster_ymax, 0, -src_yres ) )
    dstDS=gdal_array.OpenArray(dstAR)
    srcDS.SetGeoTransform((raster_x0,dst_xres,0, raster_ymax, 0, -dst_yres ) )

    gdal_warp_options=osgeo.gdal.WarpOptions(xRes=dst_xres,yRes=dst_yres, resampleAlg=osgeo.gdal.GRA_NearestNeighbour)



    osgeo.gdal.Warp(dstDS, srcDS,options=gdal_warp_options)

    new_raster=np.array(dstDS.GetRasterBand(1).ReadAsArray())

    return new_raster

The function gives me an empty array or is killed.

1

I am not certain of what you try to achieve but I have two sugestions:

  1. Follow this idea on how to resize a numpy array using scipy.
  2. Use gdal.Warp to warp a tiff to a different resolution. For example:
import gdal
infn = '/path/to/source.tif'
outfn = '/path/to/target.tif'

xres=6.5
yres=6.5
resample_alg = 'near'

ds = gdal.Warp(outfn, infn, warpoptions=dict(xRes=xres, yRes=yres, resampleAlg=resample_alg)
ds = None

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.