Extract cell values at point locations

st_extract(x, ...)

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

Arguments

x

object of class stars or stars_proxy

...

ignored

pts

object of class sf or sfc with POINT geometries

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 dimension "time" 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.

Value

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; otherwise an object of sf with extracted values.

Details

points outside the raster are returned as NA values.

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 11 54 72.5 72.23333 92.5 139 #> dimension(s): #> from to offset delta refsys point #> geometry 1 10 NA NA UTM Zone 25, Southern Hem... TRUE #> band 1 6 NA NA NA NA #> values #> geometry POINT (293856.7 9116346),...,POINT (291157.3 9113704) #> 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: 289463 ymin: 9113485 xmax: 297550 ymax: 9119106 #> Projected CRS: UTM Zone 25, Southern Hemisphere #> L7_ETMs.tif.V1 L7_ETMs.tif.V2 L7_ETMs.tif.V3 L7_ETMs.tif.V4 L7_ETMs.tif.V5 #> 1 62 43 32 76 62 #> 2 83 73 79 61 120 #> 3 94 81 96 68 135 #> 4 61 49 39 78 68 #> 5 70 50 54 42 97 #> 6 72 61 67 56 116 #> 7 91 77 57 12 13 #> 8 96 79 81 54 103 #> 9 105 98 111 74 139 #> 10 62 50 36 89 70 #> L7_ETMs.tif.V6 geometry #> 1 29 POINT (293856.7 9116346) #> 2 96 POINT (289463 9115896) #> 3 116 POINT (297386.8 9119071) #> 4 36 POINT (289998.6 9118502) #> 5 75 POINT (291149.7 9113485) #> 6 92 POINT (290831.3 9115932) #> 7 11 POINT (297550 9114174) #> 8 91 POINT (293928.2 9113897) #> 9 114 POINT (295710.5 9119106) #> 10 32 POINT (291157.3 9113704)
st_extract(r[,,,1], pnt)
#> Simple feature collection with 10 features and 1 field #> Geometry type: POINT #> Dimension: XY #> Bounding box: xmin: 289463 ymin: 9113485 xmax: 297550 ymax: 9119106 #> Projected CRS: UTM Zone 25, Southern Hemisphere #> L7_ETMs.tif geometry #> 1 62 POINT (293856.7 9116346) #> 2 83 POINT (289463 9115896) #> 3 94 POINT (297386.8 9119071) #> 4 61 POINT (289998.6 9118502) #> 5 70 POINT (291149.7 9113485) #> 6 72 POINT (290831.3 9115932) #> 7 91 POINT (297550 9114174) #> 8 96 POINT (293928.2 9113897) #> 9 105 POINT (295710.5 9119106) #> 10 62 POINT (291157.3 9113704)