簡體   English   中英

確定與 R 中的多邊形相交的線

[英]Determine lines that intersect a polygon in R

我有一個數據集,其中包含 1,000 條線,我可以覆蓋在 map(Sec,Township,Range)的部分土地上。 我需要識別並收集有哪些線穿過它們的部分的名稱。

我能夠在多邊形中找到一個點,但不能找到一條線。 該線可以穿過多個部分。 我想我需要一個循環來做到這一點,但我遇到了麻煩。

我有一個例子可以幫助描述我想要實現的目標。

任何幫助將不勝感激!

在此處輸入圖像描述

如果您有標准格式的多邊形和線,則可以使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM