4

I am trying to export existing raster layers as tif files. I just want to avoid saving each raster manually. this is the code I am trying to use, but it doesn't seem to work:

from PyQt4.QtCore import *
from qgis.core import *
from qgis.gui import *
import os
import sys
layers = iface.layerTreeView().selectedLayers()
for layer in layers:
 output='Desktop'%layer.name%'.tif'

As specified in a comment below, I modified my code to this:

from PyQt4.QtCore import *
from qgis.core import *
from qgis.gui import *
import os
import sys
layers = iface.layerTreeView().selectedLayers()
for layer in layers:
 file_name = 'D:\tif' + layer.name() + '.tif'
 file_writer = QgsRasterFileWriter(file_name)
 pipe = QgsRasterPipe()
 provider = layer.dataProvider()
 if not pipe.set(provider.clone()):
 print "Cannot set pipe provider"
 continue
 file_writer.writeRaster(
 pipe,
 provider.xSize(),
 provider.ySize(),
 provider.extent(),
 provider.crs())

I can't seem to get it to work and I am not getting any error.

I already selected the layers and it still does not work.

enter image description here

Kersten
10k3 gold badges39 silver badges59 bronze badges
asked Apr 15, 2016 at 2:59

1 Answer 1

5

Your code doesn't work because you're not doing anything in your for loop (except setting a file name).

Saving a raster is a bit tedious, afaik. Here is a sample code I gathered from varied sources; I didn't test it, so it might need some adjustments. It relies on the QgsRasterPipe and QgsRasterFileWriter classes.

layers = iface.layerTreeView().selectedLayers()
for layer in layers:
 file_name = '/full/raster/path/' + layer.name() + '.tif'
 file_writer = QgsRasterFileWriter(file_name)
 pipe = QgsRasterPipe()
 provider = layer.dataProvider()
 if not pipe.set(provider.clone()):
 print "Cannot set pipe provider"
 continue
 file_writer.writeRaster(
 pipe,
 provider.xSize(),
 provider.ySize(),
 provider.extent(),
 provider.crs())

Note: I'd love to know if there is a simpler way to save rasters!

EDIT: added dummy path in file_name (full path is required).

answered Apr 15, 2016 at 6:38
15
  • I tried your code, but like mine, it doesn't work. If you could please try it out, that would be awesome. Thanks. Commented Apr 15, 2016 at 11:14
  • Well, I just tried it and it works like a charm (QGIS 2.8). However, you have to put the full path to your new raster in file_name (including the name itself). If you still have an issue with this correction, please copy the error message in your next comment. EDIT: I didn't explicitely put it in my answer, but the code I provided replaces only the two last lines of your code. You still have to write the imports and layers = iface.layerTreeView().selectedLayers(). Commented Apr 15, 2016 at 11:34
  • Sorry to bother so much, but could you tell me which imports are needed? I am really new to this. Thanks! Commented Apr 15, 2016 at 12:52
  • One auick question would the imports I ised in my first post work for this code? Commented Apr 15, 2016 at 12:57
  • Sorry, no import is actually needed if you run this script from the QGIS Python console. If you run it externally, you'll probably need from qgis.core import *. Commented Apr 15, 2016 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.