1

I have 24 layers I want to save on QGIS which all happen to be Tiff files (rasters). I am wondering how to run code to complete this task.

myDir = 'C:\temp'
layers = layers = iface.mapCanvas().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("%s\\%s.tif" %(myDir, layer.name() ))
 file_writer.writeRaster(pipe,
 width,
 height,
 extent,
 layer.crs())

This is the code I have currently to try and run.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 9, 2017 at 12:20
11
  • 2
    And what happens when you run it? :) Commented Jun 9, 2017 at 12:23
  • 3
    @Joseph an error could be file_writer = QgsRasterFileWriter(myDir + layer.name() + ".tif")with a missing "\" I would use QgsRasterFileWriter("%s\\%s.tif" %(myDir, layer.name() )) instead Commented Jun 9, 2017 at 12:27
  • 2
    @Jhunt9 - Could you add print "%s\\%s.tif" %(myDir, layer.name() ) inside your loop to see exactly what the paths look like? Commented Jun 9, 2017 at 12:55
  • 2
    Is it possible that an escape sequence is ruining your path? Try: myDir = r'C:\temp' --> \t is an escape sequence Commented Jun 9, 2017 at 12:59
  • 1
    Yes! The escape charater got you! --> python-reference.readthedocs.io/en/latest/docs/str/escapes.html Commented Jun 9, 2017 at 13:01

1 Answer 1

1
myDir = r'C:\temp'
layers = layers = iface.mapCanvas().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("%s\\%s.tif" %(myDir, layer.name() ))
 file_writer.writeRaster(pipe,
 width,
 height,
 extent,
 layer.crs())
 print "%s\\%s.tif" %(myDir, layer.name() )

This is the Code that worked! Thank you @LaughU, @AndreasK, & @Joseph

answered Jun 9, 2017 at 13:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.