[R-sig-Geo] Does stars::st_extract preserve records order

Ben Tupper btupper @end|ng |rom b|ge|ow@org
Wed Jul 27 14:36:28 CEST 2022


Hi,

I have never observed st_extract() fail to preserve row order.

Your workflow works well.  The documentation for st_extract() mentions
that if you might see better performance for large sets of points if
you pass `at` locations as a matrix instead as sf.  I don't think it
makes it any more or less elegant.  Such an approach might look like
this...

### start

# Set column names of matrix/data.frame to months
set_column_names <- function(x) {
  colnames(x) <- month.abb
  x
}

# Extract monthly avg temperature for cities
tavg_df <- st_extract(tavg, at = sf::st_coordinates(sfdf)) %>%  # <- pass matrix
  set_column_names() %>%
       # <- name the columns
  dplyr::as_tibble()
                    # <-cast as data frame

# Recombine with original sf dataframe
full_df <- cbind(sfdf, tavg_df)

# Simple feature collection with 3 features and 13 fields
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: 2.3522 ymin: 43.2965 xmax: 5.3698 ymax: 48.8566
# Geodetic CRS:  WGS 84
#       names Jan Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov Dec
# 1     Paris 4.8 5.4  8.2 10.6 14.8 17.6 20.0 20.0 16.5 12.3  7.8 5.7
# 2 Marseille 8.6 9.2 11.1 13.3 17.3 20.7 23.6 23.4 20.3 16.4 11.7 9.5
# 3      Lyon 3.6 5.1  8.1 10.8 15.4 18.7 21.7 21.2 17.2 12.5  7.2 4.5
#                 geometry
# 1 POINT (2.3522 48.8566)
# 2 POINT (5.3698 43.2965)
# 3  POINT (4.8357 45.764)

### end

Cheers,
Ben

On Wed, Jul 27, 2022 at 5:54 AM DUTRIEUX Loic
<Loic.DUTRIEUX using ec.europa.eu> wrote:
>
> Hi everyone,
>
> I'm wondering whether st_extract() from stars package preserves the order of the input sf dataframe.
> See the example below; is it a safe approach? Is there perhaps a more "elegant" way to do the same extraction?
>
> library(sf)
> library(stars)
> library(geodata)
> library(magrittr)
>
> # Create sf dataframe of cities
> cities <- data.frame(x=c(2.3522, 5.3698, 4.8357),
>                      y=c(48.8566, 43.2965, 45.7640),
>                      names=c('Paris', 'Marseille', 'Lyon'))
> sfdf <- st_as_sf(cities,
>                  coords=c('x','y'),
>                  crs=st_crs(4326),
>                  agr='identity')
>
> # Get raster of monthly average tempature
> tavg <- worldclim_tile(var = 'tavg', lon = 15, lat = 45, path = tempdir()) %>%
>     st_as_stars()
>
> # Extract monthly avg temperature for cities
> tavg_df <- st_extract(tavg, at = sfdf) %>%
>     st_as_sf() %>%
>     st_drop_geometry()
>
> # Recombine with original sf dataframe
> full_df <- cbind(sfdf, tavg_df)
>
>
> Kind regards,
> Loïc
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



-- 
Ben Tupper (he/him)
Bigelow Laboratory for Ocean Science
East Boothbay, Maine
http://www.bigelow.org/
https://eco.bigelow.org



More information about the R-sig-Geo mailing list