Examples for 'stars::st_extract'


Extract cell values at point locations

Aliases: st_extract st_extract.stars

Keywords:

### ** 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    10   54.75   65.5 66.93333      85  130
dimension(s):
         from to                     refsys point
geometry    1 10 SIRGAS 2000 / UTM zone 25S  TRUE
band        1  6                         NA    NA
                                                      values
geometry POINT (292167 9115885),...,POINT (293355.5 9116196)
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: 289429.5 ymin: 9111162 xmax: 297880.3 ymax: 9118043
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              71             56             57             56             94
2              64             55             50             79             87
3              96             88             64             13             13
4              65             54             49             59             58
5              97             90             69             13             13
6              85             78             79             77            111
7             125            115            130             68            106
8              64             55             49             75             88
9              61             47             38             85             76
10             74             67             64             80            108
   L7_ETMs.tif.V6                 geometry
1              66   POINT (292167 9115885)
2              54   POINT (293764 9117544)
3              12 POINT (297378.9 9111918)
4              33 POINT (290152.9 9117895)
5              10 POINT (297880.3 9112533)
6              85 POINT (295168.3 9117460)
7              86 POINT (289429.5 9111162)
8              57 POINT (292023.5 9116278)
9              35   POINT (289516 9118043)
10             63 POINT (293355.5 9116196)
st_extract(r[,,,1], pnt)
Simple feature collection with 10 features and 1 field
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 289429.5 ymin: 9111162 xmax: 297880.3 ymax: 9118043
Projected CRS: SIRGAS 2000 / UTM zone 25S
   L7_ETMs.tif                 geometry
1           71   POINT (292167 9115885)
2           64   POINT (293764 9117544)
3           96 POINT (297378.9 9111918)
4           65 POINT (290152.9 9117895)
5           97 POINT (297880.3 9112533)
6           85 POINT (295168.3 9117460)
7          125 POINT (289429.5 9111162)
8           64 POINT (292023.5 9116278)
9           61   POINT (289516 9118043)
10          74 POINT (293355.5 9116196)
st_extract(r, st_coordinates(pnt)) # "at" is a matrix: return a matrix
      [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]   71   56   57   56   94   66
 [2,]   64   55   50   79   87   54
 [3,]   96   88   64   13   13   12
 [4,]   65   54   49   59   58   33
 [5,]   97   90   69   13   13   10
 [6,]   85   78   79   77  111   85
 [7,]  125  115  130   68  106   86
 [8,]   64   55   49   75   88   57
 [9,]   61   47   38   85   76   35
[10,]   74   67   64   80  108   63

[Package stars version 0.6-7 Index]