[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:
http://www.maptell.com/index.php?option=com_remository&Itemid=159&func=fileinfo&filecatid=115&parent=category

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

#Code:
#After extracting the shapefiles from the india_state.zip file, then:
library(maptools)
library(sp)
library(rgeos)
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.

Thanks
Dan Bebber



More information about the R-help mailing list