具有一个形状文件,该文件具有多个逻辑分区为“区域”和“图”的多边形。地域上的图重叠。任务是溶解/合并区域不重叠的图。
这是形状文件的显示。这里的图位于“区域”的顶部。 这也是带有重叠多边形(区域和图解)的shapefile:Shapefile
在QGIS中,通过提取区域和图,找到差异然后使用Union进行溶解来实现相同的功能。现在需要在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')
答案 0 :(得分:1)
读取shapefile
library(raster)
fields <- shapefile("gadenstedt2_outer_field 3 -26_0_zoned-plotm.shp")
首先将区域和字段分开。
zone <- fields[fields$Type == "Zone", ]
plot <- fields[fields$Type == "Plot", ]
从区域擦除图
d <- erase(zone, plot)
然后将plot
附加到d
r <- bind(plot, d)
现在聚合
rd <- aggregate(r, "Rx")
spplot(rd, "Rx")
----现在举一个可复制的例子,这样其他人也可以受益。您不应问依赖于需要下载文件的问题----
示例数据(两个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')
擦除并追加
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
答案 1 :(得分:0)
为确保解决方案可在所有字段上使用,请在上述解决方案上添加一行额外的代码以添加缓冲区几何。
[sytle.color]