looping sp data

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

looping sp data

ahelmore
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
Reply | Threaded
Open this post in threaded view
|

Re: looping sp data

Barry Rowlingson
On Fri, Jul 22, 2011 at 3:45 PM, ahelmore <[hidden email]> 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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: looping sp data

Kemp, Michael
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: [hidden email] [mailto:[hidden email]] On Behalf Of Barry Rowlingson
Sent: Friday, July 22, 2011 5:31 PM
To: ahelmore
Cc: [hidden email]
Subject: Re: [R-sig-Geo] looping sp data

On Fri, Jul 22, 2011 at 3:45 PM, ahelmore <[hidden email]> 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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: looping sp data

Samuel Turgeon
In reply to this post by Barry Rowlingson
Hi,

I resolved this kind of problem once by using:

eval(as.name(x))

so in your case:

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

Hope this helps,

Sam


I don't know if it's the solution for the  function readOGR. But for me it
was the solution with the function wrtie.ascii

On Fri, Jul 22, 2011 at 11:30 AM, Barry Rowlingson <
[hidden email]> wrote:

> On Fri, Jul 22, 2011 at 3:45 PM, ahelmore <[hidden email]> 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
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: looping sp data

ahelmore
In reply to this post by Kemp, Michael
Thanks, you guys.  With your input I finally put two and two together.  It
was a problem in both my looping structure and in readOGR's requirement
for the path and the file name to both be loaded as separate arguments.
Since I was tweaking only one thing at a time, I didn't hit the necessary
combination.

Here is what worked for me:

patt <- ".*eden.*dbf.*"
data.dbf <-list.files(pattern=patt) # list of eden .dbf data
data.eden <- gsub(".dbf","",data.dbf) #stripping the extension
for (x in data.eden) {
    data<-readOGR(wd2,x)
    summary(data)
}
OGR data source with driver: ESRI Shapefile
Source: "c:\myDir", layer: "DOQQ_Q1205nw_eden72600_3000pts"
with 3000 features and 1 fields
Feature type: wkbPoint with 2 dimensions
OGR data source with driver: ESRI Shapefile
Source: "c:\myDir", layer: "DOQQ_Q1205nw_eden74098_3000pts"
with 3000 features and...
(etc.)

I greatly appreciate your assistance.  It's hard to get around thinking
ruts sometimes.

Best regards,
Annie

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
Annette H. Elmore
Physical Scientist
Eastern Geographic Science Center
521 National Center
U.S. Geological Survey             [hidden email]
Reston, VA  20192                                      703 648 4805
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*

... So, friends, every day do something
that won't compute.  Love the Lord.
Love the world.  Work for nothing.
Take all that you have and be poor.
Love someone who does not deserve it.
Denounce the government and embrace
the flag.  Hope to live in that free
republic for which it stands...

Ask the questions that have no answers.
Invest in the millennium.  Plant sequoias...

-- Wendell Berry
     Manifesto:  The Mad Farmer Liberation Front



From:
"Kemp, Michael" <[hidden email]>
To:
Annette H Elmore <[hidden email]>
Date:
07/22/2011 12:05 PM
Subject:
RE: [R-sig-Geo] looping sp data



Hey Annette,
 
The error suggests that readOGR() is trying to take your argument
literally.  I wonder, first, if you can get readOGR() to work outside of
the loop if you give it a file name explicitly.
 
If so, you may need to use the evaluate function to force readOGR() to
evaluate your argument before executing the function.  That should look
something like?.
 
for (x in 1:length(data.eden)) {
      eval(parse(text=paste(?data <- readOGR(?, data.eden[x], ?)?,
sep=??)))
      summary(data)
 }
 
Maybe this helps?
Regards,
Michael
 
From: Annette H Elmore [mailto:[hidden email]]
Sent: Friday, July 22, 2011 5:41 PM
To: Kemp, Michael
Subject: RE: [R-sig-Geo] looping sp data
 

Hi Michael,

Hmm... still not there yet...

> for (x in 1:length(data.eden)) {
+      data<-readOGR(data.eden[x])
+      summary(data)
+ }
Error in readOGR(data.eden[x]) : missing layer
>
> for (x in data.eden) {
+      data<-readOGR(x)
+      summary(data)
+ }
Error in readOGR(x) : missing layer

Thanks for the suggestions though.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
Annette H. Elmore
Physical Scientist
Eastern Geographic Science Center
521 National Center
U.S. Geological Survey             [hidden email]
Reston, VA  20192                                      703 648 4805
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*

... So, friends, every day do something
that won't compute.  Love the Lord.
Love the world.  Work for nothing.
Take all that you have and be poor.
Love someone who does not deserve it.
Denounce the government and embrace
the flag.  Hope to live in that free
republic for which it stands...

Ask the questions that have no answers.
Invest in the millennium.  Plant sequoias...

-- Wendell Berry
    Manifesto:  The Mad Farmer Liberation Front


From:
"Kemp, Michael" <[hidden email]>
To:
Barry Rowlingson <[hidden email]>, ahelmore
<[hidden email]>
Cc:
<[hidden email]>
Date:
07/22/2011 11:37 AM
Subject:
RE: [R-sig-Geo] looping sp data
 




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: [hidden email] [
mailto:[hidden email]] On Behalf Of Barry Rowlingson
Sent: Friday, July 22, 2011 5:31 PM
To: ahelmore
Cc: [hidden email]
Subject: Re: [R-sig-Geo] looping sp data

On Fri, Jul 22, 2011 at 3:45 PM, ahelmore <[hidden email]> 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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo