How to extract coordinates values from a shapefile?
long = q[,1] lat = q[,2]
On 06/14/2010 05:05 AM, Thiago Veloso wrote:
This function worked like a charm, but I can't individually invoke the columns which contain the coordinates of variable "q".
names(q)NULL
For example, I need to plot the coordinates using polygon function, whose help tells us that "?polygon? draws the polygons whose vertices are given in ?x? and ?y?".
So, I'd like to be able to refer to lon and lat columns of variable "q" as "q$lon" and "q$lat".
Is it possible??
Best wishes,
Thiago.
--- On Wed, 9/6/10, Edzer Pebesma <edzer.pebesma at uni-muenster.de> wrote:
From: Edzer Pebesma <edzer.pebesma at uni-muenster.de>
Subject: Re: [R-sig-Geo] How to extract coordinates values from a shapefile?
To: r-sig-geo at stat.math.ethz.ch
Date: Wednesday, 9 June, 2010, 17:24
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