You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何对两个sf对象执行相交操作以筛选含/不含建筑物的地块?

你之前使用st_intersection()不符合需求的原因是该函数返回的是两个图层相交的几何片段,而非原始的完整地块对象,要实现筛选需求,使用sf的空间谓词判断函数即可,操作如下:

核心实现代码

步骤1:生成地块是否包含建筑物的布尔索引

首先判断每个地块是否与任意建筑物存在空间相交关系,以此作为筛选依据:

# 加载需要的包,dplyr用于管道和数据处理
library(dplyr)
library(sf)

# 确认两个图层的坐标系一致(你之前的预处理已经统一为WGS84,可跳过校验)
stopifnot(st_crs(lots) == st_crs(buildings))

# 生成布尔索引:值为TRUE代表对应地块存在相交的建筑物
lot_has_building <- st_intersects(lots, buildings, sparse = FALSE) %>% 
  apply(MARGIN = 1, FUN = any)

如果需要严格筛选建筑物完全落在地块内部的场景,把st_intersects替换为st_contains即可。

步骤2:按索引筛选对应地块

# 需求2:筛选包含建筑物的地块
lots_with_buildings <- lots[lot_has_building, ]

# 需求1:筛选不包含建筑物的地块
lots_without_buildings <- lots[!lot_has_building, ]

绘图示例

可以用不同颜色区分两类地块:

library(ggmap)
ggmap() +
  # 无建筑地块用浅绿色填充
  geom_sf(data = lots_without_buildings, alpha = 0.9, fill = "#90EE90", inherit.aes = FALSE) +
  # 有建筑地块用浅红色填充
  geom_sf(data = lots_with_buildings, alpha = 0.9, fill = "#FFA07A", inherit.aes = FALSE)

内容的提问来源于stack exchange,提问作者yuliaUU

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 16:18:00