0

Here is my code. I want to resample the raster. But it doesn't set the projection, although I write it. May be there is a simplier way to perform the resampling by pixel size? Is it incorrect to set the projection in such a way from array?

dataset = gdal.Open(file1)
projection = dataset.GetProjection()
transform = dataset.GetGeoTransform()
gdalBand = dataset.GetRasterBand(43)
array = gdalBand.ReadAsArray().astype(numpy.float32)
xsize = dataset.RasterXSize
ysize = dataset.RasterYSize
dataset=None
gdalBand=None
format = "GTiff"
driver = gdal.GetDriverByName(format)
outRaster = driver.Create(path+r'outfile.tif', xsize, ysize, 1, gdal.GDT_Float32 )
outRaster.SetProjection( projection )
outRaster.SetGeoTransform( transform )
outRaster.GetRasterBand( 1 ).WriteArray( array )
outRaster = None

print(projection)


dataset = gdal.Open(path+r'outfile.tif')
prj=dataset.GetProjection()
print(prj)
dataset=gdal.Translate(path+r'outfile1.tif', \
    dataset,xRes=50,yRes=50)
dataset=None



dataset = gdal.Open(path+'outfile1.tif')
print(dataset.RasterXSize)
stats=zonal_stats(path+shape,dataset)
  • I have tested you code on my data and it works fine. Can you elaborate on your problem? Perhaps state what outputs you are getting and what outputs you are expecting. Also make sure that GetRasterBand(43) is in deed 43? (hyperspectral?) and be careful with using "format" as a variable because its reserved for a python function. – Jascha Muller Mar 11 at 19:44

Your Answer

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

Browse other questions tagged or ask your own question.