在R个重叠的空间多边形数据框中spdf,并总结与第二个spdf重叠的第一个spdf的特征数量 因此,上述问题的完整答案应该是:

问题描述

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

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

# load 2nd spdf

将ibra polygons读取为sf对象。要在搜索项中下载粘贴“澳大利亚临时生物地理区域化(IBRA)”,然后单击“澳大利亚临时生物地理区域化(IBRA),版本7(地区)”

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)。任何建议/反馈将不胜感激。

解决方法

我已经找到解决方案。上述问题的最后一个代码部分应为:

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对象。要在搜索项中下载粘贴“澳大利亚临时生物地理区域化(IBRA)”,然后单击“澳大利亚临时生物地理区域化(IBRA),版本7(地区)”

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,-3] # print output as data frame.