繁体   English   中英

在R中找到最匹配的重叠多边形

[英]Find best matching overlapping polygons in R

我有两个shapefile,我使用readOGR()作为SpatialPolygonsDataFrame对象读入R. 两者都是新西兰的地图,内部边界不同。 一个人有大约70个多边形代表领土权限边界; 另一个约有1900个代表区域单位。

我的目标 - 一个更大的项目的烦人基本部分 - 是使用这些地图生成一个参考表,可以查找一个区域单位并返回它主要在哪个领土权威。我可以使用over()来查找哪个多边形重叠,但在许多情况下,区域单位似乎,至少在很小的范围内,在多个领土当局内 - 即使查看个别案例表明通常90%以上的区域单位属于单一的地域当局。

是否有一个准备好的手段来做over()所做的但是它不仅可以识别(或不是)所有重叠的多边形,而是几个重叠的多边形中的哪一个在每种情况下最重叠?

这是完成这项工作的代码,借鉴@ Silverfish的答案

library(sp)
library(rgeos)
library(rgdal)

###
# Read in Area Unit (AU) boundaries
au <- readOGR("C:/Users/Peter Ellis/Documents/NZ", layer="AU12")

# Read in Territorial Authority (TA) boundaries
ta <- readOGR("C:/Users/Peter Ellis/Documents/NZ", layer="TA12")

###
# First cut - works ok when only one TA per area unit
x1 <- over(au, ta)
au_to_ta <- data.frame(au@data, TAid = x1)

###
# Second cut - find those with multiple intersections
# and replace TAid with that with the greatest area.

x2 <- over(au, ta, returnList=TRUE)

# This next loop takes around 10 minutes to run:
for (i in 1:nrow(au_to_ta)){
    tmp <- length(x2[[i]])
    if (tmp>1){
        areas <- numeric(tmp)
        for (j in 1:tmp){
            areas[j] <- gArea(gIntersection(au[i,], ta[x2[[i]][j],]))
            }
#       Next line adds some tiny random jittering because
#       there is a case (Kawerau) which is an exact tie
#       in intersection area with two TAs - Rotorua and Whakatane

        areas <- areas * rnorm(tmp,1,0.0001)

        au_to_ta[i, "TAid"] <- x2[[i]][which(areas==max(areas))]
    }

}


# Add names of TAs
au_to_ta$TA <- ta@data[au_to_ta$TAid, "NAME"]

####
# Draw map to check came out ok
png("check NZ maps for TAs.png", 900, 600, res=80)
par(mfrow=c(1,2), fg="grey")
plot(ta, col=ta@data$NAME)

title(main="Original TA boundaries")
par(fg=NA)
plot(au, col=au_to_ta$TAid)
title(main="TA boundaries from aggregated\nArea Unit boundaries")
dev.off()

在此输入图像描述

我认为你想要的东西已经涵盖在地理信息系统SE上:

https://gis.stackexchange.com/questions/40517/using-r-to-calculate-the-area-of-multiple-polygons-on-a-map-that-intersect-with?rq=1

特别是如果你的领土多边形是T1,T2,T3等你尝试分类您的多边形是A,可能要使用gAreagIntersection A和T1,那么A和T2,那么A和T3等,然后选择哪个区域最大。 (你需要rgeos包。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM