1

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)

lovalery
  • 4,524
  • 3
  • 14
  • 28

1 Answers1

1

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.

Chris
  • 1,647
  • 1
  • 18
  • 25
  • Thank you so much @Chris for your reply. Thanks for the simplification of the code. I didn't know that I could directly use the st_as_sfc function on bbox class objects. It's much more efficient and elegant! As for the tracing order (i.e. bbox and then stars, and not the other way around), I would never have found it without your help. So a big thank you for that. To tell the truth, I don't understand the logic behind it: for me, the order should not affect the graphic result. Maybe you know the reason, or maybe someone else (maybe @edzer) can shed some light on this. – lovalery Aug 04 '21 at 15:01
  • I came upon the plotting solution 'on accident' as I wondered am I even getting a bbox to plot, and plotted it independent of tif. Et viola, a rectangle. Then added the tif, and again viola. The shortcut to the bbox object came from a close read of @edzer's very good, comprehensive documentation. Glad this all worked and still can't really explain why. – Chris Aug 04 '21 at 15:33
  • Thanks Chris for your feedback. It's true that the documentation about stars and sf objects is very good but it seems that I probably read it less carefully than you ;-) So thanks again for your help. I keep my fingers crossed that @edzer or maybe someone else will explain the reason why the plotting order is important. As it stands, I agree with you, it remains a bit of mystery. – lovalery Aug 04 '21 at 21:27