Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I could use some advice on how to create a Raster file in R with a Matrix of datapoints and the coordinates stored in a seperate file. Both, data and coordinates come from a NetCDF file. I extracted the data and made some calculations that resulted in a matrix looking similar to this one:

NA NA NA 12 NA NA NA
NA 23 34 13 45 NA NA
23 12 12 23 34 54 56
NA NA NA NA 23 21 NA

With the diffrence, that there are 79 rows and 78 columns. The unprojected coordinates represent the centroids of the raster and can be extracted from the NetCDF file. Logically the longitude coordinates have a lengt of 79 and the latitude of 78. i saw a very good answer on a similar problem here on GIS Stack Exchange but I'm not quite sure if I can apply the solution to my problem since my data structure is different and my coordinates are not projected. Maybe one solution would be to project the coordenates and then make a longtable reshaping the data with the coordinates. But maybe there is also an easier solution...

csv files containing the data and the original netCDF can be found here. Any help would be very appreciated!!!

share|improve this question
1  
It's not clear what you want to accomplish. Do you want the output raster to represent your matrix of data or do you want it to be a reprojected version of such a raster? (Both question have indeed been answered here, btw: look into the Raster and gdal libraries.) –  whuber May 10 '13 at 13:48
    
@whuber I would like the output to be a raster yes, representing the data of the matrix. I thought of projection only in order to create the Raster as described in the post (see link above) –  Dspanes May 10 '13 at 18:14
add comment

2 Answers 2

up vote 3 down vote accepted

You can do

library(raster)
r <- raster(matrix)
# replace with correct coordinates
extent(r) <- (0, 1, 0, 1)
r <- writeRaster(r, 'filename.tif')

But your question suggests that you would have been better of accessing the ncdf file as a RasterLayer object (and avoid creating matrices and keeping track of coordinates)

 pft <- raster("pft_harvest_maize.nc")

etc.

share|improve this answer
    
worked for me! thanks for the hint! I tried to work with the raster package too but got a bunch of complications, thats why I extracted the data without geoinformation. I had to rotate the matrix first like this : rotate1<-t(maize.anmean.1995.2004)and rotate2<-raster(rotate1[nrow(rotate1):1,]) in order to get the coordinates right. But than it worked smoothly! –  Dspanes May 11 '13 at 11:09
add comment

You may use something like the code below to create raster from matrix. Notice that no georeference information will be created here.

library(rgdal)
raster <- your_matrix
height <- 79
width <-78
tif_driver <- new("GDALDriver", "GTiff")
tif <- new("GDALTransientDataset", tif_driver, height, width, 1, 'Byte')
pattern <- matrix(raster, width, height)
bnd1 <- putRasterData(tif, pattern)
tif_file <- "result.tif"
saveDataset(tif, tif_file)
GDAL.close(tif)
GDAL.close(tif_driver)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.