[R] SpatialPolygonsDataFrame holes problem

Dan Bebber dbebber at earthwatch.org.uk
Sat Jul 9 10:40:55 CEST 2011

I have obtained shapefiles for Indian states from here:

Problem: I want to extract centroid coordinates for each State, but there is some coding problem with the shapefiles that prevents this.

#After extracting the shapefiles from the india_state.zip file, then:
india <- readShapeSpatial("india_state.shp")

#Some states are made up of more than one polygon.
#State names are in the NAME column of the dataframe
india at data
plot(india[india$NAME == "Tamil Nadu",], col = 1:8)

#Extract centroid for a State (this works):
gCentroid(india[india$NAME == "Tamil Nadu",])

#Error arises for this State:
gCentroid(india[india$NAME == "Gujarat",])

#Error says:
#Error in createPolygonsComment(p) : 
#rgeos_PolyCreateComment: orphaned hole, cannot find containing polygon for hole at index 2

#Code ends

I don't know my way around SpatialPolygonsDataFrame objects very well, and have no idea how to fix the errant hole.
Any help greatly appreciated.

Dan Bebber

More information about the R-help mailing list