![](/img/trans.png)
[英]Check polygon intersection in R: sf::intersects incorrectly returns TRUE result while rgeos::gIntersects correctly returns FALSE
[英]polygon intersection in R
我有一個包含很多多邊形,點和線的列表。 現在,我想檢查每個多邊形之間的相交區域。 我知道我可以使用gIntersect
,但是我的數據在data.frame中,而不在spatialpolygon文件中。 有什么聰明的方法嗎?
lon lat ID
127.0167 29.14449 1
127.0148 29.14507 1
123.8638 17.63341 2
123.8593 17.62754 2
123.8568 17.63601 2
123.8528 17.65023 2
127.6790 49.01934 3
127.2719 49.12513 6
127.0249 49.14633 7
127.6763 49.02139 8
127.6710 49.02426 8
127.6684 49.02668 8
127.4648 49.07208 13
127.3757 49.08205 13
127.4198 30.04310 14
127.4259 40.04974 14
127.3136 39.09050 15
127.3197 29.09516 15
127.2360 39.16492 16
127.2099 49.16787 16
127.2266 29.01800 17
128.2771 48.36411 18
128.1930 28.44411 18
128.1925 18.44530 18
128.1928 28.44553 18
128.1932 48.44598 18
128.1953 38.44774 18
128.1978 28.44532 18
125.7947 28.71272 19
125.7982 28.72078 19
125.8402 18.74029 19
125.8572 18.74141 19
這是清單。 它在原始文件中包含更多數據點。 具有相同ID的坐標屬於同一多邊形,點或線。 我需要檢查每個多邊形與列表中所有其他多邊形的交集。
您可以將data.frame
轉換為SpatialPolygons
。
library(sp)
polys <- lapply(unique(coords$ID), function(i) {
Polygons(list(Polygon(coords[coords$ID==i, 1:2])), ID=i)
})
spa_polys <- SpatialPolygons(polys)
另外,我發現gIntersects
不適用於點(單點空間多邊形)或線(兩點空間多邊形)。
library(rgeos)
> gIntersects(spa_polys[13], spa_polys[1])
Error in RGEOSBinPredFunc(spgeom1, spgeom2, byid, func) :
IllegalArgumentException: Invalid number of points in LinearRing found 3 - must be 0 or >= 4
> gIntersects(spa_polys[13], spa_polys[2])
[1] FALSE
> gIntersects(spa_polys[13], spa_polys[3])
Error in RGEOSBinPredFunc(spgeom1, spgeom2, byid, func) :
IllegalArgumentException: point array must contain 0 or >1 elements
> gIntersects(spa_polys[13], spa_polys[13])
[1] TRUE
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.