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.
No comments:
Post a Comment