[R-sig-Geo] Reading an Ascii file

Juliane Struve juliane_struve at yahoo.co.uk
Thu Jan 15 15:42:11 CET 2009


Dear Kamran,

this solution works fine for me but I have noticed that plot() does not work on the resulting dataframe. I am trying to read a variety of shape files and raster data sets into R for the purpose of running a model using the geographic info as inputs. I am using readOGR and plot() for my other shapefiles. What do I need to do to the SpatialGridDataFrame created in your script in order to use plot()  ? 

Sorry to ask such simplistic questions, but I am not that familar yet with with R or spatial analysis.

many thanks and best wishes,

Juliane 

 Dr. Juliane Struve
Environmental Scientist
10, Lynwood Crescent
Sunningdale SL5 0BL
01344 620811 



----- Original Message ----
From: Kamran Safi <kamran.safi at ioz.ac.uk>
To: "van Etten, Jacob (IRRI)" <J.vanEtten at cgiar.org>; Robert Hijmans <r.hijmans at gmail.com>
Cc: R-sig-Geo at stat.math.ethz.ch
Sent: Thursday, 15 January, 2009 9:20:04
Subject: Re: [R-sig-Geo] Reading an Ascii file

Good comment, and actually if the number of lines should be important, I can do it in two lines ;-)

library(maptools)
table <- data.frame(readAsciiGrid("<your file and its path>"))

I realized that you don't need rgdal.


Kamran


-----Original Message-----
From: r-sig-geo-bounces at stat.math.ethz.ch on behalf of van Etten, Jacob (IRRI)
Sent: Thu 15.01.2009 08:34
To: Robert Hijmans
Cc: R-sig-Geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] Reading an Ascii file

The case for a as.data.frame(signature=RasterStack) method (then you
would have three lines (one less than Kamran)

-----Original Message-----
From: r-sig-geo-bounces at stat.math.ethz.ch
[mailto:r-sig-geo-bounces at stat.math.ethz.ch] On Behalf Of Kamran Safi
Sent: Thursday, January 15, 2009 3:28 PM
To: Robert Hijmans; Juliane Struve
Cc: R-sig-Geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] Reading an Ascii file

Hi all,

This should deliver the results Julie is looking for:

library(rgdal)
library(maptools)
grid <- readAsciiGrid("<your file and its path>")
table <- data.frame(grid)

That was it! Replace s1 and s2 with X and Y.

Kamran



------------------------
Kamran Safi

Postdoctoral Research Fellow
Institute of Zoology
Zoological Society of London
Regent's Park
London NW1 4RY

http://www.zoo.cam.ac.uk/ioz/people/safi.htm

http://spatialr.googlepages.com
http://asapi.wetpaint.com



-----Original Message-----
From: r-sig-geo-bounces at stat.math.ethz.ch on behalf of Robert Hijmans
Sent: Thu 15.01.2009 03:13
To: Juliane Struve
Cc: R-sig-Geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] Reading an Ascii file

Dear Julie,

This is one way to do that, using the raster package (under
development):


install.packages("raster",
repos="http://R-Forge.R-project.org<http://r-forge.r-project.org/>
")

require(raster)
r <- rasterFromFile("d:/alt.asc", TRUE)
xy <- xyFromCell(r, 1:ncells(r))
xyv <- cbind(xy, values(r))

xyv[1:10,]

# To get rid of the NAs

xyv <- na.omit(xyv)
xyv[1:10,]




On Thu, Jan 15, 2009 at 1:01 AM, Juliane Struve
<juliane_struve at yahoo.co.uk>wrote:

> Dear list,
>
> I am trying to read ArcGIS raster data sets into R after converting
them
> into ASCII .txt file. The text file I am trying to read contains
>
> ncols        250
> nrows        276
> xllcorner    344147.19411119
> yllcorner    3024657.6122834
> cellsize      0.19970250725746
> NODATA_value  -9999
>
> in the first lines followed by a grid of values that are either -9999
or 1.
>
> I have used read.table("myfile.txt") which yields this (first 10
lines).
>
>
> V1            V2
> 1          ncols  2.500000e+02
> 2          nrows  2.760000e+02
> 3      xllcorner  3.441472e+05
> 4      yllcorner  3.024658e+06
> 5      cellsize  1.997025e-01
> 6      NODATA_value -9.999000e+03
> 7          -9999 -9.999000e+03
> 8          -9999 -9.999000e+03
> 9          -9999 -9.999000e+03
> 10        -9999 -9.999000e+03
>
> I would like to have the data as a table with coordinates for each
cell and
> the actual value.
>
> Could somebody advise me which function I should use ?
>
> Many thanks,
>
> Juliane
>
>
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

    [[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




Click https://www.mailcontrol.com/sr/wQw0zmjPoHdJTZGyOCrrhg==  to report
this email as spam.



The Zoological Society of London is incorporated by Royal Charter
Principal Office England. Company Number RC000749
Registered address: 
Regent's Park, London, England NW1 4RY
Registered Charity in England and Wales no. 208728 

________________________________________________________________________
_
This e-mail has been sent in confidence to the named\ ad...{{dropped:8}}

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




Click https://www.mailcontrol.com/sr/wQw0zmjPoHdJTZGyOCrrhg==  to report this email as spam.



The Zoological Society of London is incorporated by Royal Charter
Principal Office England. Company Number RC000749
Registered address: 
Regent's Park, London, England NW1 4RY
Registered Charity in England and Wales no. 208728 

_________________________________________________________________________
This e-mail has been sent in confidence to the named addressee(s).
If you are not the intended recipient, you must not disclose or distribute
it in any form, and you are asked to contact the sender immediately.
Views or opinions expressed in this communication may not be those
of The Zoological Society of London and, therefore, The Zoological
Society of London does not accept legal responsibility for the contents
of this message. The recipient(s) must be aware that e-mail is not a
secure communication medium and that the contents of this mail may
have been altered by a third party in transit.
If you have any issues regarding this mail please contact:
administrator at zsl.org.
___________________________________________________________________________





    [[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








More information about the R-sig-Geo mailing list