0

Reproducible example with QGIS:

Download this file: https://github.com/npct/pct-lsoa-vis/releases/download/0.0.1/dutch_slc-10m-merge2-test.tif

Open it in QGIS, open the Layer Properties Window, and untick No Data Value.

It looks like this (note unwanted red patches):

enter image description here

What I would like is for it to be transparent, even when the transparency tick box is unticked, and an output that is something like this (see code below that creates this updated 'fixed' file - but the problem is that this is not geo-referenced): https://github.com/npct/pct-lsoa-vis/releases/download/0.0.1/test-fixed.tif

Note: I tried to solve this using this post but it the input data types are different, this question is fully reproducible, and the 'correct' answer does not work: How do I convince gdal2tiles to output pngs with transparency? hence creating a new question.

Is there a way to do this with GDAL? This was what I did (with the tiling command produced by QGIS):

gdalwarp -dstalpha -srcnodata "255 0 0" -co "ALPHA=YES" dutch_slc-10m-merge2-test.tif "raster_w-alpha.tif"
gdal2tiles -p mercator -z 5-14 -w all -r average -a 0.0 raster_w-alpha.tif output

Which I think is almost there, but looses the colours:

enter image description here

I've also tried these commands in GDAL 2.4, but none worked:

gdalwarp -srcnodata "255,0,0" -dstnodata "0,0,0" -wo INIT_DEST="255,255,255,0"  dutch_slc-10m-merge2-test.tif dutch_slc-10m-merge2-test4.tif
gdal_translate -a_nodata "0,0,0" dutch_slc-10m-merge2-test.tif dutch_slc-10m-merge2-test2.tif 
gdalbuildvrt -srcnodata "255 0 0" outfile.vrt dutch_slc-10m-merge2-test.tif 
gdal_translate outfile.vrt final.tif

Reproducible example showing a way to do what I'm after with magick:

u = "https://github.com/npct/pct-lsoa-vis/releases/download/0.0.1/dutch_slc-10m-merge2-test.tif"
download.file(url = u, destfile = "test.tif")
remotes::install_cran("magick")
#> Skipping install of 'magick' from a cran remote, the SHA1 (2.0) has not changed since last install.
#>   Use `force = TRUE` to force installation
library(magick)
#> Linking to ImageMagick 6.9.7.4
#> Enabled features: fontconfig, freetype, fftw, lcms, pango, x11
#> Disabled features: cairo, ghostscript, rsvg, webp
i = image_read("test.tif")
i

i_fixed = image_transparent(image = i, color = "red")
i_fixed

image_write(i_fixed, "test-fixed.tif")



devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.5.2 (2018-12-20)
#>  os       Ubuntu 18.04.2 LTS          
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_GB:en                    
#>  collate  en_GB.UTF-8                 
#>  ctype    en_GB.UTF-8                 
#>  tz       Europe/London               
#>  date     2019-02-28                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                         
#>  assertthat    0.2.0      2017-04-11 [3] CRAN (R 3.5.0)                 
#>  backports     1.1.3      2018-12-14 [3] CRAN (R 3.5.1)                 
#>  callr         3.1.1      2018-12-21 [3] CRAN (R 3.5.2)                 
#>  cli           1.0.1      2018-09-25 [1] CRAN (R 3.5.1)                 
#>  crayon        1.3.4      2017-09-16 [3] CRAN (R 3.5.0)                 
#>  curl          3.3        2019-01-10 [1] CRAN (R 3.5.2)                 
#>  desc          1.2.0      2018-05-01 [3] CRAN (R 3.5.0)                 
#>  devtools      2.0.1      2018-10-26 [1] CRAN (R 3.5.2)                 
#>  digest        0.6.18     2018-10-10 [3] CRAN (R 3.5.1)                 
#>  evaluate      0.13       2019-02-12 [3] CRAN (R 3.5.2)                 
#>  fs            1.2.6      2018-08-23 [1] CRAN (R 3.5.1)                 
#>  glue          1.3.0.9000 2019-01-28 [1] Github (tidyverse/glue@8188cea)
#>  highr         0.7        2018-06-09 [3] CRAN (R 3.5.0)                 
#>  htmltools     0.3.6      2017-04-28 [3] CRAN (R 3.5.0)                 
#>  httr          1.4.0      2018-12-11 [3] CRAN (R 3.5.1)                 
#>  knitr         1.21       2018-12-10 [3] CRAN (R 3.5.1)                 
#>  magick      * 2.0        2018-10-05 [1] CRAN (R 3.5.1)                 
#>  magrittr      1.5        2014-11-22 [3] CRAN (R 3.5.0)                 
#>  memoise       1.1.0      2017-04-21 [3] CRAN (R 3.5.0)                 
#>  mime          0.6        2018-10-05 [3] CRAN (R 3.5.1)                 
#>  pkgbuild      1.0.2      2018-10-16 [3] CRAN (R 3.5.1)                 
#>  pkgload       1.0.2      2018-10-29 [3] CRAN (R 3.5.1)                 
#>  png           0.1-7      2013-12-03 [3] CRAN (R 3.5.0)                 
#>  prettyunits   1.0.2      2015-07-13 [1] CRAN (R 3.5.1)                 
#>  processx      3.2.1      2018-12-05 [3] CRAN (R 3.5.1)                 
#>  ps            1.3.0      2018-12-21 [3] CRAN (R 3.5.2)                 
#>  R6            2.4.0      2019-02-14 [1] CRAN (R 3.5.2)                 
#>  Rcpp          1.0.0      2018-11-07 [1] CRAN (R 3.5.1)                 
#>  remotes       2.0.2      2018-10-30 [1] CRAN (R 3.5.2)                 
#>  rlang         0.3.1      2019-01-08 [1] CRAN (R 3.5.2)                 
#>  rmarkdown     1.11       2018-12-08 [1] CRAN (R 3.5.1)                 
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.5.1)                 
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.5.1)                 
#>  stringi       1.3.1      2019-02-13 [3] CRAN (R 3.5.2)                 
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 3.5.2)                 
#>  testthat      2.0.1      2018-10-13 [1] CRAN (R 3.5.1)                 
#>  usethis       1.4.0      2018-08-14 [1] CRAN (R 3.5.1)                 
#>  withr         2.1.2      2018-03-15 [3] CRAN (R 3.5.0)                 
#>  xfun          0.5.1      2019-02-23 [1] Github (yihui/xfun@d547831)    
#>  xml2          1.2.0      2018-01-24 [3] CRAN (R 3.5.0)                 
#>  yaml          2.2.0      2018-07-25 [3] CRAN (R 3.5.1)                 
#> 
#> [1] /home/robin/R/x86_64-pc-linux-gnu-library/3.5
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library
2
  • I think what you are looking for is a mask band or alpha channel - gis.stackexchange.com/a/24822/2856
    – user2856
    Commented Feb 28, 2019 at 11:50
  • Not sure but think I may have found an answer. Will post below, not sure if that's the best way... Commented Feb 28, 2019 at 12:02

2 Answers 2

3

The answer below applies to the edited question

You need to make your NoData value consistent across the bands

gdalbuildvrt -ot byte -srcnodata "255 0 0" -vrtnodata "0 0 0" output.vrt input.tif

Then convert to Byte datatype (as gdal2tiles.py doesn't seem to handle Float32 very well)

gdal_translate -ot Byte output.vrt output2.vrt

Then tile it:

gdal2tiles.py -p mercator -z 5-14 -w all -r average -a 255 output2.vrt output

enter image description here


The answer below is correct for the original unedited question which has since changed

You can use an alpha channel/mask band:

gdalwarp -srcnodata "255 0 0"  -dstalpha  -co "ALPHA=YES" input.tif output.tif
3
  • Thanks - tried that also but from a tiling perspective it still does not show as transparent - the same as my answer. Commented Feb 28, 2019 at 12:19
  • 1
    @RobinLovelace, you asked about creating a tiff not creating tiles. PNG can store an alpha channel, so perhaps ask a new question with details about how you are creating the tiles.
    – user2856
    Commented Feb 28, 2019 at 12:25
  • Thank for the suggestion, I've updated the question accordingly! Commented Feb 28, 2019 at 12:35
1

Update, based on another SO question I tried this, and think it may have solved it:

gdalwarp -dstalpha -srcnodata "255 0 0" -co "ALPHA=YES" dutch_slc-10m-merge2-test.tif "raster_w-alpha.tif"

How to calculate a new raster based on nodata values from original raster, in QGIS?

The problem with this approach: when I tile it, the tiles are still are still not transparent.

Here is the result, containing leaflet.html which shows that the transparency still is not there: https://github.com/mem48/pct-raster-tests/files/2914883/raster_w-alpha.zip

Screenshot of the tiled result in that zip file generated by the above command (and the command suggested by another post):

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.