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

Julian M. Burgos ju||@n@burgo@ @end|ng |rom h@|ogv@tn@|@
Thu Feb 11 16:54:37 CET 2021


I get it.  Thank you for the explanation.

Edzer Pebesma writes:

> On 03/02/2021 20:14, Julian M. Burgos wrote:
>> Although if I use bind_cols the result is not an sf object....
>
> right, this is because bind_cols is not a generic (its first argument
> is ... , which you can't dispatch on) so doesn't have an associated sf
> method.
>
> dplyr offers a mechanism to work around this by providing an sf method
> for dplyr_reconstruct - something we could try, but then the dplyr
> authors mention that this is experimental and considered a stop-gap
> measure, so that might work now but stop working any time.
>
> However,
>
> sf3 <- cbind(st_extract(st1, sf1), st_drop_geometry(sf1))
>
> does return an sf object (note the changed order of arguments)
>
>> Julian M. Burgos writes:
>>
>>> Thanks Edzer, I should have checked if st_extract kept the order of the rows. In this case, a way to do it more generally is:
>>>
>>> sf1 <- bind_cols(st_drop_geometry(sf1),
>>>                   st_extract(st1, sf1))
>>>
>>>
>>>
>>> Edzer Pebesma writes:
>>>
>>>> st_join does a spatial join, which comes at a cost; since st_extract
>>>> doesn't change the row order of the sf object, you could as well use
>>>>
>>>> st_extract(st1, sf1) %>% mutate(nums = sf1$nums)
>>>>
>>>> On 03/02/2021 17:10, Julian M. Burgos wrote:
>>>>> 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
>>>>> http://www.hafogvatn.is/
>>>>> Sími/Telephone : +354-5752037
>>>>> Netfang/Email: julian.burgos using hafogvatn.is
>>>>>
>>>>> _______________________________________________
>>>>> R-sig-Geo mailing list
>>>>> R-sig-Geo using r-project.org
>>>>> https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=04%7C01%7C%7C3c716a56362547fd736b08d8c8f98e1e%7C8e105b94435e4303a61063620dbe162b%7C0%7C0%7C637480321457517463%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=j7fW5XZ7xsRpbhbcCobcfFdy%2BvZpmnL7OVoA%2FnQoihg%3D&reserved=0
>>>>>
>>>>
>>>> _______________________________________________
>>>> R-sig-Geo mailing list
>>>> R-sig-Geo using r-project.org
>>>> https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=04%7C01%7C%7C3c716a56362547fd736b08d8c8f98e1e%7C8e105b94435e4303a61063620dbe162b%7C0%7C0%7C637480321457517463%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=j7fW5XZ7xsRpbhbcCobcfFdy%2BvZpmnL7OVoA%2FnQoihg%3D&reserved=0
>>
>> --
>> 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
>> http://www.hafogvatn.is/
>> Sími/Telephone : +354-5752037
>> Netfang/Email: julian.burgos using hafogvatn.is
>>


--
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