I am attempting to script a process that I undertake quite frequently, and have got everything working apart from one little step.
Essentially I want to convert a singleband raster into a 3 band geotiff based on a qml style file.
This is working, apart from the fact that No Data needs to be displayed as 255,0,255. If for example the No Data value is 15, I define this in the symbology, but the qml doesn't save the checkbox "No data value" setting which needs to be unchecked in order to display the no data. As a result, my 3 band geotiff displays no data as black instead of pink.
I was wondering if there is a way to integrate this step into my script? I use the python-qgis.bat file from OSGEO4w in C:\OSGeo4W64\bin to run this in command prompt.
Below is the script that exports as rendered image based on my style file, then removes 4th band that is created.
import qgis
from qgis.core import *
from qgis.utils import *
from qgis.gui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
QgsApplication.setPrefixPath("C:/OSGeo4W64/apps/qgis", True)
app = QApplication([], True)
QgsApplication.initQgis()
rasterpath = "C:\Users\folderpath\1bandraster.tif"
layer = QgsRasterLayer(rasterpath, "rastername")
QgsMapLayerRegistry.instance().addMapLayer(layer)
uri = "C:\Users\folderpath\stylefile.qml"
layer.loadNamedStyle(uri)
extent = layer.extent()
width, height = layer.width(), layer.height()
renderer = layer.renderer()
provider=layer.dataProvider()
crs = layer.crs().toWkt()
pipe = QgsRasterPipe()
pipe.set(provider.clone())
pipe.set(renderer.clone())
file_writer = QgsRasterFileWriter('C:\Users\folderpath\4bandraster.tif')
file_writer.writeRaster(pipe,
width,
height,
extent,
layer.crs())
import os
os.system('gdal_translate -co COMPRESS=LZW -of GTiff -b 1 -b 2 -b 3 C:\Users\folderpath\4bandraster.tif C:\Users\folderpath\OUTPUT-3band.tif’)