0

I'm having trouble rasterizing a data.frame using the raster package in R.

My data.frame contains environmental data for the world oceans (temperature etc.) with their coordinates (grid 0.5*0.5), decimal longitude from -90 to 90 and decimal latitude from -180 to 180. So the base contains 90*2*2 x 180*2*2 = 360*720 = 259200 rows, and 59 colones (57 variables + 2 colones of coordinates).

After rasterizing, this is what I get with plot(r): http://postimg.org/image/rqocxcbi3/

So, a duplicate image, in the wrong direction.

My code is:

FILE_ENV = read.csv('ENV_DATABASE.csv')
coordinates(FILE_ENV) <- ~LON+LAT
proj4string(FILE_ENV3)=CRS("+init=epsg:4326")
FILE_ENV = spTransform(FILE_ENV,CRS("+init=epsg:4326"))
gridded(FILE_ENV) = TRUE
r = raster(FILE_ENV)

plot(r)

Can anyone see what I'm missing/screwing up here?

Thanks

Edit : head(FILE_ENV)

LON LAT BAT BAT_CLASSE SLOPE SEDIMENT SST SST_SEAFLOOR SST_SUM SST_WIN SAL_SURF SAL_SEAFLOOR...

1 -179.75 89.75 2804 NA 0.14031838 NA NA NA NA NA NA NA

2 -179.25 89.75 2941 NA 0.12495525 NA NA NA NA NA NA NA

3 -178.75 89.75 3048 NA 0.07784129 NA NA NA NA NA NA NA

4 -178.25 89.75 3093 NA 0.03123910 NA NA NA NA NA NA NA

5 -177.75 89.75 3109 NA 0.01536359 NA NA NA NA NA NA NA

6 -177.25 89.75 3063 NA 0.15619729 NA NA NA NA NA NA NA

Spes Alpha
  • 27
  • 1
  • 9
  • Sounds like you need a transpose in there somewhere, I don't know if the standard `t()` works on raster objects, but worth a try. – Gregor Thomas May 30 '15 at 00:38
  • Thanks! I tried it but I still get a duplicated plot.. – Spes Alpha Jun 01 '15 at 18:21
  • `spTransform` is for doing coordinate transformations -- you don't need that. My guess is that the `raster` constructor does not understand coordinates of your FILE_ENV object. Break it down into more explicit pieces for `raster` – aaryno Jun 02 '15 at 23:21

2 Answers2

0

Since you did not show head(FILE_ENV) (d in my code) it hard to say how exactly you should to this. But in essence you should be able to do something like the below:

library(raster)
d <- read.csv('ENV_DATABASE.csv')
r <- rasterFromXYZ(d, crs="+proj=longlat +datum=WGS84")
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thanks! I edited my first message to add `head(FILE_ENV)`. The result improved with your code but I still get the same problem (duplicated and rotated image) : http://s27.postimg.org/6krcblg43/Rplot2.png – Spes Alpha Jun 01 '15 at 17:04
  • I totally recreated the coordinates colones of my database and it works now ; I still don't know where the problem came from but it was obviously in the database – Spes Alpha Jun 02 '15 at 23:31
0

I would think rasterfromXYZ would have worked. That said, it seems like it works best when working with raster to break things down into smaller, more explicit chunks. It seems that the constructor is assuming you want something different out of your object and it's losing the coordinates for your matrix entries.

This isn't the best practice but since you know your data is a strict grid we can turn the entries into points and then rasterize them:

library(raster)
coords <- coordinates(FILE_ENV[,c("LON","LAT")])
spdf <- SpatialPointsDataFrame(coords,data=FILE_ENV)
r <- rasterize(spdf,field='value',raster(nrow=360,ncol=720))

Now r should contain your raster.

aaryno
  • 606
  • 5
  • 13