2

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

[checkbox that is needed to be unchecked in order to visualise no data value[1] desired symbology (15 is no data value)

1

1 Answer 1

1

Hopefully it's ok to answer my own question after I've asked it.

I found that adding the following code solved my problem of NoData not displaying in the 255,0,255 colour that I defined. I specify the value in each band as follows:

provider.setNoDataValue(1, 255) 
provider.setNoDataValue(2, 0) 
provider.setNoDataValue(3, 255) 
layer.triggerRepaint()

So successful code is as follows:

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()
provider.setNoDataValue(1, 255) 
provider.setNoDataValue(2, 0) 
provider.setNoDataValue(3, 255) 
layer.triggerRepaint()
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’)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.