在R中,我对一组X, Y坐标进行了分析,生成了Voronoi图,然后我创建了一个边界。在这里,我将图表与边界相交,并尝试获得生成多边形的面积。内部"孔"多边形的面积是正确的,但边缘多边形似乎保持了原来的夸张大小。我的数据链接在这里:
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?
我应该使用gIntersection而不是gDifference。由于某种原因,我仍然不明白,使用gDifference后的情节似乎是正确的,这是混淆的主要原因。