[GIS] How to use QgsRasterFileWriter to save and reproject raster

coordinate systempyqgisqgisraster

I have to save georeferenced png images as GeoTiff and reproject them from different coordinate systems (UTMs ang Google) to given UTM. I want to do this with pyton code and tried to use QgsRasterFileWriter.

I face two problems here:

  1. How to save file with 4 bands (transparency band)
  2. How to reproject file at the same time

For now my code does the first thing and looks like this:

# Load Raster
fileName = myRaster.png
baseName = myRaster
rlayer = QgsRasterLayer(fileName, baseName)

crsSrc =  rlayer.crs()  # may be any
crsDest =  QgsCoordinateReferenceSystem(4326)  # WGS84 or UTM Zone

renderer = rlayer.renderer()
provider = rlayer.dataProvider()

pipe = QgsRasterPipe()
pipe.set(provider.clone())
pipe.set(renderer.clone())

file_writer = QgsRasterFileWriter(baseName + '_TEST.tif')
file_writer.Mode(1)

file_writer.writeRaster(pipe, provider.xSize(), provider.ySize(), provider.extent(), provider.crs())

I tried with using QgsRasterProjector but never got it right.

Best Answer

Just use this command:

gdalwarp -overwrite -s_srs EPSG:32643 -t_srs EPSG:4326 -of GTiff "Input Raster with EPSG=32643" "Output Raster with EPSG=4326"

Related Question