从maptools执行checkPolygonsHoles后,多边形中的孔消失

时间:2015-04-01 15:03:28

标签: r spatial polygons maptools sp

注意:根据Edzer Pebesma的建议,这个问题已被转发到R-sig-geo,here,在那里收到了一些好的回复。


我使用checkPolygonsHoles遇到了以下意外结果:

# attach the worldmap as SpatialPolygonsDataFrame from the package maptools
library(sp)
library(maptools)
data(wrld_simpl)

# get a polygon with a hole
shape_with_hole <- wrld_simpl[5,]

# plot it (hole is left white, surrounded by blue color)
plot(shape_with_hole, col = "blue")

# perform checkPolygonsHoles 
shape_with_hole@polygons <- lapply(shape_with_hole@polygons,   checkPolygonsHoles)

# plot again, now holes aren't recognized as such
plot(shape_with_hole, col = "blue")

# and even the original SpatialPolygonsDataFrame object is changed !?
plot(wrld_simpl[5,], col = "blue")

这里有一个令人不快的副作用是原始对象wrld_simpl也发生了变化。这个结果在我看来就像一个错误,或者我错过了什么?

P.S。:之前用shape_with_hole编辑的对象checkPolygonsHoles继续表现得很奇怪:

# we check which polygons are marked as holes. The flags are still set 
# properly, although the `plot` function didn't recognize them:
sapply(shape_with_hole@polygons[[1]]@Polygons, slot, "hole")

[1] FALSE  TRUE  TRUE  TRUE

# load library rgdal for reprojection
library(rgdal)

# reproject with `spTransform`, just for testing
shape_with_hole <- spTransform(shape_with_hole, 
CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))

# after reprojection all flags are set to FALSE 
sapply(shape_with_hole@polygons[[1]]@Polygons, slot, "hole")

[1] FALSE FALSE FALSE FALSE

1 个答案:

答案 0 :(得分:1)

这篇清晰的报告揭示了R package sp中的一个错误,该错误仅在R 3.1.x中显示。有关更多详细信息,请参阅here。 CRAN提供的sp 1.1-0已修复此问题。

相关问题