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

Leaflet用户绘制多边形内点判断问题求助

问题根源与解决方法

你的核心问题是把用户绘制的多边形坐标转换成了点要素集,而非多边形要素,导致st_intersects在判断点与点的交集,自然只有完全重合的点才会返回TRUE,所以结果全为FALSE。

修正步骤

1. 正确构造SF多边形对象

Leaflet绘制的多边形坐标格式为嵌套数组[[[lon1, lat1], [lon2, lat2], ...]],无需拆分成单一点,直接构造多边形:

# 获取用户绘制的多边形坐标
poly_coords <- input$leaflet_geonosis_draw_new_feature$geometry$coordinates[[1]]

# 转换为SF多边形要素
shp_poly <- st_polygon(list(do.call(rbind, poly_coords))) %>%
  st_sfc(crs = 4326) %>%  # 设置WGS84坐标系
  st_sf()

验证shp_poly类型,应为POLYGON而非POINT:

> shp_poly
Simple feature collection with 1 feature and 0 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 48.489989 ymin: -2.799787 xmax: 48.526382 ymax: -2.717045
Geodetic CRS:  WGS 84
                        geometry
1 POLYGON ((48.52638 -2.79361...

2. 执行点-in-多边形判断

使用st_within更贴合“点是否在多边形内”的场景(st_intersects也可,但st_within语义更准确):

bin_sirene_in_poly <- st_within(shp_geo_sirene, shp_poly, sparse = FALSE)

额外排查点

坐标顺序一致性

WGS84标准坐标顺序为经度(lon)在前,纬度(lat)在后,需确保点数据与多边形的坐标顺序统一:

  • 检查shp_geo_sirene的POINT格式:如果显示为POINT (纬度 经度)(如你的示例中POINT (48.326389 -2.882054),48为北纬,-2为西经),则坐标顺序颠倒,需交换:
# 交换点数据的经纬度顺序
shp_geo_sirene$geometry <- st_sfc(lapply(shp_geo_sirene$geometry, function(p) {
  coords <- st_coordinates(p)
  st_point(c(coords[2], coords[1]))  # 从(lat, lon)转为(lon, lat)
}), crs = 4326)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 15:06:22