r-spatial / stars

Spatiotemporal Arrays, Raster and Vector Data Cubes
https://r-spatial.github.io/stars/
Apache License 2.0
554 stars 94 forks source link

Subset stars based on attribute value #178

Closed florisvdh closed 3 years ago

florisvdh commented 5 years ago

I hope this is a sensible question; I'm yet starting with stars, which looks very promising to me. Maybe I still overlooked an available solution for my question.

In the raster package one can produce a raster subset based on the cell values (for x and y dimensions at once), e.g. :

rasterobj[rasterobj > 10, drop = FALSE]

It sets the non-conforming cells to NA.

Further, if rasterobj has pure 'NA' rows and/or columns at the edges, the following effectively crops these and reduces the extent:

rasterobj[!is.na(rasterobj), drop = FALSE]

My case at hand is the last one. I could not find an equivalent 'subsetting' application in stars for this purpose, but maybe you can help.

I've looked into the migration table but saw no specific mention.

edzer commented 5 years ago

See section 4.2.3 of http://r-spatial.org/book/ for masking out areas based on attribute(s). There is (currently) no auto-crop based on NA edges; you can use st_crop if you have a geometry of the crop area.

florisvdh commented 5 years ago

Thanks for the quick response and for the pointers, I will study this further! As this is for a workflow on several rasters where the NA edges cannot be easily predicted, I think I'll have to convert forth and back to/from raster.

I'm using stars now because it is successful in storing the rasters as subsets in a geopackage and because of its efficient and versatile polygonization and rasterization - that's so great!

edzer commented 5 years ago

See #176 for raster round tripping, but that approach might fail if the size of the returned sub-array changes randomly.

florisvdh commented 5 years ago

OK, thanks for this interesting example. (FYI, a rough prototype of the workflow, i.e. on a subset of the whole RasterBrick, is in here, but for further implementation I'll instead convert back to stars in order to write each layer as a geopackage subset, as prototyped further).