# 检查点坐标是否在多边形内

``````library(sf)
library(dplyr)

#create sf of lat long
heat_df\$point <- heat_df[,5:6] %>%
as.data.frame %>%
st_as_sf(coords = c(1,2)) %>%
st_set_crs(4326)

#make planar
heat_df\$point <- st_transform(heat_df\$point, 2163)
kml\$geometry <- st_transform(kml\$geometry, 2163)

#itterate through the following 5 times (once per polygon)
heat_df\$inter <- st_intersects(heat_df\$point, kml\$geometry[1], sparse = FALSE)
heat_df <-  heat_df[!(heat_df\$inter == TRUE),]
``````

``````> print(heat_df\$point[1])
[[1]]
[1] 6407800 9211903
attr(,"class")
[1] "XY"    "POINT" "sfg"

> print(kml\$geometry[1])
[[1]]
[[1]]
[,1]    [,2] [,3]
[1,] 4520903 5043254    0
[2,] 4520945 5043244    0
[3,] 4521016 5043207    0
[4,] 4521029 5043312    0
[5,] 4521027 5043325    0
[6,] 4521016 5043341    0
[7,] 4520962 5043405    0
[8,] 4520926 5043388    0
[9,] 4520903 5043254    0

attr(,"class")
[1] "XYZ"     "POLYGON" "sfg"
``````

• CRS 系统必须对齐（您选择哪一个很少重要；这将采取极端的极端情况 - 但它必须对齐；`st_transform`为此使用
• 根据班级的点对象（SFC与SF），你可能需要调用`st_as_sf()`第一

``````pip <- points %>%
st_join(heat_df)
``````

0 条评论