带R的SpatialPolygonsDataFrame中重叠多边形的属性值求和 [英] Summing values of attributes of overlapping polygons in SpatialPolygonsDataFrame with R

查看:63
本文介绍了带R的SpatialPolygonsDataFrame中重叠多边形的属性值求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形状文件,其中包含许多部分重叠的SpatialPolygon.这些多边形属于杀真菌剂在田间的施用,并且每个多边形具有关联的施用率作为属性.

I have one shape file containing lots of SpatialPolygons which are partly overlapping. These polygons belong to the application of a fungicide on a field and each polygon has an associated application rate as attribute.

我要获取的是考虑到重叠区域的情况更正AsApplied地图,这意味着如果两个(或多个)多边形重叠,则应该对比率进行汇总和合并.

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.

以下示例代码创建一个SpatialPolygonsDataFrame来简化此问题:

The following example code creates a SpatialPolygonsDataFrame simplifying the problem:

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)

您可以看到部分重叠的四个多边形中的两个正方形.每个多边形的关联费率为100.我想拥有三个多边形.两个不重叠的多边形的比率应为100,而两个重叠的多边形的比率应为一个值为200的多边形.

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. 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.

我已经尝试了栅格数据包的并集或相交功能,但只能获取多边形重叠的信息,而不能求和和合并.另外,我正在明确寻求R中的解决方案.

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.

任何帮助解决此问题的方法都将受到高度赞赏.

Any help solving this problem is highly appreciated.

更新:下面由RobertH提供的解决方案适用于我的简单示例.已经非常感谢您了!

Update: The solution provided by RobertH provided below works for my simple example. 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?

更新#2 使用当前的开发版本2.5-10确实可以修复RGEOSUnaryPredFunc中的警告.但是,如果多边形仅重叠得很少,我仍然会收到错误:

Update #2 Using the current development version 2.5-10 indeed fixes the warnings in 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

发生这种情况的示例形状文件上传到此处: http://www.share-online.biz/dl/O4ZIVH8OBW .更准确地说,该字段如下所示:

An example shape file for which this is happening is uploaded here: http://www.share-online.biz/dl/O4ZIVH8OBW. More precise the field looks like the following:

多边形示例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!

推荐答案

我认为确实是您追求的 union .它合并并标识重叠的多边形.有了它,您可以计算出费率.

I think it is indeed union you are after. 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

首次使用工会

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

求和多边形的比率

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

这篇关于带R的SpatialPolygonsDataFrame中重叠多边形的属性值求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