Skip to contents

Extract cell values at point locations

Usage

st_extract(x, ...)

# S3 method for stars
st_extract(
  x,
  at,
  ...,
  bilinear = FALSE,
  time_column = attr(at, "time_column") %||% attr(at, "time_col"),
  interpolate_time = bilinear,
  FUN = mean
)

Arguments

x

object of class stars or stars_proxy

...

passed on to aggregate.stars when geometries are not exclusively POINT geometries

at

object of class sf or sfc with geometries, or two-column matrix with coordinate points in rows, indicating where to extract values of x

bilinear

logical; use bilinear interpolation rather than nearest neighbour?

time_column

character or integer; name or index of a column with time or date values that will be matched to values of the first temporal dimension (matching classes POSIXct, POSIXt, Date, or PCICt), in x, after which this dimension is reduced. This is useful to extract data cube values along a trajectory; see https://github.com/r-spatial/stars/issues/352 .

interpolate_time

logical; should time be interpolated? if FALSE, time instances are matched using the coinciding or the last preceding time in the data cube.

FUN

function used to aggregate pixel values when geometries of at intersect with more than one pixel

Value

if at is of class matrix, a matrix with extracted values is returned; otherwise: if x has more dimensions than only x and y (raster), an object of class stars with POINT geometries replacing x and y raster dimensions, if this is not the case, an object of sf with extracted values.

Details

points outside the raster are returned as NA values. For large sets of points for which extraction is needed, passing a matrix as to at may be much faster than passing an sf or sfc object.

Examples

tif = system.file("tif/L7_ETMs.tif", package = "stars")
r = read_stars(tif)
pnt = st_sample(st_as_sfc(st_bbox(r)), 10)
st_extract(r, pnt)
#> stars object with 2 dimensions and 1 attribute
#> attribute(s):
#>              Min. 1st Qu. Median     Mean 3rd Qu. Max.
#> L7_ETMs.tif    12   60.75     74 72.51667      87  150
#> dimension(s):
#>          from to                     refsys point
#> geometry    1 10 SIRGAS 2000 / UTM zone 25S  TRUE
#> band        1  6                         NA    NA
#>                                                         values
#> geometry POINT (298340.2 9114943),...,POINT (289531.4 9111471)
#> band                                                      NULL
st_extract(r, pnt) %>% st_as_sf()
#> Simple feature collection with 10 features and 6 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 288950.3 ymin: 9111189 xmax: 298340.2 ymax: 9119338
#> Projected CRS: SIRGAS 2000 / UTM zone 25S
#>    L7_ETMs.tif.V1 L7_ETMs.tif.V2 L7_ETMs.tif.V3 L7_ETMs.tif.V4 L7_ETMs.tif.V5
#> 1              97             88             67             14             13
#> 2              82             66             74             49            107
#> 3              66             54             46             73             79
#> 4              80             68             69             77            117
#> 5              87             85            104             87            120
#> 6              90             83             65             13             13
#> 7              63             46             38             65             83
#> 8             110            101            114             74            150
#> 9              80             68             74             54            110
#> 10             80             65             65             44             84
#>    L7_ETMs.tif.V6                 geometry
#> 1              12 POINT (298340.2 9114943)
#> 2              82 POINT (293918.4 9114415)
#> 3              42 POINT (293485.2 9118749)
#> 4              86 POINT (294440.9 9114839)
#> 5              79   POINT (295209 9118813)
#> 6              12 POINT (295048.9 9111189)
#> 7              50 POINT (289649.4 9116888)
#> 8             128 POINT (295730.3 9119338)
#> 9              88 POINT (288950.3 9111816)
#> 10             71 POINT (289531.4 9111471)
st_extract(r[,,,1], pnt)
#> Simple feature collection with 10 features and 1 field
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 288950.3 ymin: 9111189 xmax: 298340.2 ymax: 9119338
#> Projected CRS: SIRGAS 2000 / UTM zone 25S
#>    L7_ETMs.tif                 geometry
#> 1           97 POINT (298340.2 9114943)
#> 2           82 POINT (293918.4 9114415)
#> 3           66 POINT (293485.2 9118749)
#> 4           80 POINT (294440.9 9114839)
#> 5           87   POINT (295209 9118813)
#> 6           90 POINT (295048.9 9111189)
#> 7           63 POINT (289649.4 9116888)
#> 8          110 POINT (295730.3 9119338)
#> 9           80 POINT (288950.3 9111816)
#> 10          80 POINT (289531.4 9111471)
st_extract(r, st_coordinates(pnt)) # "at" is a matrix: return a matrix
#>       [,1] [,2] [,3] [,4] [,5] [,6]
#>  [1,]   97   88   67   14   13   12
#>  [2,]   82   66   74   49  107   82
#>  [3,]   66   54   46   73   79   42
#>  [4,]   80   68   69   77  117   86
#>  [5,]   87   85  104   87  120   79
#>  [6,]   90   83   65   13   13   12
#>  [7,]   63   46   38   65   83   50
#>  [8,]  110  101  114   74  150  128
#>  [9,]   80   68   74   54  110   88
#> [10,]   80   65   65   44   84   71