5

So I am trying to batch save 100's of styled rasters. I have managed to save a single raster in the python console using the following code:

layer = iface.activeLayer()

extent = layer.extent()
width, height = layer.width(), layer.height()
renderer = layer.renderer()
provider=layer.dataProvider()
crs = layer.crs().toWkt()

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

file_writer = QgsRasterFileWriter('e:/Data/output.tif')

file_writer.writeRaster(pipe,
                        width,
                        height,
                        extent,
                        layer.crs())

What do I need to add so I can save all the rasters currently open in QGIS?

1
  • I found this question when looking for a way to batch save rasters so that I could use the "VRT" option to cut multiple rasters into tiles, and it took a while before I found a solution, but this did work: howtoinqgis.wordpress.com/2016/12/17/…
    – wfgeo
    Oct 13, 2020 at 7:36

2 Answers 2

6

With this code snipped you would get all layers in your current project:

layers = QgsMapLayerRegistry.instance().mapLayers()
# for testing 
for name, layer in layers.iteritems():
     print name, layer.type()

I assume your code is correct and that you only have your rasters you want to save loaded. so you would do something like this:

layers = iface.legendInterface().layers()
pipe = QgsRasterPipe()
for  layer in layers:
   extent = layer.extent()
   width, height = layer.width(), layer.height()
   renderer = layer.renderer()
   provider=layer.dataProvider()
   crs = layer.crs().toWkt() 
   pipe.set(provider.clone())
   pipe.set(renderer.clone())

   file_writer = QgsRasterFileWriter('e:/Data/%s.tif' %name)

   file_writer.writeRaster(pipe,
                        width,
                        height,
                        extent,
                        layer.crs())
0
5

Thank you, your code worked! I had to just make a minor change as %name didn't work for me. Here is code that worked for me for anybody else having the same problem:

myDir = 'e:/Data/'
layers = iface.legendInterface().layers()
pipe = QgsRasterPipe()
for layer in layers:
   extent = layer.extent()
   width, height = layer.width(), layer.height()
   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())
3
  • Is there any way to modify this Python to save DEM files? I can save my raster files but when i do it creates the new raster with 4 bands taking the colours from the pixel rather than taking the elevation data from Band 1 as i would like. Oct 9, 2018 at 9:40
  • 2
    @DavidLaverty you can solve that just removing the line "pipe.set(renderer.clone())" Nov 16, 2018 at 7:47
  • 1
    In QGIS 3.16.3 the code exports the rasters but it does but without styling/rendering information. Any Idea why that happens? Also, is there a way to add the build pyramids parameter? Jan 30, 2021 at 1:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.