在R中,我对一组产生Voronoi图的X,Y坐标进行了分析,然后我创建了一个边框。在这里,我与图和边界相交,并尝试获得所得多边形的面积。内部区域"孔"多边形是正确的,但边多边形似乎保持其原始夸张的大小。我的数据链接在这里:
https://drive.google.com/a/ruths.ai/file/d/0B8QG4cbDqH0UaGM2VkkxZHZkZTA/edit?usp=sharing
说明问题的代码在这里:
# Read in shapefiles.
# Files are located at:
# https://drive.google.com/a/ruths.ai/file/d/0B8QG4cbDqH0UaGM2VkkxZHZkZTA/edit?usp=sharing
require(sp)
require(rgeos)
SPDF <- readShapeSpatial("SPDF.shp")
SpP <- readShapeSpatial("SpP.shp")
# Examine plots
plot(SPDF)
SPDF@polygons[[337]]@area # Too large; want it cut off
SPDF@polygons[[339]]@area # Hole poly; area correct
gArea(SPDF[339,]) # Provides same area
gArea(SPDF[337,]) # Still provide wrong answer for problem
# poly # 337
# Merge polys using gDifference
D <- gDifference(SpP, SPDF, byid = TRUE)
plot(D)
# Seems to work, but areas now have a couple of problems.
# I pick apart D using the plotOrder slot to separate
# polys that are holes versus those that are not, allowing
# me to get the correct area for "hole" polys, but the
# edge polygons are still not correct, maintaining their
# area estimates from the original SPDF data frame.
areas <- vector()
for (i in 337:339){ # 337 = exterior poly, 338 and 339 are holes
po <- D@polygons[[i]]@plotOrder
if (max(po) == 2) {
areas[i] <- D@polygons[[i]]@Polygons[[2]]@area
} else {
areas[i] <- D@polygons[[i]]@area
}
}
areas
# How does one get the right areas for the edges that should be cut
# off by the intersection?
答案 0 :(得分:0)
我应该使用gIntersection而不是gDifference。由于某些原因,我仍然不明白,使用gDifference后的情节似乎是正确的,这是混淆的主要原因。