0

I have loaded a raster using stars package and then created tiles over the raster. Now i subset the raster based on these tile.

tiles[[i]] <- st_bbox(c(xmin=x0,ymin=y0,xmax=x1,ymax=y1),crs=st_crs(r))
crop_tiles[[i]] <- r[tiles[[i]]]

Here r is raster loaded using read_stars("filename.tif").

Now i want to remove among the crop_tiles the empty ones. i.e the tiles where the raster values are NA

rw_11
  • 11
  • 2

1 Answers1

0

You can check if all values of a stars object named r are NA with:

all(is.na(r[[1]]))

then remove those tiles where the above is equal to TRUE.

For more specific code, please provide reproducible sample data in your question, thanks.

Michael Dorman
  • 950
  • 6
  • 8
  • Does that also work the other way round? I have missing time steps in my data and need to fill in blank layers. – Joanne Demmler Dec 09 '21 at 14:47
  • @JoanneDemmler Interesting question! First thought would be to loop over the vector dates, for each date `i` check if it exist in your raster: if it does then keep it, if it doesn't then create an empty raster. In the end, combine all resulting bands. If you can post an example in a separate question I'll try to prepare sample code. – Michael Dorman Dec 09 '21 at 20:23