2

Starting with FME, i convert some hundreds of geotiffs to jpeg2000. The jpeg2000 writer does not support nodata-values. Adding the data to QGIS gives me these black (#000000) areas when rasters are rotated. With raster>properties i can set a transparency for #000000 and save a layer definition (*.qlr) for that raster which can be loaded on demand with the desired transparency.

Question: Since I have to do this for hundreds of rasters, is there a way (i.e. Python, GraphicalModeler) to automatically set transparency for a raster and save it to a layer definition?

3 Answers 3

3

I would read the JPEG2000 files through GDAL virtual format http://www.gdal.org/drv_vrt.html.

Convert JPEG2000 into VRT and assign nodata value with gdal_translate:

gdal_translate -of vrt -a_nodata 0 input.jp2 output.vrt

Check with gdalinfo

gdalinfo output.vrt

Band 1 Block=128x128 Type=Byte, ColorInterp=Red
  NoData Value=0
  Overviews: 6000x6000, 3000x3000, 1500x1500, 750x750, 375x375, 187x187
Band 2 Block=128x128 Type=Byte, ColorInterp=Green
  NoData Value=0
  Overviews: 6000x6000, 3000x3000, 1500x1500, 750x750, 375x375, 187x187
Band 3 Block=128x128 Type=Byte, ColorInterp=Blue
  NoData Value=0
  Overviews: 6000x6000, 3000x3000, 1500x1500, 750x750, 375x375, 187x187

Looks good with NoData. Now just keep both the .vrt and .jp2 files available when you open .vrt with QGIS.

The GDAL VRT file is a small XML file which has a pointer to the real image file and additional instructions about how to handle it. In this snippet "read file input.jp2 and consider 0 as NoData for band 1".

<VRTRasterBand dataType="Byte" band="1">
    <NoDataValue>0</NoDataValue>
    <ColorInterp>Red</ColorInterp>
    <SimpleSource>
      <SourceFilename relativeToVRT="1">input.jp2</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="12000" RasterYSize="12000" DataType="Byte" BlockXSize="1024" BlockYSize="1024" />
      <SrcRect xOff="0" yOff="0" xSize="12000" ySize="12000" />
      <DstRect xOff="0" yOff="0" xSize="12000" ySize="12000" />
    </SimpleSource>
  </VRTRasterBand>
2

Just a quick thought off the top of my head: maybe its possible to create the .qlr file by programmatic means without using qgis? Look at a generated qlr and see if you can make sense of it.

2
  • yay, i think thats it!!! The qlr files structure all the same, and can be simply generated by replacing the file name in the datasource. But one point remains to me: What is this layer id the qlr refers to and how is it generated? Commented Mar 31, 2016 at 8:49
  • i've just seen that the id is obviously made of layername+yyyymmdd+hhmm+therest, i figure out what therest is, but possibly the id doesn't really have an impact on if it works. Commented Mar 31, 2016 at 8:58
0

That worked fine for me. I put it in a for-do-done like

for f in *.jp2; do gdal_translate -of vrt -a_nodata 0 $f $f.vrt; done

Besides messages 'FAILURE: Too many command options.' i get the desired results as well.

JPEG2000:

enter image description here

VRT:

enter image description here

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.