2. stars proxy objects

Edzer Pebesma

For a better version of the stars vignettes see https://r-spatial.github.io/stars/articles/

When your imagery or array data easily fits a couple of times in R’s working memory (RAM), consider yourself lucky. This document was not written for you. If your imagery is too large, or for other reasons you want to work with smaller chunks of data than the files in which they come, read on about your options. First, we will discuss the low-level interface for this, then the higher level, using stars proxy objects that delay all reading.

Preamble: the starsdata package

To run all of the examples in this vignette, you must install a package with datasets that are too large (1 Gb) to be held in the stars package. They are in a drat repo, installation is done by

install.packages("starsdata", repos = "http://gis-bigdata.uni-muenster.de", type = "source") 

Reading chunks, change resolution, select bands

read_stars has an argument called RasterIO which controls how a GDAL dataset is being read. By default, all pixels and all bands are read in memory. This can consume a lot of time and require a lot of memory. Remember that your file may be compressed, and that pixel values represented in the file by bytes are converted to 8-byte doubles in R.

The reason for using RasterIO for this is that the parameters we use are directly mapped to the GDAL RasterIO function used (after adapting the 1-based offset index in R to 0-based offset in C++).

Reading a particular chunk

An example of using RasterIO is

Compare this to

and we see that

Reading at a different resolution

Reading datasets at a lower (but also higher!) resolution can be done by setting nBufXSize and nBufYSize

and we see that in addition:

We can also read at higher resolution; here we read a 3 x 3 area and blow it up to 100 x 100:

The reason we “see” only three grid cells is that the default sampling method is “nearest neighbour”. We can modify this by

The following methods are allowed for parameter resample:

resample method used
nearest_neighbour Nearest neighbour (default)
bilinear Bilinear (2x2 kernel)
cubic Cubic Convolution Approximation (4x4 kernel)
cubic_spline Cubic B-Spline Approximation (4x4 kernel)
lanczos Lanczos windowed sinc interpolation (6x6 kernel)
average Average
mode Mode (selects the value which appears most often of all the sampled points)
Gauss Gauss blurring

All these methods are implemented in GDAL; for what these methods exactly do, we refer to the GDAL documentation or source code.

Stars proxy objects

Stars proxy objects take another approach: upon creation they contain no data at all, but only pointers to where the data can be read. Data is only read when it is needed, and only as much as is needed: if we plot a proxy objects, the data are read at the resolution of pixels on the screen, rather than at the native resolution, so that if we have e.g. a 10000 x 10000 Sentinel 2 (level 1C) image, we can open it by

granule = system.file("sentinel/S2A_MSIL1C_20180220T105051_N0206_R051_T32ULE_20180221T134037.zip", package = "starsdata")
s2 = paste0("SENTINEL2_L1C:/vsizip/", granule, "/S2A_MSIL1C_20180220T105051_N0206_R051_T32ULE_20180221T134037.SAFE/MTD_MSIL1C.xml:10m:EPSG_32632")
(p = read_stars(s2, proxy = TRUE))
## stars_proxy object with 1 attribute in file:
## $`MTD_MSIL1C.xml:10m:EPSG_32632`
## [1] "[...]/MTD_MSIL1C.xml:10m:EPSG_32632"
## 
## dimension(s):
##      from    to offset delta                refsys point    values x/y
## x       1 10980  3e+05    10 WGS 84 / UTM zone 32N    NA      NULL [x]
## y       1 10980  6e+06   -10 WGS 84 / UTM zone 32N    NA      NULL [y]
## band    1     4     NA    NA                    NA    NA B4,...,B8

and this happens instantly, because no data is read. When we plot this object,

system.time(plot(p))

##    user  system elapsed 
##   0.910   0.259   0.253

This takes only around 1 second, since only those pixels are read that can be seen on the plot. If we read the entire image in memory first, as we would do with

p = read_stars(s2, proxy = FALSE)

then only the reading would take over a minute, and require 5 Gb memory.

Methods for stars proxy objects

Select attributes

