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
  • 9,899
  • 3
  • 37
  • 59
user1905507
  • 93
  • 1
  • 8

1 Answers1

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).

ArMoraer
  • 5,649
  • 2
  • 26
  • 48
  • I tried your code, but like mine, it doesn't work. If you could please try it out, that would be awesome. Thanks. – user1905507 Apr 15 '16 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(). – ArMoraer Apr 15 '16 at 11:34
  • Sorry to bother so much, but could you tell me which imports are needed? I am really new to this. Thanks! – user1905507 Apr 15 '16 at 12:52
  • One auick question would the imports I ised in my first post work for this code? – user1905507 Apr 15 '16 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 *. – ArMoraer Apr 15 '16 at 13:08
  • Can you please tell me which importa I have to use, since I am really new to this? Thanks, I really appreciate it. – user1905507 Apr 15 '16 at 13:09
  • from what I can understand, I would only need to add the full path where I want to save the raster file, since I already am using the layer's name as the name I want to save the raster file. Please correct me if I am wrong. – user1905507 Apr 15 '16 at 13:41
  • when I run the script in the console, it runs but gives me no error and does nothing. I posted the code I am currently using in the question. – user1905507 Apr 15 '16 at 13:53
  • Your code seems to be fine. I forgot to mention one thing: you have to select the raster(s) you want to export in the TOC (QGIS left panel). – ArMoraer Apr 15 '16 at 14:16
  • I added a picture, so you can see what I am doing. I already selected the layers and it still doesn't work. – user1905507 Apr 15 '16 at 14:25
  • By 'select', I mean that the layer must be highlighted (just click on the layer's name). This is different than activating the layers: your layers are activated but not selected. – ArMoraer Apr 15 '16 at 14:29
  • I did as you said, but still nothing. I replaced the image again. – user1905507 Apr 15 '16 at 14:43
  • Well, I'm lost then, sorry. I have no idea why it doesn't work on your computer, especially since there is no error message. I can only encourage you to check your path and your copy of the script (indentation is critical with Python). – ArMoraer Apr 15 '16 at 14:59
  • I am finally getting an error that I am getting: Traceback (most recent call last): File "", line 1, in File "D:/tif/SaveRaster.py", line 15, in if not pipe.set(provider.clone()): AttributeError: 'QgsVectorDataProvider' object has no attribute 'clone' – user1905507 Apr 15 '16 at 15:30