riatelab / cartography

Thematic Cartography for R
http://riatelab.github.io/cartography/docs/
398 stars 46 forks source link

Add alpha channel to pngs #78

Closed dieghernan closed 3 years ago

dieghernan commented 3 years ago

The current implementation of getTiles and getPngLayer does not account for pngs with transparency. I have made an implementation here, basically assessing if the raw png has a fourth channel (alpha channel) and if so, replacing the RGB values for NA before rasterising.

https://github.com/dieghernan/cartography/blob/646d2d502fd4f5fb5e2409ce41321120dae8e94f/R/getTiles.R#L222:L233

https://github.com/dieghernan/cartography/blob/646d2d502fd4f5fb5e2409ce41321120dae8e94f/R/getPngLayer.R#L41:L71

Check with


library(sf)
library(cartography)

  mtq <-
    st_read(system.file("gpkg/mtq.gpkg", package = "cartography"))
  mtq <- st_transform(mtq, 3857)
  mtqTerrain <- getTiles(mtq, type = "Stamen.TerrainBackground")
  mtqlabels <- getTiles(mtq, type = "CartoDB.PositronOnlyLabels")

  par(mar = c(0, 0, 0, 0))
  tilesLayer(mtqTerrain)
  tilesLayer(mtqlabels, add = TRUE)
rCarto commented 3 years ago

Thanks, the fix to getTiles() is used in maptiles (riatelab/maptiles#2)