简体   繁体   English

在R中使用差异和并集来溶解重叠的多边形

[英]Dissolve Overlapping Polygons using difference and union in R

Have the shape file which has multiple polygons with logical division of Zones and Plots. 具有形状文件,该文件具有多个由区域和图进行逻辑划分的多边形。 Plots are overlapping over Zones. 地域上的图重叠。 The task is to dissolve / merge plots with Zones with no overlapping. 任务是溶解/合并区域不重叠的图。

在此处输入图片说明 Here is spplot of the shape file. 这是形状文件的图。 Here plots are on top of the Field Zones. 这里的图位于“区域”的顶部。 Also here is the shapefile with overlapped polygons (Zones & Plots): Shapefile 另外,还有重叠多边形(区域和图解)的shapefile: Shapefile

In QGIS, the same was achieved using Extracting the the Zones & Plots, Finding the difference and then dissolving using Union.Now need to program the same in R. 在QGIS中,通过提取区域和图,找到差异然后使用Union进行溶解来实现相同的功能,现在需要在R中进行编程。

Tried below steps in R but would not able to get the right results , looking for ways how to dissolve this type of overlapping ploygons in R: 在R中尝试了以下步骤,但无法获得正确的结果,寻找了在R中解决这种重叠多角形的方法:

library(sp);
library(raster);
library(rgeos)

#Importing the shape files

field_boundary_fp <- "Database/gadenstedt2_outer_field 3 -26_0_zoned-plotm.shp"
poly_map_proj_str <- "+proj=longlat +datum=WGS84 +no_defs";
utm_target_proj   <- "+init=epsg:32632";

field_boundary_sdf <- maptools::readShapePoly(fn = field_boundary_fp,
                                          proj4string =  CRS(poly_map_proj_str),
                                          repair = T,
                                          delete_null_obj = T,
                                          verbose = T);
spplot(
field_boundary_sdf,"Rx"
)

# Extracting the Zones and Plots#

Zone_sdf <- field_boundary_sdf[field_boundary_sdf@data$Type == "Zone", ]
Plot_sdf <- field_boundary_sdf[field_boundary_sdf@data$Type == "Plot", ]
plot(Plot_sdf)
plot(Zone_sdf)

#Finding the Intersection Part between the both
test <- gIntersection(Zone_sdf, Plot_sdf,id="ZoneIdx")
plot(test)
plot(test, add = T, col = 'blue')

# Finding the difference

test2 <- gDifference(Zone_sdf,Plot_sdf,id="ZoneIdx")
plot(test2)
plot(test2, add = T, col = 'red')

#Trying for Union then
polygon3 <- gUnion(test2, Plot_sdf,id="ZoneIdx")
plot(polygon3)
plot(polygon3, add = T, col = 'yellow')

Read the shapefile 读取shapefile

library(raster)
fields <- shapefile("gadenstedt2_outer_field 3 -26_0_zoned-plotm.shp")

First separate the zones and fields. 首先将区域和字段分开。

zone <- fields[fields$Type == "Zone", ]
plot <- fields[fields$Type == "Plot", ]

Erase plot from the zone 从区域擦除图

d <- erase(zone, plot)  

Then append plot to d 然后将plot追加到d

r <- bind(plot, d)

And now aggregate 现在聚合

rd <- aggregate(r, "Rx")
spplot(rd, "Rx")

---- Now with a reproducible example, so that others can also benefit; ----现在有一个可复制的示例,这样其他人也可以受益; you should not ask questions that depend on a file that needs to be downloaded ---- 您不应该问依赖于需要下载文件的问题----

Example data (two SpatialPolygonDataFrame objects) 示例数据(两个SpatialPolygonDataFrame对象)

library(raster)
p <- shapefile(system.file("external/lux.shp", package="raster"))
p <- aggregate(p, "NAME_1")
p$zone <- 10 + (1:length(p))
r <- raster(ncol=2, nrow=2, vals=1:4, ext=extent(6, 6.4, 49.75, 50), crs=crs(p))
names(r) <- "zone"
b <- as(r, 'SpatialPolygonsDataFrame')

erase and append 删除并追加

e <- erase(p, b)
pb <- bind(e, b)

data.frame(pb)
#        NAME_1 zone
#1     Diekirch   11
#2 Grevenmacher   12
#3   Luxembourg   13
#4         <NA>    1
#5         <NA>    2
#6         <NA>    3
#7         <NA>    4

To ensure Solution works on all Fields , added an extra line of code to above solution to add Buffer Geometry. 为确保解决方案可在所有字段上使用,在上述解决方案上添加了额外的一行代码以添加缓冲区几何。

fields <- gBuffer(fields, byid=TRUE, width=0) # Expands the given geometry to include 
the area within the specified width 

zone <- fields[fields$Type == "Zone", ]
plot <- fields[fields$Type == "Plot", ]

d <- erase(zone, plot)
spplot(d, "Rx")

r <- bind(plot, d)

rd <- aggregate(r, "Rx")

spplot(rd, "Rx")

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

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