[R] extract data from shapefiles

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Sat Aug 15 00:55:51 CEST 2009

On Fri, Aug 14, 2009 at 9:11 PM, Kate
Zinszer<kate.zinszer at mail.mcgill.ca> wrote:
> I'm hoping that someone could guide me in how to extract data from shapefiles.  I want to extract data from a shapefile (classed as "SpatialPolygonsDataFrame") and more specifically, the data is contained within the slot called "coords" from the class "polygons" within this file and despite my best efforts (and much reading!), I'm at a lost.

 What you're doing is reading the shapefile into an R object of class
SpatialPolygonsDataFrame. And it seems you want the coordinates of the

 A SpatialPolygonsDataFrame breaks down thus:

 foo at polygons is a list of the feature geometries

 foo at polygons[[i]] is the i'th feature geometry

 foo at polygons[[i]]@Polygons is a list of the simple rings that make up
that feature

 foo at polygons[[i]]@Polygons[[j]] is the j'th ring of the i'th feature

 foo at polygons[[i]]@Polygons[[j]]@coords is the coords of the j'th ring
of the i'th feature.

Hence for the example in ?"SpatialPolygonsDataFrame-class" you can get
the coordinates of the first ring of the first feature thus:

 > ex_1.7 at polygons[[1]]@Polygons[[1]]@coords
     x    y
s1 0.5  9.5
s1 0.5 10.5
s1 1.5 10.5
s1 1.5  9.5
s1 0.5  9.5

 So you probably now want to loop over i and j and do something with
the coordinates.

 The structure is complex because features can have multiple rings -
eg some regions have islands or holes. You need to check the @hole
slot of the ... at Polygons[[1]] object if you care whether it's a hole
or an island!


More information about the R-help mailing list