我正在尝试获取两个 shapefile 的交集(位于某些大都市区边界内的人口普查区域)。我能够成功获得相交特征,但是当我尝试将 sf_intersect 的输出转换为 SpatialPolygonsDataframe 时,出现错误:
“as_Spatial(from) 中的错误:不支持从要素类型 sfc_GEOMETRY 到 sp 的转换”
这是我的代码:
library(sf)
library(dplyr)
library(tigris)
library(sp)
#download shapefiles corresponding to metro areas
metro_shapefiles<-core_based_statistical_areas(cb = FALSE, year = 2016)
#convert to sf and filter
metro_shapefiles<-st_as_sf(metro_shapefiles)%>%filter(GEOID==31080 )
#Data for California
census_tracts_california<-tracts(state="CA",year=2016)
census_tracts_california<-st_as_sf(census_tracts_california)
#INTERSECT AND CONVERT BACK TO SP
census_tracts_intersected1<-st_intersection(census_tracts_california,
metro_shapefiles)
#back to spatial
census_tracts_intersected1<-as(census_tracts_intersected1,"Spatial")