rr-sfr-stars

Bbox of stars object smaller than stars object itself?


When I plot the bbox of a "stars" object, the bbox is smaller than the object itself. Probably an error in my code, but I don't know where...

In fact, two issues in one, the second one being unexpected because it happened while I was preparing my reprex (see below): while the bbox is plotted in the 'Plots pane', it does not appear on the image from the REPREX. Maybe a problem with add=TRUE...

I would be very grateful if someone could help me with these two issues.

Here is the REPREX:

library(stars)
#> Le chargement a nécessité le package : abind
#> Le chargement a nécessité le package : sf
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(sf)

tif <- system.file("tif/L7_ETMs.tif", package = "stars")
tif <- read_stars(tif)

# 1 - plotting the first band of stars object
plot(tif[,,,1], main = NULL, key.pos = NULL)
#> downsample set to c(0,0,1)

# 2 - plotting the bbox of the raster
plot(st_geometry(st_sfc(st_polygon(list(cbind(c(st_bbox(tif)[[1]],
                                                st_bbox(tif)[[3]],
                                                st_bbox(tif)[[3]],
                                                st_bbox(tif)[[1]],
                                                st_bbox(tif)[[1]]),
                                              c(st_bbox(tif)[[2]],
                                                st_bbox(tif)[[2]],
                                                st_bbox(tif)[[4]],
                                                st_bbox(tif)[[4]],
                                                st_bbox(tif)[[2]])))),
                        crs = st_crs(tif))),
     border = "red", lwd = 5, add = TRUE)

Created on 2021-08-03 by the reprex package (v2.0.0)


Solution

  • I think you just need to change the order of plotting, and you can additionally simplify your bbox construction:

    library(stars)
    library(sf)
    tif <- system.file("tif/L7_ETMs.tif", package = "stars")
    tif <- read_stars(tif)
    plot(st_as_sfc(st_bbox(tif)), border = 'red', lwd = 5)
    plot(tif[,,,1], main = NULL, key.pos = NULL, add = TRUE)
    
    

    I experienced the same head scratch with the reversed plotting order.