I've been trying to reproject an input (multi-band) GEOTIFF file to a destination transformation/projection and write these reprojected bands to a new 3 band GEOTIFF file (since I don't need the other bands). I've tried combining 2 blocks of code that work independently of each other (one writes 3 bands to a new GEOTIFF, the other reprojects a 1 band GEOTIFF to a reference file). However, I receive the following error:
TypeError: in method 'ReprojectImage', argument 1 of type 'GDALDatasetShadow *'
Find the function that I've used below:
def reprojectRaster(inputfile, referencefile, outputfile):
if os.path.exists(outputfile):
os.remove(outputfile)
input = gdal.Open(inputfile, gdalconst.GA_ReadOnly)
with rasterio.open(inputfile) as src:
input_array = src.read((2,1,4)) #red band, green band, nir band
inputProj = input.GetProjection()
#inputTrans = input.GetGeoTransform
reference = gdal.Open(referencefile, gdalconst.GA_ReadOnly)
referenceProj = reference.GetProjection()
referenceTrans = reference.GetGeoTransform()
bandreference = reference.GetRasterBand(1)
x = reference.RasterXSize
y = reference.RasterYSize
no_bands = reference.RasterCount
driver= gdal.GetDriverByName('GTiff')
output = driver.Create(outputfile,x,y,no_bands, bandreference.DataType)
output.SetGeoTransform(referenceTrans)
output.SetProjection(referenceProj)
#I've been trying to make put this loop inside from the other code
for i, image in enumerate(input_array, 1):
input.GetRasterBand(i).WriteArray( image )
gdal.ReprojectImage(inputfile,outputfile,inputProj,referenceProj,gdalconst.GRA_Bilinear)
del output
def CreateGeoTiff(outputfile, inputfile, referencefile):
with rasterio.open(inputfile) as src:
input_array = src.read((2,1,4)) #red, green, nir bands
reference = gdal.Open(referencefile, gdalconst.GA_ReadOnly)
referenceProj = reference.GetProjection()
referenceTrans = reference.GetGeoTransform()
driver = gdal.GetDriverByName('GTiff')
no_bands, cols, rows = input_array.shape
DataSet = driver.Create(outputfile, cols, rows, no_bands, gdal.GDT_Float32)
DataSet.SetGeoTransform(referenceTrans)
DataSet.SetProjection(referenceProj)
for i, image in enumerate(input_array, 1):
DataSet.GetRasterBand(i).WriteArray( image )
DataSet = None
return(gdal.Open(outputfile, gdalconst.GA_ReadOnly))
The tried to combine the following 2 functions:
def reprojectRaster(inputfile, referencefile, outputfile):
input = gdal.Open(inputfile, gdalconst.GA_ReadOnly)
inputProj = input.GetProjection()
reference = gdal.Open(referencefile, gdalconst.GA_ReadOnly)
referenceProj = reference.GetProjection()
referenceTrans = reference.GetGeoTransform()
bandreference = reference.GetRasterBand(1)
x = reference.RasterXSize
y = reference.RasterYSize
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
def CreateGeoTiff(outputfile, inputfile, referencefile):
with rasterio.open(inputfile) as src:
input_array = src.read((2,1,4)) #red, green, nir bands
reference = gdal.Open(referencefile, gdalconst.GA_ReadOnly)
referenceProj = reference.GetProjection()
referenceTrans = reference.GetGeoTransform()
driver = gdal.GetDriverByName('GTiff')
no_bands, cols, rows = input_array.shape
DataSet = driver.Create(outputfile, cols, rows, no_bands, gdal.GDT_Float32)
DataSet.SetGeoTransform(referenceTrans)
DataSet.SetProjection(referenceProj)
for i, image in enumerate(input_array, 1):
DataSet.GetRasterBand(i).WriteArray( image )
DataSet = None
return(gdal.Open(outputfile, gdalconst.GA_ReadOnly))
The code seems to check out, the right inputs and calling the right functions, Python however raises the TypeError mentioned above. Does anyone have an idea where it went wrong?