On Fri, Aug 14, 2009 at 9:11 PM, Kate Zinszer<kate.zins...@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 polygons. A SpatialPolygonsDataFrame breaks down thus: f...@polygons is a list of the feature geometries f...@polygons[[i]] is the i'th feature geometry f...@polygons[[i]]@Polygons is a list of the simple rings that make up that feature f...@polygons[[i]]@Polygons[[j]] is the j'th ring of the i'th feature f...@polygons[[i]]@polygons[[...@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_...@polygons[[1]]@polygons[[...@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 ....@polygons[[1]] object if you care whether it's a hole or an island! Barry ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.