Friday 19 May 2017

qgis - Export raster layer in python to specific folder


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




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...