0

I have a .txt file which consists of 160 rows and 320 columns. It covers the geographic region from 34 to 41.95 in latitude and 19 to 34.95 in longitude. Its spatial resolution is 0.05 x 0.05. I use the following code to read the .txt file:

a1 = as.matrix(read.table("/home/...", header=F, sep = "\t",   as.is=TRUE))
rast1 = raster(a1)
extent(rast1) = c(34,41.95,19,34.95)
projection(rast1) = CRS("+proj=longlat +datum=WGS84")

Which gives me the following result:

class       : RasterLayer 
dimensions  : 160, 320, 51200  (nrow, ncol, ncell)
resolution  : 0.02484375, 0.0996875  (x, y)
extent      : 34, 41.95, 19, 34.95  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 0, 245.2738  (min, max)

The resolution given above is not correct. When I try to resolve this issue by running:

res(rast1) = c(0.05, 0.05)

I get the following result:

class       : RasterLayer 
dimensions  : 319, 159, 50721  (nrow, ncol, ncell)
resolution  : 0.05, 0.05  (x, y)
extent      : 34, 41.95, 19, 34.95  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 

In that case the number of rows and columns in reversed and diminished by 1! Also, all the cells have NA's as values.

Could you please explain to me why this happens and how can I properly read my .txt file and transform it to raster?

2

First, given your extent, you appear to have your row/col dimensions reversed. If I switch them and apply the extent I automatically get very close (0.049) to your desired resolution (0.050). I would point out that some spatial array data is flipped so, this may not be your mistake per se.

library(raster)

nrows = 160 
ncols = 320 
r.mat <- matrix(data = runif(nrows*ncols), nrow = nrows, ncol = ncols)
r <- raster(r.mat)
extent(r) <- extent(c(34,41.95,19,34.95))
res(r)


nrows = 320 
ncols = 160 
r.mat <- matrix(data = runif(nrows*ncols), nrow = nrows, ncol = ncols)
r <- raster(r.mat)
extent(r) <- extent(c(34,41.95,19,34.95))
res(r)

Now, getting exactly to 0.05 will require you fiddling with the values in your defined extent. This could be a precision issue in the raster function or an error on your part but your defined extent is consistently dropping a row and column causing a mismatch between your matrix and raster dimensions. If you can find the correct expansion then you can fit in these two missing dimensions. In whatever metadata your are pulling the extent from, you may be inadvertently dropping decimal precision thus causing the dimensions to be slightly off.

  • For example purposes, I created a matrix object "r.mat" which would be the same as your "a1". Look at dim(a1) to check your row/col dimensions. If the data is flipped you can use transpose t(a1) to flip it to the correct dimensions. There is also a "flip" function in the raster package that allows you to control the direction that the array is flipped. – Jeffrey Evans Feb 18 '16 at 18:37
  • Just ignore the example matrix and associated syntax. Since you did not provide data, I am just creating a random matrix of the same dimensions as your matrix. Just read in your matrix the way you were and substitute r.mat with a1. – Jeffrey Evans Feb 18 '16 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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