[R-sig-Geo] Spatial joins between sf and stars objects

Julian M. Burgos ju||@n@burgo@ @end|ng |rom h@|ogv@tn@|@
Wed Feb 3 17:10:58 CET 2021


Dear list,

What would be the best way to do a spatial join between a sf object (of POINT geometry) and a star object, so the sf object gets the values of the corresponding pixels in the star object?  I have done using a combination of st_join and st_extract, but it feels a bit clunky.  Is there a better way?

Here is what I have done:

## --------------------------------------------------------------
## Load a stars object
st1 <- read_stars(system.file("tif/L7_ETMs.tif", package = "stars")) %>%
  slice(band, 1)

## Create an sf object

set.seed(100)
bb <- st_bbox(st1)

sf1 <- tibble(lon = runif(n = 10, min = bb[1], max = bb[3]),
              lat = runif(n = 10, min = bb[2], max = bb[4]),
              nums = 1:10) %>%
  st_as_sf(coords = c("lon", "lat"), crs = st_crs(st1))

## Do the spatial join

sf1 <- st_extract(st1, sf1) %>%
    st_join(sf1)
## --------------------------------------------------------------

Thanks,

Julian

--
Julian Mariano Burgos, PhD
Hafrannsóknastofnun, rannsókna- og ráðgjafarstofnun hafs og vatna/
Marine and Freshwater Research Institute
Botnsjávarsviðs / Demersal Division
  Fornubúðir 5, IS-220 Hafnarfjörður, Iceland
www.hafogvatn.is
Sími/Telephone : +354-5752037
Netfang/Email: julian.burgos using hafogvatn.is



More information about the R-sig-Geo mailing list