【问题标题】:Area of polygons wrong after intersection using gDifference in R在 R 中使用 gDifference 相交后多边形面积错误
【发布时间】: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。

标签: r polygon spatial


【解决方案1】:

我应该使用 gIntersection 而不是 gDifference。由于某种我仍然不明白的原因,使用 gDifference 后的情节似乎是正确的,这是造成混乱的主要原因。

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多