1

I am attempting to batch save 300 10m DTM tiles all in the same Raster type 'Float 32-bit' for using to create a VRT. At present I cannot create a VRT using the original Raster layers as some of the Tiles cannot be converted due to various reasons Such as the postive NS resolution, some saved as VRT's already and others 'Float 64-bit' meaning it will not create a full VRT of these type combinations for whatever reason.

I have been able to create and save the full 300 tiles using the following python code in the console which I have gathered from this site in another question

myDir = "C:\Users\MY_DIR\"
layers = iface.legendInterface().layers()
pipe = QgsRasterPipe()
for layer in layers:
   extent = layer.extent()
   width, height = layer.width(), layer.GetRasterBand(1)
   renderer = layer.renderer()
   provider=layer.dataProvider()
   crs = layer.crs().toWkt() 
   pipe.set(provider.clone())
   pipe.set(renderer.clone())
   file_writer = QgsRasterFileWriter(myDir + layer.name() + ".tif")
   file_writer.writeRaster(pipe,
                           width,
                           height,
                           extent,
                           layer.crs())

The problem is that once created in my directory folder, the .tif's created have got 4 raster bands and the heights of my Terrain are all lower than what the original raster is. Is there something I could add to this code that would allow me to keep the original Band one values of the raster corresponding to the true height of the terrain?

2
  • 1
    I have figured this one out, if you remove the line 'pipe.set(renderer.clone())' , it will make an exact duplicate of your DTM and not as a 4-banded rendered image. Hope this is of some use to anyone trying to batch save raster tiles like i was Oct 9, 2018 at 12:04
  • I wish you'd answered your own question and marked it as accepted. Would have saved me a long time searching for this fix. Feb 13, 2020 at 10:46

1 Answer 1

1

From the author's comment:

I have figured this one out, if you remove the line pipe.set(renderer.clone()) , it will make an exact duplicate of your DTM and not as a 4-banded rendered image. Hope this is of some use to anyone trying to batch save raster tiles like i was.

I can also confirm that this is the solution for the issue where single band rasters are written out as 4-band rasters.

I still see differences in the values of the output tiff compared with the input, like some smoothing or interpolation is performed.

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.