【发布时间】:2014-08-01 19:57:14
【问题描述】:
在 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?
【问题讨论】:
-
r-sig-geo邮件列表仍然是这个问题的最佳位置。rgeos包作者 生活 在该列表中。并且您应该始终包含require( sp )等包依赖项以使您的示例运行。 -
你应该命名贡献的包,而不是说“in R”。这显然不是基本 R 或任何推荐包的一部分。
-
可能还涉及其他问题,但您至少应该知道(原因我不会在这里讨论)提取
@area插槽的值不是检索区域的可靠方法SpatialPolygons对象。相反,使用rgeos::gArea() -
乔希,请注意我使用了 gArea() 并且得到了相同的区域。我在之前的帖子中注意到了这条评论。
-
这些当然很有用。我编辑了问题以解决这些 cmets。