在 R Overlap spatial polygons dataframe (spdf) 中总结了第一个 spdf 与第二个 spdf 重叠的特征数量

In R Overlap spatial polygons dataframe (spdf) and summarise number of features of 1st spdf overlapped by 2nd spdf

我尝试了几个来源,但没有成功。请在下面查看我的代码,我在代码末尾说明了问题。我在大面积上创建了随机六边形网格,并想总结其中有多少属于第二个空间多边形数据框的特征。

library (sf)
library(dplyr)
library(raster)

# load 2nd spdf

将 ibra polygons 读取为 sf 对象。要在搜索项中下载粘贴 'Interim Biogeographic Regionalisation for Australia (IBRA)' 然后单击 'Interim Biogeographic Regionalisation for Australia (IBRA), Version 7 (Regions)'

ibra <- st_read("ibra7_subregions.shp")
ibra <- st_transform(ibra, crs = 4326)

# ibra has >2000 features (i.e., rows) for 89 regions of same name, group them together
ibraGrid <- ibra %>%
  group_by(REG_NAME_7) %>%
  st_sf() %>%
  mutate(cellid = row_number()) %>%
  summarise()

colnames(ibraGrid)[1] <- "id"

# crop ibra to specific boundary
box <- extent(112,155,-45,-10)
ibraGrid <- st_crop(ibraGrid, box)


# make dataframe of spatial grid (1st spdf)
ran.p <- st_sample(au, size = 1040)

here 加载 au 的 shp,然后单击“nsaasr9nnd_02211a04es_geo___.zip”。

au <- st_read("aust_cd66states.shp")
au <- st_transform(au, crs = 4326)

# create grid around multipoints
rand_sampl_Grid <- ran.p %>%
  st_make_grid(cellsize = 0.1, square = F) %>%
  st_intersection(au) %>%
  st_cast("MULTIPOLYGON") %>%
  st_sf() %>%
  mutate(cellid = row_number())


# sampled grid per ibra region
density_per_ib_grid <- ibraGrid %>%
  st_join(rand_sampl_Grid) %>% 
  mutate(overlap = ifelse(!is.na(id), 1, 0)) %>%
  group_by(cellid) %>%
  summarize(num_sGrid = sum(overlap))

一切正常。但是,我预计 View(density_per_ib_grid$num_sGrid) 的长度将等于 ibraGrid 中的特征数(即 89)。目前,View(density_per_ib_grid$num_sGrid) 的特征长度等于 rand_sample_Grid(即 ~1040)。另外,我想重复这个过程 100 次,这样 num_sGrid 就是 100 次迭代的平均值。

上面的代码使用从坐标创建的更大的 spdf(在这个例子中是 ibraGrid)工作得很好。任何 suggestions/feedback 将不胜感激。

我想出了解决办法。上述问题中的最后一个代码部分应为:

richness_per_ib_grid <- st_intersection(ibraGrid, rand_sampl_Grid) %>% 
  group_by(id) %>%
  count()

out <- as.data.frame(int.result)[,-3] # print output as data frame.

因此,上述问题的完整答案应该是:

library (sf)
library(dplyr)
library(raster)

# load 2nd spdf

将 ibra polygons 读取为 sf 对象。要在搜索项中下载粘贴 'Interim Biogeographic Regionalisation for Australia (IBRA)' 然后单击 'Interim Biogeographic Regionalisation for Australia (IBRA), Version 7 (Regions)'

ibra <- st_read("ibra7_subregions.shp")
ibra <- st_transform(ibra, crs = 4326)

# ibra has >2000 features (i.e., rows) for 89 regions of same name, group them together
ibraGrid <- ibra %>%
  group_by(REG_NAME_7) %>%
  st_sf() %>%
  summarise()

colnames(ibraGrid)[1] <- "id"

# crop ibra to specific boundary
box <- extent(112,155,-45,-10)
ibraGrid <- st_crop(ibraGrid, box)


# make dataframe of spatial grid (1st spdf)
ran.p <- st_sample(au, size = 1040)

here 加载 au 的 shp,然后单击“nsaasr9nnd_02211a04es_geo___.zip”。

au <- st_read("aust_cd66states.shp")
au <- st_transform(au, crs = 4326)

# create grid around multipoints
rand_sampl_Grid <- ran.p %>%
  st_make_grid(cellsize = 0.1, square = F) %>%
  st_intersection(au) %>%
  st_cast("MULTIPOLYGON") %>%
  st_sf()


# sampled grid per ibra region
density_per_ib_grid <- <- st_intersection(ibraGrid, rand_sampl_Grid) %>% 
  group_by(id) %>%
  count()

out <- as.data.frame(int.result)[,-3] # print output as data frame.