# Create Raster file in R from Matrix and latlons

I could use some advice on how to create a Raster file in R with a Matrix of data points and the coordinates stored in a separate 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 difference, 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 length 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 coordinates and then make a long table `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!!!

• 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.) 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) May 10 '13 at 18:14

You can do

``````matrix <- matrix(1:16, nrow=4)
library(raster)
r <- raster(matrix)
# replace with correct coordinates
extent(r) <- c(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.

• 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! May 11 '13 at 11:09
• Note that matrix and raster consider columns and rows differently. That's why you need to transpose. See here: stackoverflow.com/questions/14513480/… Nov 13 '19 at 20:53
• For me this worked only if I used extent(r) <- extent (0, 1, 0, 1) Jun 2 '20 at 21:39
• There was supposed to be a `c`. Now fixed Jun 3 '20 at 5:30

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)
``````