5

I'm trying to resample a GeoTIFF file to match another raster layer using python GDAL package. Most of the ways I found online is to use gdalwarp resample data from the command line. Is there a way to get cell resolution inside of python script and do the resample process inside of the python script? Maybe things like:

disout.SetGeoTransform(diskin.GetGeoTransform())
asked Mar 29, 2017 at 18:04

1 Answer 1

9

I found that the easiest way is to create a new raster file with dimensions equal to the reference file, SetGeoTransofrm and SetProjection of the new raster file to match the reference file then re-project the file and output, sample code shown below:

from osgeo import gdal, gdalconst
inputfile = #Path to input file
input = gdal.Open(inputfile, gdalconst.GA_ReadOnly)
inputProj = input.GetProjection()
inputTrans = input.GetGeoTransform()
referencefile = #Path to reference file
reference = gdal.Open(referencefile, gdalconst.GAReadOnly)
referenceProj = reference.GetProjection()
referenceTrans = reference.GetGeoTransform()
bandreference = reference.GetRasterBand(1) 
x = reference.RasterXSize 
y = reference.RasterYSize
outputfile = #Path to output file
driver= gdal.GetDriverByName('GTiff')
output = driver.Create(outputfile,x,y,1,bandreference.DataType)
output.SetGeoTransform(referenceTrans)
output.SetProjection(referenceProj)
gdal.ReprojectImage(input,output,inputProj,referenceProj,gdalconst.GRA_Bilinear)
del output
Yanofsky
2232 silver badges7 bronze badges
answered Mar 30, 2017 at 13:53
2
  • So which is the final pixel spacing (cell resolution)? How is computed? Commented Feb 13, 2019 at 17:24
  • eduardosufan they are 'standard' (or commonly used) raster resampling techniques. all options laid out in gdal (gdal.org/programs/…). @yanofsky - I have noticed that this will resample a multi band image into a single value. is there a way to resample such that the output image has resample values for each band? Commented Apr 27, 2020 at 17:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.