我正在将代码从sp包迁移到较新的sf包。我以前的代码中有一个多边形SpatialDataFrame(censimentoMap)和一个SpatialPointDataFrame(indirizzi.sp),并使用以下指令为放置在其中的每个点获取了多边形像元ID(“ Cell110”):

points.data <- over(indirizzi.sp, censimentoMap[,"Cell110"])

实际上我创建了两个sf对象:以上说明中的内容...
应该是:

shape_sf <- st_read(dsn = shape_dsn) shape_sf <- st_transform(x=shape_sf, crs=crs_string)

#1 楼

您可以使用st_join获得相同的结果:
首先创建一个演示多边形并使用sf标记一些点。对象

library(sf)
library(magrittr)

poly <- st_as_sfc(c("POLYGON((0 0 , 0 1 , 1 1 , 1 0, 0 0))")) %>% 
  st_sf(ID = "poly1")    

pts <- st_as_sfc(c("POINT(0.5 0.5)",
                   "POINT(0.6 0.6)",
                   "POINT(3 3)")) %>%
  st_sf(ID = paste0("point", 1:3))


现在等同于sf st_join

over(as(pts, "Spatial"), as(polys, "Spatial"))
>#      ID
># 1 poly1
># 2 poly1
># 3  <NA>


或完全相同的结果

st_join(pts, poly, join = st_intersects)
># Simple feature collection with 3 features and 2 fields
># geometry type:  POINT
># dimension:      XY
># bbox:           xmin: 0.5 ymin: 0.5 xmax: 3 ymax: 3
># epsg (SRID):    NA
># proj4string:    NA
>#     ID.x  ID.y               .
># 1 point1 poly1 POINT (0.5 0.5)
># 2 point2 poly1 POINT (0.6 0.6)
># 3 point3  <NA>     POINT (3 3)