Hello, Is there anyone who could give me an example of how to import an ascii grid (i.e. ArcGIS exported raster) into R. I want to use it with gstat but don't know the appropriate import routine. Thanks very much for your help. Regards, femke Femke Reitsma Graduate Student (ABD) Geography Department 2181 LeFrak Hall University of Maryland College Park, MD 20742 Phone: 301-405-4121 E-mail: femke@geog.umd.edu http://www.glue.umd.edu/~femke/ [[alternative HTML version deleted]]
On Tue, 17 Feb 2004, femke wrote:> Hello, > > Is there anyone who could give me an example of how to import an ascii > grid (i.e. ArcGIS exported raster) into R. I want to use it with gstat > but don't know the appropriate import routine.On a Unix/Linux platform, you can use the rgdal library (after having compiled and installed the external GDAL libraries). But if you look at the grid file in a text viewer/editor, you'll see that its format is very simple, so reading using connections is quite possible. See> ?connectionsand> ?readLinesshould do it in a platform-neutral way.> > Thanks very much for your help. > > Regards, > > femke > > > Femke Reitsma > Graduate Student (ABD) > Geography Department > 2181 LeFrak Hall > University of Maryland > College Park, MD 20742 > Phone: 301-405-4121 > E-mail: femke at geog.umd.edu > http://www.glue.umd.edu/~femke/ > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger Bivand Econonic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway, voice: +47-55959355, fax: +47-55959393; Roger.Bivand at nhh.no
If you have exported the grid from Arc using the asciigrid command then you can read it in with scan or read.table. You can tell R to skip the six lines of header info and to convert -9999 to NA e.g., $ snep.tmin <- read.table(file = "tmin.asc", sep = " ", na.strings "-9999", skip = 6) Check the number of rows and columns to make sure it matches your data (in Windows, Arc puts a space before the line rturn at the end of a row making the resulting R object have one too manty columns.) If so then remove it: $ snep.tmin <- snep.tmin[,-ncol(snep.tmin)] (If there is a work around for the read.table command that somebody else uses then I'd love to hear it.) For gstat, it is helpful to put the grid into a vector and attach the coordinate information in a data.frame>From the header information take the lower left corner and make yourcoordinate columns and join it to the grid data. e.g., $ xLLcorner <- -1855500 $ yLLcorner <- -944500 $ cellsize <- 1000 $ $ xURcorner <- xLLcorner + (cellsize * (ncol(snep.tmin) - 1)) $ xLRcorner <- xURcorner $ xULcorner <- xLLcorner $ $ yULcorner <- yLLcorner + (cellsize * (nrow(snep.tmin) - 1)) $ yURcorner <- yULcorner $ yLRcorner <- yLLcorner $ $ coords <- expand.grid(y = seq(yULcorner, yLRcorner, by = -1000), + x = seq(xULcorner, xLRcorner, by = 1000)) $ $ tmin.frame <- data.frame(coords, tmin = as.vector(c(snep.tmin, recursive = T))) $ $>From there you can krige or whatever easily.HTH, Andy $ version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 1 minor 8.1 year 2003 month 11 day 21 language R
If you have exported the grid from Arc using the asciigrid command then you can read it in with scan or read.table. You can tell R to skip the six lines of header info and to convert -9999 to NA e.g., $ snep.tmin <- read.table(file = "tmin.asc", sep = " ", na.strings "-9999", skip = 6) Check the number of rows and columns to make sure it matches your data (in Windows, Arc puts a space before the line return at the end of a row making the resulting R object have one too many columns.) If that happens, then remove it: $ snep.tmin <- snep.tmin[,-ncol(snep.tmin)] (If there is a work around for the read.table command that somebody else uses then I'd love to hear it.) For gstat, it looks like it would be helpful to put the grid into a vector and attach the coordinate information in a data frame? If so, from the header information take the lower left corner and make your coordinate columns and join it to the grid data. e.g., $ xLLcorner <- -1855500 $ yLLcorner <- -944500 $ cellsize <- 1000 $ $ xURcorner <- xLLcorner + (cellsize * (ncol(snep.tmin) - 1)) $ xLRcorner <- xURcorner $ xULcorner <- xLLcorner $ $ yULcorner <- yLLcorner + (cellsize * (nrow(snep.tmin) - 1)) $ yURcorner <- yULcorner $ yLRcorner <- yLLcorner $ $ coords <- expand.grid(y = seq(yULcorner, yLRcorner, by = -1000), + x = seq(xULcorner, xLRcorner, by = 1000)) $ $ tmin.frame <- data.frame(coords, tmin = as.vector(c(snep.tmin, recursive = T))) $ $ Watch your signs depends on the coordinate system. From there you can krige or whatever easily. HTH, Andy $ version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 1 minor 8.1 year 2003 month 11 day 21 language R
No problem. I'm glad it worked. It would be pretty easy to package as a function for gstat. -Andy> -----Original Message----- > From: femke [mailto:femke at geog.umd.edu] > Sent: Thursday, February 19, 2004 2:54 PM > To: Andy Bunn > Subject: Re: [R] importing ascii grids (for gstat) > > > > Thanks very much for your help on this. Your directions > worked beautifully. Now if only somone would put that in a > standard function for a package like gstat. > > If you don't mind, I'll post it to the gstat list > (referencing you as the source). > > Cheeers, > > femke > > ----- Original Message ----- > From: "Andy Bunn" <abunn at montana.edu> > To: "'femke'" <femke at geog.umd.edu>; <r-help at stat.math.ethz.ch> > Sent: Tuesday, February 17, 2004 12:00 PM > Subject: RE: [R] importing ascii grids (for gstat) > > > > If you have exported the grid from Arc using the asciigrid command > > then you can read it in with scan or read.table. You can tell R to > > skip the six lines of header info and to convert -9999 to NA e.g., > > > > $ snep.tmin <- read.table(file = "tmin.asc", sep = " ", > na.strings = > > "-9999", skip = 6) > > > > Check the number of rows and columns to make sure it > matches your data > > (in Windows, Arc puts a space before the line return at the > end of a > > row making the resulting R object have one too many columns.) > > > > If that happens, then remove it: > > > > $ snep.tmin <- snep.tmin[,-ncol(snep.tmin)] > > > > (If there is a work around for the read.table command that somebody > > else uses then I'd love to hear it.) > > > > For gstat, it looks like it would be helpful to put the grid into a > > vector and attach the coordinate information in a data > frame? If so, > > from the header information take the lower left corner and > make your > > coordinate columns and join it to the grid data. e.g., > > > > $ xLLcorner <- -1855500 > > $ yLLcorner <- -944500 > > $ cellsize <- 1000 > > $ > > $ xURcorner <- xLLcorner + (cellsize * (ncol(snep.tmin) - 1)) $ > > xLRcorner <- xURcorner $ xULcorner <- xLLcorner > > $ > > $ yULcorner <- yLLcorner + (cellsize * (nrow(snep.tmin) - 1)) > > $ yURcorner <- yULcorner > > $ yLRcorner <- yLLcorner > > $ > > $ coords <- expand.grid(y = seq(yULcorner, yLRcorner, by = -1000), > > + x = seq(xULcorner, xLRcorner, by = 1000)) > > $ > > $ tmin.frame <- data.frame(coords, tmin = as.vector(c(snep.tmin, > > recursive = T))) $ $ > > > > Watch your signs depends on the coordinate system. From > there you can > > krige or whatever easily. > > > > HTH, Andy > > > > > > > > $ version > > _ > > platform i386-pc-mingw32 > > arch i386 > > os mingw32 > > system i386, mingw32 > > status > > major 1 > > minor 8.1 > > year 2003 > > month 11 > > day 21 > > language R > > > >