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.
1 Answer 1
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).
-
I tried your code, but like mine, it doesn't work. If you could please try it out, that would be awesome. Thanks.user1905507– user19055072016年04月15日 11:14:48 +00:00Commented 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 andlayers = iface.layerTreeView().selectedLayers()
.ArMoraer– ArMoraer2016年04月15日 11:34:26 +00:00Commented 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!user1905507– user19055072016年04月15日 12:52:18 +00:00Commented Apr 15, 2016 at 12:52
-
One auick question would the imports I ised in my first post work for this code?user1905507– user19055072016年04月15日 12:57:10 +00:00Commented 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 *
.ArMoraer– ArMoraer2016年04月15日 13:08:50 +00:00Commented Apr 15, 2016 at 13:08