On Tue, 29 Jan 2013, Irucka Embry wrote:

Hi Don MacQueen, I want to thank you for your response.

I received assistance from the main R help mailing list with this code
suggestion: maxdepth.plys <- lapply(modeldepthsmore, function(x)
Grid2Polygons(x, level = FALSE)).

You posted to the second list when nobody replied within two hours - some of us sleep at some time of the day. The two replies are equivalent, and both indicate that either you didn't read the documentation of lapply, or didn't understand what you read.

I have not allowed that code suggestion or your suggestion to complete all 54 polygons because it has taken too long. Thus I thought about revising the code that you provided me to save each polygon to a file so that I can know which files have been completed and which ones have not been yet.


Wrong, you mean object, not file. You also need to learn about trapping errors. Simplify what you are doing as a loop, as most of this is confusing because you've written it in a confusing way (don't write a single line function, it is pointless). In addition, you have not said what you are actually doing.

You are reading 54 ASCII Grid files into SpatialGridDataFrame objects, then using Grid2Polygons in the Grid2Polygons package to make a simple conversion of each input grid cell (pixel) to a Polygons object. The output SpatialPolygonsDataFrame objects are simple copies of the input, so you do not use the functionality of Grid2Polygons to aggregate to blocks of pixels; you could use coercion in sp instead.

However, in your message to R-help, you showed that your goal was to use BayesX. So is your actual aim to stack 54 rasters (possibly 54 time periods???), create a single set of SpatialPolygons, output to Shapefile, read in through the functions in BayesX (which are not based on sp classes - had they been, you wouldn't need to write out and read in again), then run some space-time analysis?

Or are the 54 grids all different - in which case get your whole workflow functioning with a single raster first, and then iterate that in a loop, storing results in a list. If your raster has many cells, you may find that the time it takes to run BayesX makes the time taken to convert SpatialGrid -> SpatialPolygons very small, but then again, it could be the use of Grid2Polygons when its advanced features are not needed:

library(Grid2Polygons)
GT <- GridTopology(c(0.5, 0.5), c(1,1), c(100, 100))
SGDF <- SpatialGridDataFrame(GT, data=data.frame(x=runif(10000)))
system.time(G2P <- Grid2Polygons(SGDF, level=FALSE))
   user  system elapsed
 36.129   0.832  37.013
system.time(SPDF <- as(SGDF, "SpatialPolygonsDataFrame"))
   user  system elapsed
  2.875   0.043   2.945

If the SpatialGridDataFrame objects all share the same GridTopology, you should make a single SpatialGridDataFrame in a loop, or a single RasterBrick or RasterStack in the raster package, and go from there. But your proposed workflow is so opaque that helping you fix lapply isn't going to move you forward at all.

You must think through your workflow carefully - it is very possible that you can create the BayesX bnd object directly from the GridTopology of the input data, without any intermediate SpatialPolygons objects or shapefiles. The authors of BayesX would have done everyone a big favour if they had supported sp and spacetime classes, for example by coercion - spatstat is a good example of how to use both sp and package-specific classes, and possibly used facilities in sp and spacetime for visualisation.

Roger

I have tried to update the previous R code to save each output file to a
folder:

library(maptools)
library(Grid2Polygons)

readfunct <- function(x)
{
u <- readAsciiGrid(x)
}

modfilesmore <- paste0("MaxFloodDepth_", 1:54, ".txt")
modeldepthsmore <- lapply(modfilesmore, readfunct)

myfolder <- paste0("/MaxFloodDepthImages/MaxFloodDepthPolygon_", 1:54)
maxdepth.plys <- lapply(modeldepthsmore, Grid2Polygons, level=FALSE,
cat, file = "myfolder", append = TRUE)

Error in FUN(X[[1L]], ...) :
unused argument(s) (file = "myfolder", append = TRUE)


<-----Original Message----->
From: MacQueen, Don [[email protected]]
Sent: 1/28/2013 12:34:56 PM
To: [email protected];[email protected]
Subject: Re: [R-sig-Geo] lapply and SpatialGridDataFrame

I suspect you're not passing arguments correctly to lapply()

I'd try
maxdepth.plys <- lapply(modeldepthsmore, Grid2Polygons, level=FALSE)

-Don



--
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 1/27/13 2:47 AM, "Irucka Embry" <[email protected]> wrote:

Hi all, I have a set of 54 files that I need to convert from ASCII
grid
format to .shp files to .bnd files for BayesX.

I have the following R code to operate on those files:

library(maptools)
library(Grid2Polygons)

readfunct <- function(x)
{
u <- readAsciiGrid(x)
}

modfilesmore <- paste0("MaxFloodDepth_", 1:54, ".txt")
modeldepthsmore <- lapply(modfilesmore, readfunct)

maxdepth.plys <- lapply(modeldepthsmore,
Grid2Polygons(modeldepthsmore,
level = FALSE))

layers <- paste0("examples/floodlayers_", 1:54)
writePolyShape(maxdepth.plys, layers)
shpName <- sub(pattern="(.*)\\.dbf", replacement="\\1",
x=system.file("examples/Flood/layer_.dbf", package="BayesX"))
floodmaps <- shp2bnd(shpname=shpName, regionnames="SP_ID")

## draw the map
drawmap(map=floodmaps)


This is the error message that I receive:
maxdepth.plys <- lapply(modeldepthsmore,
Grid2Polygons(modeldepthsmore, level = FALSE))
Error in Grid2Polygons(modeldepthsmore, level = FALSE) :
Grid object not of class SpatialGridDataFrame


Can someone assist me in modifying the R code so that I can convert
the
set of files to .shp files?

Thank-you.

Irucka Embry

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


<span id=m2wTl><p><font face="Arial, Helvetica, sans-serif" size="2" 
style="font-size:13.5px">_______________________________________________________________<BR>Get the Free email that has everyone talking at <a href=http://www.mail2world.com 
target=new>http://www.mail2world.com</a><br>  <font color=#999999>Unlimited Email Storage &#150; POP3 &#150; Calendar &#150; SMS &#150; Translator &#150; 
Much More!</font></font></span>
        [[alternative HTML version deleted]]

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


--
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [email protected]

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

Reply via email to