6

I have 3 band GeoTiffs of aerial imagery. The imagery is byte data with values of 0-255 on each band. A 'nodata' value has not been assigned on any band. I am assembling these images using a VRT, with no additional arguments, and displaying them in QGIS.

gdalbuildvrt out.vrt *.tif

Is it possible to tell QGIS or gdalbuildvrt to ONLY render transparency in those instances where:

b1 = 0 b2 = 0 b3 = 0

Currently, if you set transparency to zero in QGIS or use:

gdalbuildvrt -srcnodata "0 0 0" out.vrt *.tif

many instances outside of these conditions are rendered transparent. For example, if:

b1 = 0 b2 = 0 b3 = 15

Am I missing something in defining transparent/nodata values in QGIS or with gdal?

4
  • 1
    No, you're not missing anything. -vrtnodata 255 will set the raster band as 'nodata' (not to be confused with transparency which is another issue altogether) where the band is 255 but the reading software may not be intelligent enough to only render NoData when all the bands == 255 only (I know Esri doesn't). You could consider creating a mask band raster using gdal_calc and insert that into your VRT then display as RGBA Sep 7, 2017 at 1:16
  • Cheers, Michael. Setting -srcnodat or -vrtnodata doesn't really solve the issue. The raster mask is something we are considering; however, we are doing a good bit of warping in our operations and the mask becomes an issue. I was just hoping to somewhere be able to state that 'only those instances where nodata = "0 0 0" will there be transparency. I do not think QGIS has this ability either. Trouble is, in the end I am using neither. Instead I am running the vrt thorough a mapnikXML and rendering using MapProxy.
    – icr
    Sep 7, 2017 at 2:07
  • Could you put a small sample tif that shows that 0,0,15 case available somewhere? It should not happen. For the vrt case it might be worth trying if using also the -vrtnodata option helps.
    – user30184
    Sep 7, 2017 at 5:58
  • I'll see if I can get a test case up somewhere. the case "0 0 15" should not happen, but it does in this project. We are using aerial imagery that has these instances in very dark locations. This question was meant to solve a large issue we were having. We did manage to solve it using a combination of: 'gdalbuildvrt -srcnodata "0 0 0" welly.vrt *.tif' and separating the individual rgb bands in mapnik. By having control of each individual band in a dataset, I can render transparent only those locations that are truly "0 0 0"
    – icr
    Sep 8, 2017 at 2:55

3 Answers 3

4

QGIS can handle this under the Layer Properties/Transparency.

Layer Properties / Transparency

1

Here are two images representing what got us to questioning how nodata is read in QGIS.

enter image description here

When we defining the nodata of the image to "0" using QGIS, we were also making internal pixels transparent too. Even those regions that were not "0 0 0" are being rendered transparent.

enter image description here

I was only using QGIS as my testing space to be honest, but it was helping me understand how to solve the issue using MapnikXML; which we managed to do using a combination of:

gdalbuildvrt -srcnodata "0 0 0" welly.vrt *.tif

then rendering the individual rgb bands in Mapnik and adding them back together with:

raster-comp-op: plus;

Using CartoCSS, in order to achieve a similar outcome to the solution in QGIS seen above, the code looks something like:

#red 
{
  raster-opacity:1;
  raster-scaling:bilinear;
  raster-comp-op:plus;
  raster-colorizer-default-mode:linear;
  raster-colorizer-default-color: transparent;
  raster-colorizer-stops:
    stop(0,transparent, exact)
    stop(1,#000)
    stop(200,rgb(255,0,0))
}


#green 
{
  raster-opacity:1;
  raster-scaling:bilinear;
  raster-comp-op:plus;
  raster-colorizer-default-mode:linear;
  raster-colorizer-default-color: transparent;
  raster-colorizer-stops:
    stop(0,transparent, exact)
    stop(1,#000)
    stop(200,rgb(0,255,0))
}

#blue 
{
  raster-opacity:1;
  raster-scaling:bilinear;
  raster-comp-op:plus;
  raster-colorizer-default-mode:linear;
  raster-colorizer-default-color: transparent;
  raster-colorizer-stops:
    stop(0,transparent, exact)
    stop(1,#000)
    stop(200,rgb(0,0,255));
}
0

I took an orthophoto and warped it to have black slivers. In this state the new image is without nodata as can be verified with gdalinfo.

Band 1 Block=1000x2 Type=Byte, ColorInterp=Red
Band 2 Block=1000x2 Type=Byte, ColorInterp=Green
Band 3 Block=1000x2 Type=Byte, ColorInterp=Blue

Then I used your gdalbuildvrt command

gdalbuildvrt -srcnodata "0 0 0" out.vrt test.tif

The resulting .vrt file has now noData defined and gdalinfo reports it

Band 1 Block=128x128 Type=Byte, ColorInterp=Red
  NoData Value=0
Band 2 Block=128x128 Type=Byte, ColorInterp=Green
  NoData Value=0
Band 3 Block=128x128 Type=Byte, ColorInterp=Blue
  NoData Value=0

This is how the warped image shows in QGIS with default settings on top of a green polygon

enter image description here

And this is how the VRT file with nodata opens in QGIS out-of-the-box

enter image description here

I can't see any issue with my test image. If you still have a problem it would be nice if you could provide us with test data.

1
  • I will see if I can build a test case somewhere for you to have a look. As stated above, we did solve our original issue. This questions was a smaller piece of a larger puzzle we were trying to solve. I'll at least give you a screen shot of the larger problem we were trying to solve, but it is getting away from the original intension of the issue stated here.
    – icr
    Sep 8, 2017 at 3:08

Your Answer

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

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