在R中使用差分和并集溶解重叠多边形
具有图形文件,该文件具有多个多边形,并具有分区和打印的逻辑划分。地块与区域重叠。任务是分解/合并带有无重叠区域的绘图 这是许多形状文件。这里的图位于田地区域的顶部。 此外,这是具有重叠多边形区域和绘图的shapefile: 在QGIS中,同样是通过提取分区和地块,找出差异,然后使用Union进行分解来实现的 在R中尝试了以下步骤,但无法获得正确的结果,正在寻找如何在R中消除此类重叠策略的方法:在R中使用差分和并集溶解重叠多边形,r,r-raster,sp,rgeo-shapefile,R,R Raster,Sp,Rgeo Shapefile,具有图形文件,该文件具有多个多边形,并具有分区和打印的逻辑划分。地块与区域重叠。任务是分解/合并带有无重叠区域的绘图 这是许多形状文件。这里的图位于田地区域的顶部。 此外,这是具有重叠多边形区域和绘图的shapefile: 在QGIS中,同样是通过提取分区和地块,找出差异,然后使用Union进行分解来实现的 在R中尝试了以下步骤,但无法获得正确的结果,正在寻找如何在R中消除此类重叠策略的方法: library(sp); library(raster); library(rgeos) #Impo
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')
阅读shapefile
library(raster)
fields <- shapefile("gadenstedt2_outer_field 3 -26_0_zoned-plotm.shp")
现在加起来
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
为了确保解决方案适用于所有字段,在上述解决方案中添加了一行额外的代码,以添加缓冲区几何体
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")
我们没有你的数据,所以我们无法运行你的代码,所以我们看不出哪里出了问题。请更新您的问题。感谢@Spacedman的回复。已上载包含多边形和关联数据的shapefile。链接:谢谢罗伯特的回答。“相交”仅提供相交绘图的绘图和分区。还需要剩余的区域,以使地块和区域溶解。再次感谢Robert,但添加的区域没有如地块一样的数据值。Rx值将用于现场生物量区域识别。再次感谢Robert的更新。在回答正确之前,我继续分析数据,发现绘图的Rx值现在与分区相同。图Rx值应保持不变,就像我们现在在分解图和区域后对区域所做的一样。你能帮个忙吗?我简化了,现在它就这么做了。很难准确理解你的问题,你使用的术语“溶解”是不正确的。哦,所以你确实想要溶解。目前的解决方案如何?
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
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")