R 星将 x/y 维度折叠为单个几何维度

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

我有一个星星对象,有 2 个“类似光栅”的网格尺寸(但没有投影),即 x 和 y。我还有一个多边形 sf 文件,其中包含一个字段,该字段包含多边形对应的 xy 元素 id 的组合(但将是它们的子集!)。我想重塑星形以删除 xy 尺寸并替换为单个几何尺寸。本质上相当于在星形数据与 xy 维度之间进行表连接。

这是一个表示:

# create a star
x = seq(1,5,1)
y = seq(1,8,1)
z = seq(1,3,1)
t = seq(1,4,1)

d <- st_dimensions(x=x, y=y,z=z,t=t)

values <- runif(length(x) * length(y) * length(z) * length(t))
data = array(values, dim = c(length(x),
                             length(y),
                             length(z),
                             length(t)))

star <- st_as_stars(data = data,  dimensions = d)
star

# create an arbitrary geometry of a subset of the number of x and y
nc = sf::read_sf(system.file("gpkg/nc.gpkg", package = "sf"), "nc.gpkg") |>
  head(0.5 * length(x) * length(y)) |>  # the number of polygons < total number of x * y grid elements
  select(geom) |>
  # add an x-y ID, these are what should be 'joined' with the star xy
   mutate(x = c(rep(1,4), rep(2,4), rep(3,4), rep(4,4), rep(5,4)),
         y = rep(c(1,3,5,8),5),
         label = paste0(x,"-",y))

我基本上需要提取 nc$label 列中星形的元素,并应用 nc 的几何形状制作一个新星形。

我尝试做类似的事情:

st.g <- star |> filter(x %in% c(1.5,2.5,3.5,4.5,5.5),
                       y %in% c(1.5,3.5,5.5,8.5)) 

(nc.geom <- st_set_dimensions(st.g, 1, st_geometry(nc)))

但我收到错误,因为尺寸不匹配。

知道如何通过匹配几何文件中存储的 xy 数据来减少恒星的 xy 尺寸吗?

r geospatial r-stars
1个回答
0
投票

这里讨论并回答了这个问题:https://github.com/r-spatial/stars/issues/654

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