如何使用stars::st_as_stars函数将sf表转换为星星立方体?

问题描述 投票:0回答:1

我想将 sf data.frame 转换为星形立方体,并选择维度。 但我没有成功地正确使用函数 star::st_as_stars 并得到我想要的东西。 我为什么要改造它?避免数据中出现重复的空间多边形(请参阅下面的

sf_dta
)。

在这个例子中,我想要:

  • 以下维度:几何形状、工作日和时间。
  • 以下属性:人口。

请帮忙:)

library(sf)
library(stars)
library(dplyr)

# Create example spatial data --------------------------------------------------

spatial_dim <- st_sf(
  ID = 1:3, 
  geometry = list(
    st_polygon(list(
      cbind(c(0, 1, 1, 0, 0), c(0, 0, 1, 1, 0))
    )),
    st_polygon(list(
      cbind(c(1, 2, 2, 1, 1), c(0, 0, 1, 1, 0))
    )),
    st_polygon(list(
      cbind(c(2, 3, 3, 2, 2), c(0, 0, 1, 1, 0))
    ))
  )
)

weekdays_dim <- data.frame(weekdays = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
hours_dim <- data.frame(hours = c("8am", "11am", "4pm", "11pm"))
sf_dta <- spatial_dim |> 
  cross_join(weekdays_dim)|> 
  cross_join(hours_dim) |> 
  mutate(population = rnorm(n(), mean = 1000, sd = 200)) |> 
  select(everything(), geometry)


# Try to transform as a stars object with dimensions : geometry, weekdays, hours -----

st_as_stars(sf_dta, name = attr(dta, "sf_column"), dims = c('jour','geometry'))

不幸的是,这段代码返回一个错误:

Error in st_as_stars.sf(sf_dta, name = attr(dta, "sf_column"), dims = c("jour",  :  ... arguments ignored

r vector spatial r-sf r-stars
1个回答
0
投票

这似乎有效:

st_as_stars(as.data.frame(sf_dta), dims = c("weekdays", "hours", "geometry")) |>
   st_set_dimensions(xy = NA)

但肯定还有一些改进的空间,可以使其更加直观。

© www.soinside.com 2019 - 2024. All rights reserved.