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
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.
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:
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')