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:
library(rgdal)
is0 <- readOGR(".", "india_state")
# reading with readOGR also reads the coordinate reference system
library(maptools)
# 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))
coordinates(is1)
# 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,
Roger
> 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