简体   繁体   English

带R的SpatialPolygonsDataFrame中重叠多边形的属性值求和

[英]Summing values of attributes of overlapping polygons in SpatialPolygonsDataFrame with R

I have one shape file containing lots of SpatialPolygons which are partly overlapping. 我有一个形状文件,其中包含许多部分重叠的SpatialPolygon。 These polygons belong to the application of a fungicide on a field and each polygon has an associated application rate as attribute. 这些多边形属于杀真菌剂在田间的施用,并且每个多边形具有关联的施用率作为属性。

What I want to obtain is to correct AsApplied map taking into account the overlapping areas meaning that the rate should be summed up and merged if two (or more) polygons are overlapping. 我要获得的是考虑到重叠区域来更正AsApplied地图,这意味着如果两个(或更多)多边形重叠,则应该对比率求和并合并。

The following example code creates a SpatialPolygonsDataFrame simplifying the problem: 以下示例代码创建一个SpatialPolygonsDataFrame来简化此问题:

library(raster)
library(sp)

p<-SpatialPolygons(list(Polygons(list(Polygon(cbind(c(1,4,4,3,3,1,1),c(1,1,3,3,4,4,1)),hole = F)), "1_ "),
                    Polygons(list(Polygon(cbind(c(3,4,4,3,3),c(3,3,4,4,3)),hole = F)), "1_2"),
                    Polygons(list(Polygon(cbind(c(3,4,4,3,3),c(3,3,4,4,3)),hole = F)), "2_1"),
                    Polygons(list(Polygon(cbind(c(4,4,5,5,3,3,4),c(4,3,3,5,5,4,4)),hole = F)),"2_")))

pid <- sapply(slot(p, "polygons"), function(x) slot(x, "ID"))
p.df <- data.frame( ID=1:length(p), row.names = pid) 
p <- SpatialPolygonsDataFrame(p, p.df)
p$Rate <- c(100, 100, 100, 100)
crs(p) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
plot(p)

You can see two squares from four polygons which are partly overlapping. 您可以从四个部分重叠的多边形中看到两个正方形。 Each polygon has an associated rate of 100. What I would like to have is three polygons. 每个多边形的关联费率为100。我想拥有三个多边形。 The two non overlapping ones should have the rate 100 and the two overlapping ones should be joined to one polygon having a value of 200. 两个不重叠的多边形的比率应为100,而两个重叠的多边形的比率应为一个值为200的多边形。

I already tried the union or intersect functions of the raster package but was only able to get the information which polygons overlap but not the summing and merging. 我已经尝试了栅格数据包的并集或相交功能,但只能获取多边形重叠的信息,而不能求和和合并。 In addition I am seeking explicitly for a solution in R. 另外,我正在明确寻求R中的解决方案。

Any help solving this problem is highly appreciated. 非常感谢您对解决此问题的任何帮助。

Update: The solution provided by RobertH provided below works for my simple example. 更新:下面提供的RobertH提供的解决方案适用于我的简单示例。 Thank you very much already! 已经非常感谢您了!

However when switching to my real usecase I am getting to following kind of errors and warnings: 但是,当切换到实际用例时,我会遇到以下错误和警告:

Error in if (is.numeric(i) && i < 0) { : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In RGEOSUnaryPredFunc(spgeom, byid, "rgeos_isvalid") :
  Too few points in geometry component at or near point 8.3634020800000002 50.056772690000003
...

An example shape file is uploaded here: (obsolete) 示例形状文件已上传到此处:(已淘汰)

Any ideas how to deal with this problem? 有什么想法如何处理这个问题?

Update #2 Using the current development version 2.5-10 indeed fixes the warnings in RGEOSUnaryPredFunc. 更新#2使用当前的开发版本2.5-10确实可以修复RGEOSUnaryPredFunc中的警告。 However, if polygons are only overlapping very very little I am still getting the error: 但是,如果多边形仅重叠得很少,我仍然会收到错误:

Error in if (is.numeric(i) && i < 0) { : 
  missing value where TRUE/FALSE needed

An example shape file for which this is happening is uploaded here: http://www.share-online.biz/dl/O4ZIVH8OBW . 发生这种情况的示例形状文件上传到此处: http : //www.share-online.biz/dl/O4ZIVH8OBW More precise the field looks like the following: 更准确地说,该字段如下所示:

Image of polygon example 2 多边形示例2的图像

The two polygons marked in red cause the error and if one of the two is removed the union works fine. 用红色标记的两个多边形会导致错误,并且如果删除了其中两个之一,则合并效果很好。

Thank you very much already for your great help! 非常感谢您的大力帮助!

I think it is indeed union you are after. 我认为您确实追求的是union It merges and identifies the overlapping polygons. 它合并并标识重叠的多边形。 With that you can sum the rates. 有了它,您可以求和费率。

# example data
library(raster)
p1 <- cbind(c(1,4,4,1),c(1,1,4,4))
p2 <- cbind(c(3,5,5,3),c(3,3,5,5))
p <- spPolygons(p1, p2, crs="+proj=longlat +datum=WGS84", 
                        attr=data.frame(ID=1:2, Rate =c(50,100)))

#data.frame(p)
#  ID Rate
#1  1   50
#2  2  100

First use union 首次使用工会

x <- union(p)
ud <- data.frame(x)
ud$count <- NULL

Sum the rates for contributing polygons 对贡献多边形的比率求和

udRate <- t( t(ud) * p$Rate )
x$Rate <- rowSums(udRate)
data.frame(x)

#  ID.1 ID.2 count Rate
#1    1    0     1   50
#2    0    1     1  100
#3    1    1     2  150

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

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