我正在尝试获取两个形状文件的交集(位于某些大都市区边界内的人口普查区域)。我能够成功获得相交特征,但是当我尝试将 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")
错误消息告诉您无法将
sfc_GEOMETRY
转换为 Spatial
对象。没有 sp
等效对象。
在相交结果中,您有多种几何图形(因此,您将返回
sfc_GEOMETRY
作为“几何图形”)。您可以在这里看到所有几何形状:
types <- vapply(sf::st_geometry(census_tracts_intersected1), function(x) {
class(x)[2]
}, "")
unique(types)
# [1] "POLYGON" "MULTILINESTRING" "MULTIPOLYGON"
如果需要,您可以提取每种类型的几何图形,并将它们单独转换为 SP:
lines <- census_tracts_intersected1[ grepl("*LINE", types), ]
polys <- census_tracts_intersected1[ grepl("*POLYGON", types), ]
spLines <- as(lines, "Spatial")
spPolys <- as(polys, "Spatial")
我在评论中提到你可以使用
st_join
。但是,这可能不会给您带来您想要的结果。在 sf
库中,有几何二元谓词,例如 ?st_intersects
,以及几何运算,例如 ?st_intersection
谓词返回一个稀疏(默认)或密集矩阵,告诉您 x 的每个几何图形与 y 的哪个几何图形相交。如果您在
st_join
内使用它,它将返回相交的(原始)几何图形,而不是稀疏矩阵。
而操作(例如
st_intersection
)将计算交集,并返回新的几何图形。
谓词(
st_intersects
)可以在st_join
内部使用,它们将返回“相交”的原始几何图形
sf_join <- sf::st_join(census_tracts_california, metro_shapefiles, join = st_intersects)
在这种情况下,这给出了单个对象
type
types <- vapply(sf::st_geometry(sf_join), function(x) {
class(x)[2]
}, "")
unique(types)
# [1] "MULTIPOLYGON"
## so you can convert to a Spatial object
spPoly <- as(sf_join, "Spatial")
但是你需要决定
st_intersect
的结果是否是你想要的,或者你是否需要st_intersection
给出的新几何图形。
感谢用户 @lbussett 对
st_intersect
和 st_intersection
之间差异的描述
转换为
Spatial
对象无法处理混合线条和多边形。相交后,您可以使用以下方法仅提取多边形(并丢弃任何线):st_collection_extract("POLYGON")
您的示例似乎不再失败,因此我创建了一个与两个多边形相交且具有共享边的新示例。这会产生多边形和直线的交集输出。 在第二次尝试中,我在成功转换为
Spatial
对象之前通过 st_collection_extract() 函数传输了交集。
library(sf)
library(dplyr)
library(sp)
#Create some boxes
BoxA <- st_polygon(list(cbind(c(0,0,2,2,0),c(0,2,2,0,0))))
BoxB <- st_polygon(list(cbind(c(1,1,3,3,1),c(1,3,3,1,1))))
BoxC <- st_polygon(list(cbind(c(2,2,4,4,2),c(0,2,2,0,0))))
#Create a funny shaped union to help demonstrate the intersection issue
BoxAB <- st_union(BoxA,BoxB)
plot(BoxAB)
plot(BoxC,add=TRUE,border="blue")
#Intersect of BoxAB with BoxC results in a line and a polygon
BoxIntersects<-st_intersection(BoxAB,BoxC)
plot(BoxIntersects)
#back to spatial fails
SpatialVersionOfIntersects<-as(BoxIntersects,"Spatial")
.as_Spatial(from、cast、ID) 中的错误: 不支持从要素类型 sfc_GEOMETRY 到 sp 的转换
#Intersect again, but this time extract only the polygons
BoxIntersects<-st_intersection(BoxAB,BoxC) %>% st_collection_extract("POLYGON")
#back to spatial succeeds!
SpatialVersionOfIntersects<-as(BoxIntersects,"Spatial")