[R-sig-Geo] looping sp data

Kemp, Michael M.U.Kemp at uva.nl
Fri Jul 22 17:37:43 CEST 2011


Hello,

It also appears that there is an error in your loop.  Instead of...

for (x in 1:length(data.eden)) {
     data<-readOGR(x) 
     summary(data)   
}

Try...

for (x in 1:length(data.eden)) {
     data<-readOGR(data.eden[x]) 
     summary(data)   
}

Or...

for (x in data.eden) {
     data<-readOGR(x) 
     summary(data)   
}


Hope this helps,

Michael U. Kemp
University of Amsterdam
Computational Geo-Ecology

-----Original Message-----
From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Barry Rowlingson
Sent: Friday, July 22, 2011 5:31 PM
To: ahelmore
Cc: r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] looping sp data

On Fri, Jul 22, 2011 at 3:45 PM, ahelmore <aelmore at usgs.gov> wrote:
> Hi there,
>
> I have a rather lengthy bit of code I want to run on a series of point
> files, but I can't even figure out how to load shapefiles in a simple loop.
> Since readOGR takes shapefile names without the .shp extension, I thought I
> could make a list of unduplicated file names, sans extensions and use that
> to run the loop, but I'm getting a missing layer error.  After staring at it
> for way too long, I'm looking for a leg up.  Can anyone tell me where my
> thinking/code is off?
>
> Here's what isn't working:
>
> patt.xml <- ".*eden.*xml.*"
> patt.shp <- ".*eden.*shp.*"
> data.shp.xml <-list.files(pattern=patt.shp) # list of eden .shp & .shp.xml
> data
> data.xml <-list.files(pattern=patt.xml) # list of eden .shp.xml data
> data.shp <- data.shp.xml[!data.shp.xml %in% data.xml] # list of .shp files
> data.eden <- gsub(".shp","",data.shp) #stripping the extension
>
>
> for (x in 1:length(data.eden)) {
>     data<-readOGR(x)
>     summary(data)
> }
>
> It gives me:  Error in readOGR(x) : missing layer

 readOGR takes the directory or folder with the shapefile and the
truncated shapefile name. So to load ca.shp that is in my current
directory I do:

 > ca=readOGR(".","ca")
 OGR data source with driver: ESRI Shapefile
 Source: ".", layer: "ca"
 with 58 features and 15 fields
 Feature type: wkbPoint with 2 dimensions

Technically the source is the directory, and ca.shp is the layer, in
OGR terminology.

 You might find readShapeSpatial() in maptools useful, it loads
shapefiles given the path to a .shp file:

 > ca2 = readShapeSpatial("ca.shp")

however ca and ca2 aren't identical - I think its just minor
differences in the attributes...

Barry

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list