1

When I plot aerial photographs in mapview a black rectangle is displayed around the picture, which I believe contains NA values and which I would like to remove. Due to the lack of easily available example data I have uploaded my actual geotif here (password: 1_sillyQuestion).

test_image <- terra::rast("yourpath\\test-image.tif") %>% 
  satellite::brick()

viewRGB(test_image,
    r = 1,
    g = 2,
    b = 3,
    quantiles = NULL)

enter image description here

Using data provided with the plainview package, I am able to remove the grey area surrounding the raster (which I think is analogous to my issue) by setting the na.color argument to "transparent".

Border present:

mapview::viewRGB(plainview::poppendorf, 4, 3, 2)

enter image description here

Border removed:

mapview::viewRGB(plainview::poppendorf, 4, 3, 2, na.color = "transparent")

enter image description here

Unfortunately this doesn't produce any changes, when applied to my problem:

viewRGB(gtiff_small,
    r = 1,
    g = 2,
    b = 3,
    quantiles = NULL,
    na.color = "transparent")  

Any suggestions about how to remove the black rectangle around my aerial image will be much appreciated!

M.Teich
  • 575
  • 5
  • 22
  • Your test-image lyr4 is presumably a mask as test-image[1,1,4] = 0 and [300,300, 4] = 255. – Chris Aug 10 '22 at 14:12
  • @Chris: thanks for your comment, but I'm not sure how to interpret it. Do you have a suggestion how to remove/make the black area transparent? It must be possible, as it displays fine in QGIS – M.Teich Aug 10 '22 at 15:33
  • I guess I'm just pointing out that you're interpreting the presence of the 4th lyr as though it were an alpha, values between 0:1, whereas they're not. Currently rebuilding my entire spatial stack so more later. – Chris Aug 10 '22 at 16:43

1 Answers1

3

na.color has no effect here because there aren't any NAs in the data. Your image is an RGB image where the black pixels are encoded as rgb(0, 0, 0) - black. The right thing to do is to specify a NA value upon the creation of the image (GDAL has an option to do this IIRC). Below is a solution that replaces all rgb(0, 0, 0) pixels with NA. But be careful, this will also set any real black pixels to NA (that's why it should really be done upon image creation).

library(mapview)
library(leaflet)
library(raster)
#> Loading required package: sp

test_image <- terra::rast("/home/tim/Downloads/test-image.tif") %>% 
  raster::brick() %>%
  subset(c(1, 2, 3))

# set all values with 0 to NA
vals = values(test_image)
idx = which(rowSums(vals) == 0)
vals[idx, ] = cbind(NA, NA, NA)

test_image = setValues(test_image, vals)

viewRGB(test_image,
        r = 1,
        g = 2,
        b = 3,
        na.color = "transparent",
        quantiles = NULL)
#> Warning in rasterCheckSize(x, maxpixels = maxpixels): maximum number of pixels for Raster* viewing is 5e+05 ; 
#> the supplied Raster* has 19132224 
#>  ... decreasing Raster* resolution to 5e+05 pixels
#>  to view full resolution set 'maxpixels =  19132224 '
#> Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
#> prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
#> +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
#> +wktext +no_defs +type=crs
#> Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
#> prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
#> Warning in raster::projectRaster(x, raster::projectExtent(x, crs =
#> sp::CRS(epsg3857)), : input and ouput crs are the same


test_image
#> class      : RasterBrick 
#> dimensions : 4016, 4764, 19132224, 3  (nrow, ncol, ncell, nlayers)
#> resolution : 0.1, 0.1  (x, y)
#> extent     : 1277489, 1277966, 6087025, 6087427  (xmin, xmax, ymin, ymax)
#> crs        : +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs 
#> source     : memory
#> names      : test.image_1, test.image_2, test.image_3 
#> min values :            0,            1,            0 
#> max values :          255,          255,          255

Created on 2022-08-11 by the reprex package (v2.0.1)

TimSalabim
  • 5,604
  • 1
  • 25
  • 36