简体   繁体   English

如何在R中划分多边形?

[英]How to divide polygon in R?

I have a shapefile of Amazonas state (Brazil) and other with the six biggest rivers in this state (Negro, Solimões, Amazonas, Madeira, Purus and Juruá). 我有一个亚马逊州(巴西)的形状文件和其他六个最大的河流(黑人,Solimões,亚马逊,马德拉,普鲁斯和朱鲁亚)。 I want to divide the state using the rivers, to get the interfluvial areas (Madeira-Purus, Purus-Juruá, etc). 我想用河流划分州,以获得河流间的区域(马德拉 - 普鲁斯,Purus-Juruá等)。 I want to get the final 6 regions delimited by those rivers, each region as a different polygon. 我希望得到这些河流划分的最后6个区域,每个区域都是不同的多边形。 How do I do that? 我怎么做?

在此输入图像描述

I'm only finding "clipping" algorithms, and those give me the area of the rivers that are inside the state, and that's not what I want. 我只是找到了“削波”算法,这些算法让我看到了州内河流的区域,这不是我想要的。

Following @jbaums comment, I used gDifference, from package rgeos: 关注@jbaums评论,我使用了包rgeos中的gDifference:

intflv <- gDifference(state,rivers)

but since "state" has only one polygon, intflv became a SpatialPolygons object with only one Polygon, made of thousands of sub-polygons. 但由于“state”只有一个多边形,因此intflv成为一个SpatialPolygons对象,只有一个Polygon,由数千个子多边形组成。 To work better in GIS software, I wanted it to be an object with all the thousands of Polygons, each as a single sub-polygon. 为了更好地在GIS软件中工作,我希望它是一个包含所有数千个多边形的对象,每个多边形都是一个子多边形。 So I did: 所以我做了:

l <- list()
total <- length(intflv@polygons[[1]]@Polygons)
for (i in 1:total) {
    print(paste(i,total,sep="/"))
    flush.console()
    P <- intflv@polygons[[1]]@Polygons[[i]]
    l[[length(l)+1]] <- Polygons(list(P),i)
}
sp <- SpatialPolygons(l)
d <- data.frame(IDs=1:total)
intflv1 <- SpatialPolygonsDataFrame(sp,data=d)
writeOGR(intflv1,"shp","intflv","ESRI Shapefile")

The result (after keeping only the biggest areas): 结果(仅保留最大区域后):

在此输入图像描述

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

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