[英]Determine lines that intersect a polygon in R
如果您有標准格式的多邊形和線,則可以使用sf
st_intersects
中的 st_intersects。 下面是一個使用北卡羅來納州三個縣作為多邊形的示例。
加載sf
:
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.2, PROJ 6.2.1
制作多邊形數據(免責聲明:為了快速說明地理經度,此處使用緯度坐標,但應使用真正的平面投影坐標):
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
n <- 3
poly_dat <- st_sf(poly_id = LETTERS[1:n], nc$geometry[1:n])
制作行數據:
line1 <- st_linestring(rbind(c(-81.8,36.5),c(-80.5,36.5)))
line2 <- st_linestring(rbind(c(-81,36.3),c(-80.5,36.3)))
line_dat <- st_sf(linename = c("1", "2"), geometry = st_sfc(line1, line2, crs = st_crs(poly_dat)))
Plot:
plot(poly_dat, reset = FALSE)
plot(line_dat, add = TRUE, col = 1:2, lty = 2)
legend("topright", legend = paste("Line", 1:2), col = 1:2, lty = 2)
尋找交叉點:
st_intersects(line_dat, poly_dat)
#> although coordinates are longitude/latitude, st_intersects assumes that they are planar
#> Sparse geometry binary predicate list of length 2, where the predicate was `intersects'
#> 1: 1, 2, 3
#> 2: 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.