We can select attributes as with regular stars objects, by using the first argument to [:

Note that this selection limits the reading from 4 to 1 subdataset from all 9 NetCDF files.

Select an area

Another possibility is to crop, or select a rectangular region based on a spatial object. This can be done by passing a bbox object, or an sf, sfc or stars object from which the bounding box will be taken. An example:

Lazy evaluation, changing evaluation order

Some other actions can be carried out on stars_proxy objects, but their effect is delayed until the data are actually needed (plot, write_stars). For instance, range selections on dimensions other than shown above first need data, and can only then be carried out. Such functions are added to the object, in an attribute called call_list:

Doing this allows for optimizing the order in which operations are done. As an example, for st_apply, reading can be done sequentially over the dimensions over which the function is applied:

the order of evaluation needs to be reversed: only plot knows which pixels are going to be shown, so that should be in control of how x is subsampled before st_apply is carried out on this subsample.

Multi-resolution proxy objects

This sections shows some examples how stars_proxy objects deal with the situation where the different maps have dissimilar resolution. The assumptions here are:

We’ll create four maps with cells size 1, 2 and 3:

library(stars)

s1 = st_as_stars(matrix(1:16, 4))
s2 = st_as_stars(matrix(1:16, 4))
s3 = st_as_stars(matrix(1:16, 4))
attr(s1, "dimensions")$X2$delta = -1
attr(s2, "dimensions")$X1$delta =  2
attr(s2, "dimensions")$X2$delta = -2
attr(s3, "dimensions")$X1$delta =  3
attr(s3, "dimensions")$X2$delta = -3
plot(s1, axes = TRUE, text_values = TRUE, text_color = 'orange')

plot(s2, axes = TRUE, text_values = TRUE, text_color = 'orange')

plot(s3, axes = TRUE, text_values = TRUE, text_color = 'orange')

We created three rasters with identical cell values and dimensions, but different cell sizes, and hence extents. If we bind them in a single proxy object, with

write_stars(s1, "s1.tif")
write_stars(s2, "s2.tif")
write_stars(s3, "s3.tif")
(r1 = read_stars(c("s1.tif", "s2.tif", "s3.tif"), proxy = TRUE))
## multi-resolution stars_proxy object with 3 attributes in files:
## $s1.tif
## [1] "s1.tif"
## 
## $s2.tif
## [1] "s2.tif"
## 
## $s3.tif
## [1] "s3.tif"
## 
## dimension(s):
##   from to offset delta refsys point values x/y
## x    1  4      0     1     NA    NA   NULL [x]
## y    1  4      0    -1     NA    NA   NULL [y]

We see that multi-resolution is mentioned in the printed summary. When converting this to a stars object, the secondary rasters are resampled to the cellsize + extent of the first:

st_as_stars(r1) %>%
  merge() %>%
  plot(breaks = "equal", text_values = TRUE, text_color = 'orange', axes = TRUE)

If we do this for a sub-range, defined for the object resolutions, we get:

st_as_stars(r1[,2:4,2:4]) %>%
  merge() %>%
  plot(breaks = "equal", text_values = TRUE, text_color = 'orange', axes = TRUE)

We now create four maps, all over the same region ([0,4] x [0,4]), with different resolutions (cell size 1, 1/2 and 1/3):

s1 = st_as_stars(matrix(1: 16, 4))
s2 = st_as_stars(matrix(1: 64, 8))
s3 = st_as_stars(matrix(1:144,12))
attr(s1, "dimensions")$X2$delta = -1
attr(s2, "dimensions")$X1$delta =  1/2
attr(s2, "dimensions")$X2$delta = -1/2
attr(s3, "dimensions")$X1$delta =  1/3
attr(s3, "dimensions")$X2$delta = -1/3
plot(s1, axes = TRUE, text_values = TRUE, text_color = 'orange')

plot(s2, axes = TRUE, text_values = TRUE, text_color = 'orange')

plot(s3, axes = TRUE, text_values = TRUE, text_color = 'orange')

write_stars(s1, "s1.tif")
write_stars(s2, "s2.tif")
write_stars(s3, "s3.tif")
(r2 = read_stars(c("s1.tif", "s2.tif", "s3.tif"), proxy = TRUE))
## multi-resolution stars_proxy object with 3 attributes in files:
## $s1.tif
## [1] "s1.tif"
## 
## $s2.tif
## [1] "s2.tif"
## 
## $s3.tif
## [1] "s3.tif"
## 
## dimension(s):
##   from to offset delta refsys point values x/y
## x    1  4      0     1     NA    NA   NULL [x]
## y    1  4      0    -1     NA    NA   NULL [y]

st_as_stars(r2) %>%
  merge() %>%
  plot(breaks = "equal", text_values = TRUE, text_color = 'orange', axes = TRUE)

st_as_stars(r2[,2:4,2:4]) %>%
  merge() %>%
  plot(breaks = "equal", text_values = TRUE, text_color = 'orange', axes = TRUE)

Finally, an example where the first raster has the higher resolution:

(r3 = read_stars(c("s3.tif", "s2.tif", "s1.tif"), proxy = TRUE))
## multi-resolution stars_proxy object with 3 attributes in files:
## $s3.tif
## [1] "s3.tif"
## 
## $s2.tif
## [1] "s2.tif"
## 
## $s1.tif
## [1] "s1.tif"
## 
## dimension(s):
##   from to offset     delta refsys point values x/y
## x    1 12      0  0.333333     NA    NA   NULL [x]
## y    1 12      0 -0.333333     NA    NA   NULL [y]

st_as_stars(r3) %>%
  merge() %>%
  plot(breaks = "equal", text_values = TRUE, text_color = 'orange', axes = TRUE)

st_as_stars(r3[,2:6,3:6]) %>%
  merge() %>%
  plot(breaks = "equal", text_values = TRUE, text_color = 'orange', axes = TRUE)