[R] SpatialPolygonsDataFrame holes problem

Dan Bebber dbebber at earthwatch.org.uk
Sun Jul 10 20:58:59 CEST 2011

Thank you very much, this code worked.
I will refer future geospatial queries to R-sig-geo, as suggested.
I have also edited the original shapefile in QGIS to remove the error, as suggested by Barry Rowlingson.


-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
Sent: Sat 09/07/2011 18:49
To: Dan Bebber
Cc: r-help at r-project.org; edzer.pebesma at uni-muenster.de
Subject: Re: SpatialPolygonsDataFrame holes problem
On Sat, 9 Jul 2011, Dan Bebber wrote:

> 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:

You assume that the data source is providing cleaned boundaries, this is 
not the case:

is0 <- readOGR(".", "india_state")
# reading with readOGR also reads the coordinate reference system

# use checkPolygonsHoles() to make sure that the holes are correctly
# defined (the input file has a single ring defined as a hole
# which is illogical, holes have to be within something else
slot(is0, "polygons") <- lapply(slot(is0, "polygons"), checkPolygonsHoles)

# next run unionSpatialPolygons() to merge the Polygons objects that
# belong to the same name
is1 <- unionSpatialPolygons(is0, as.character(is0$NAME))

# finally, all Polygons objects store the centroid of the largest 
# component, non-hole, Polygon object as a label point, this is
# probably what you want

You don't need to use rgeos directly where wrapper functions are provided 
in maptools; using:

gCentroid(is1, byid=TRUE)

returns a SpatialPoints object with centroids accommodating the multiple 
Polygon components, if these suit you better.

Please consider R-sig-geo for queries of this kind.

Hope this helps,


> 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

Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no

More information about the R-help mailing list