简体   繁体   English

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

[英]Find best matching overlapping polygons in R

I have two shapefiles that I have read into R using readOGR() as SpatialPolygonsDataFrame objects. 我有两个shapefile,我使用readOGR()作为SpatialPolygonsDataFrame对象读入R. Both are maps of New Zealand with different internal boundaries. 两者都是新西兰的地图,内部边界不同。 One has about 70 polygons representing territorial authority boundaries; 一个人有大约70个多边形代表领土权限边界; the other has about 1900 representing area units. 另一个约有1900个代表区域单位。

My aim - an annoyingly basic part of a bigger project - is to use these maps to produce a reference table that can look up an area unit and return which territorial authority it is mostly in. I can use over() to find the which polygons overlap, but in many cases area units seem to be, at least in small part, within multiple territorial authorities - even though looking at individual cases suggests that normally 90%+ of an area unit is in a single territorial authority. 我的目标 - 一个更大的项目的烦人基本部分 - 是使用这些地图生成一个参考表,可以查找一个区域单位并返回它主要在哪个领土权威。我可以使用over()来查找哪个多边形重叠,但在许多情况下,区域单位似乎,至少在很小的范围内,在多个领土当局内 - 即使查看个别案例表明通常90%以上的区域单位属于单一的地域当局。

Is there a ready to hand means that does what over() does but which can identify not just (or not even) all the overlapping polygons, but which of the several overlapping polygons is the most overlapping in each case? 是否有一个准备好的手段来做over()所做的但是它不仅可以识别(或不是)所有重叠的多边形,而是几个重叠的多边形中的哪一个在每种情况下最重叠?

Here is code that did the job, drawing on @Silverfish's answer 这是完成这项工作的代码,借鉴@ 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()

在此输入图像描述

I think what you want has been covered on the geographic information systems SE: 我认为你想要的东西已经涵盖在地理信息系统SE上:

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

In particular if your territory polygons are T1, T2, T3 etc and your polygon you are trying to classify is A, probably want to use gArea on the gIntersection of A and T1, then A and T2, then A and T3, etc, then pick which has the maximum area. 特别是如果你的领土多边形是T1,T2,T3等你尝试分类您的多边形是A,可能要使用gAreagIntersection A和T1,那么A和T2,那么A和T3等,然后选择哪个区域最大。 (You would need the rgeos package.) (你需要rgeos包。)

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

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