[R-sig-Geo] How to extract coordinates values from a shapefile?
Edzer Pebesma
edzer.pebesma at uni-muenster.de
Wed Jun 9 22:24:09 CEST 2010
The example provided by Matt assumes that each polygon consists of a
single ring, and doesn't have islands, lakes etc. The function below
pasts all coordinates to a single 2-column matrix. For clarity's sake, I
avoided nested sapply's.
library(maptools)
xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
allcoordinates = function(x) {
ret = NULL
polys = x at polygons
for(i in 1:length(polys)) {
pp = polys[[i]]@Polygons
for (j in 1:length(pp))
ret = rbind(ret, coordinates(pp[[j]]))
}
ret
}
q = allcoordinates(xx)
# check:
plot(xx)
lines(q, col='blue')
On 06/09/2010 09:58 PM, Matt Beard wrote:
>>
>> library(maptools)
>>
>> # A simple way to print out a list of coordinates for each polygon in your
>> shapefile:
>>
>> # Path and filename of polygon shapefile
>> testfile <- '/media/PKBACK# 001/FNR210/QGISLab/habitats/habitats.shp'
>>
>> # Read in polygon shapefile using handy maptools function
>> test <- readShapePoly(testfile)
>>
>> # Extract the list of Polygons objects
>> polys <- slot(test,"polygons")
>>
>> # Within each Polygons object
>> # Extract the Polygon list (assuming just one per Polygons)
>> # And Extract the coordinates from each Polygon
>> for (i in 1:length(polys)) {
>> print(paste("Polygon #",i))
>> print(slot(slot(polys[[i]],"Polygons")[[1]],"coords" ))
>> }
>>
>>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
--
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics e.pebesma at wwu.de
More information about the R-sig-Geo
mailing list