I have hundreds of raster files. They have a "band 1" and contain values from 0 - 9000, however only values between 0 and 255 are of interest. In order to make the files smaller I'm thinking to delete all data with values >255 and then use LZW compression to make it even smaller. I'm not sure about how to do this though.
I have made a python script like this:
myDir = '/output/'
layers = QgsProject.instance().mapLayers()
pipe = QgsRasterPipe()
for layer in layers.values():
print(layer.name())
opts = ["COMPRESS=LZW"]
extent = layer.extent()
width, height = layer.width(), layer.height()
renderer = layer.renderer()
provider=layer.dataProvider()
provider.setUserNoDataValue(1, [QgsRasterRange(256,999999)])
crs = layer.crs().toWkt()
pipe.set(provider.clone())
pipe.set(renderer.clone())
file_writer = QgsRasterFileWriter(myDir + layer.name() + ".tif")
file_writer.setCreateOptions(opts)
file_writer.writeRaster(pipe,
width,
height,
extent,
layer.crs())
The visible file in my qgis workspace gets values 0-255 and appears to be correct, but the saved file in /output/ folder has now 3 bands and not correct values. If I manually save the file in my workspace without changing anything, it turns out to be correct. But the python-saved files turns out incorrect.
What am I doing wrong? (I use QGIS 3.4)
gdal
in your Python script, I might help you out.