multi band to single band for raster in R - r

I have the following raster:
class : RasterLayer
dimensions : 1446, 1243, 1797378 (nrow, ncol, ncell)
resolution : 1000, 1000 (x, y)
extent : 4210000, 5453000, 3372000, 4818000 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs
data source : -
names : ALL_2016_year_Raster
values : 0, 36293 (min, max)
It seems like it is a multiband raster but I would like to export it as a single band raster. when I import it, i used raster <- raster(file, band=1) but it does not change anything...
Is there a way to make it single band?

Related

Unable to do raster operations in R

Hi my raster values for a Raster Layer are the following:
dimensions : 2225, 2286, 5086350 (nrow, ncol, ncell)
resolution : 0.03333146, 0.03333146 (x, y)
extent : -20.86612, 55.32961, -35.40306, 38.75945 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : solar.tif
names : solar
values : 0, 2855 (min, max)
However whenever I try to do simple raster operations such as:
plot(solar)
It returns this error:
Error in setValues(outras, m) :
could not find symbol "values" in environment of the generic function
Thanks for any help

Calculating a CHM with dtm and dsm with different resolutions

I have a DTM and DSM with different resolutions.
Here are the summaries of each Raster layer.
> raster_dsm
class : RasterLayer
dimensions : 2001, 2501, 5004501 (nrow, ncol, ncell)
resolution : 0.5, 0.5 (x, y)
extent : -112500.2, -111249.8, 388999.8, 390000.2 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=0 +lon_0=16.33333333333333 +k=1 +x_0=0 +y_0=-5000000 +ellps=bessel +units=m +no_defs
data source : D:/Test_Raster/DSM/dsm.asc
names : dsm
>raster_dtm
class : RasterLayer
dimensions : 1001, 1251, 1252251 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : -112500.5, -111249.5, 388999.5, 390000.5 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=0 +lon_0=16.33333333333333 +k=1 +x_0=0 +y_0=-5000000 +ellps=bessel +units=m +no_defs
data source : D:/Test_Raster/DTM/dtm.asc
names : dtm
As you can see, the resolution of the dtm is 1 m and the resolution of the dsm is 0.5m.
I want to calculate a Crown Heigth Model (CHM).
The easiest way is to
CHM = dsm - dtm
But when I try in R the following error code appear:
Error in compareRaster(e1, e2, extent = FALSE, rowcol = FALSE, crs = TRUE, :
different resolution
Is there a simple way to ignore the resolution? Or must I do a resampling of the data, before further calculation?
In ArcGis you can do this kind of raster calculation easily, because you don't have to resample the data first.
Any suggestions will be appreciated!
Yes, Arc*** will do this for you, but what does it actually do? I think it is better to avoid that kind of ambiguity. In this case you cannot use dis/aggregate because the extents are different. So you need to use resample

Reprojecting list of raster stacks

I have a big list of raster stacks and I want to reproject and then clip them in R. I have done the same procedure in ArcGIS using batch processing which was significantly faster.
It is not going well in R. Any suggestion to improve the process?
my rstack.lst consist of 19 raster stacks like this:
class : RasterStack
dimensions : 4800, 7200, 34560000, 46 (nrow, ncol, ncell, nlayers)
resolution : 463.3127, 463.3127 (x, y)
extent : 11119505, 14455357, -5559753, -3335852 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs
names : ...
I am using this code:
for (i in 1:length(rstack.lst)){
rstack.lst[[i]] <- projectRaster(rstack.lst[[i]], crs = m.crs) # reproject
rstack.lst[[i]] <- crop(rstack.lst[[i]], brdshp) # crop
rstack.lst[[i]] <- mask(rstack.lst[[i]], brdshp) # mask
print (i)
}
not even one I is printed after two hours!

Make raster stack with different extent

I am in trouble making raster stack which have slightly different extent. The answer (1st one) given here is useful but did not help in my case. For example, I want to make a raster stack using bio2 raster for Australia and this Australian raster. The second raster comes for Australia only and the first one is global. So I cropped the global bio2 raster to the same extent of Australian raster using crop() function, but the resultant raster extent (i.e., bio2.au) is slightly different (therefore, I cannot make raster using the cropped raster and the Australian raster, awc). Sample code is below:
library(raster)
awc <- raster("path to Australian raster")
bio2.g <- raster("path to Bio2 global raster")
# crop bio2.g to the same extent of awc
bio2.au <- crop(bio2.g, extent(awc))
# make a raster stack
st <- stack(awc, bio2.au)
Error in compareRaster(x) : different extent
I have also tried using quick=TRUE within the stack() function. But in this case the cell values in awc is lost. Note: the size of awc raster is 4gb.
# first make a list of rasters saved in the computer
li <- list.files("path to file", pattern = ".tif$", full.names = TRUE)
st <- stack(li, quick=TRUE)
st[[1]] # no cell values for awc
Your suggestions will be highly appreciated. My ultimate goal is to crop several bioclim rasters to the same extent of Australian raster awc and stack them together so that raster cell values are not lost.
Edit (after comment of #Cobin):
Below is the attribute of each raster
# global raster (bigger raster)
> r
class : RasterLayer
dimensions : 21600, 43200, 933120000 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:\Worldclim2_Bioclim\wc2.0_bio_30s_02.tif
names : wc2.0_bio_30s_02
values : 0, 37.06667 (min, max)
# Australian raster (smaller raster)
> r1
class : RasterLayer
dimensions : 43201, 49359, 2132358159 (nrow, ncol, ncell)
resolution : 0.0008333333, 0.0008333333 (x, y)
extent : 112.8921, 154.0246, -44.00042, -7.999583 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:\SoilAWC5cm.EV1.tif
names : SoilAWC5cm.EV1
values : 2.997789, 27.86114 (min, max)
# new raster, after crop() function is applied
> r2 <- crop(r,extent(r1))
> r2
class : RasterLayer
dimensions : 4320, 4936, 21323520 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 112.8917, 154.025, -44, -8 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:\Users\Anwar\AppData\Local\Temp\Rtmpmg9fyF\raster\r_tmp_2018-11-23_164300_11308_65747.grd
names : wc2.0_bio_30s_02
values : 1.933333, 18.15833 (min, max)
# rebuild r2 to match r1
> r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1],ncols=dim(r1)[2])
Error in setValues(r, vals) :
length(values) is not equal to ncell(x), or to 1
I suppose that the extent of two raster are differet though the raster masked by crop function.You
should check the both of awc and bio.au extent base on same reolution, rows and columns. Because I couldn't download data from
hyperlink, I give an example of my own data.
r <- raster('/big_raster')
r1 <- raster('/small_raster')
r2 <- crop(r,extent(r1))
r1
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.0833333, 0.0833333 (x, y)
extent : 89.2185, 102.3018, 30.96238, 37.12905 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:\D\temp\Rtest\modis8km.tif
names : modis8km
values : -32768, 32767 (min, max)
r2
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.08333333, 0.08333333 (x, y)
extent : 89.25, 102.3333, 31, 37.16667 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : g201401a
values : -32768, 7789 (min, max)
Though r1 and r1 with same resolution and dimension, the extent have tiny offset. It cause stack error.
stack(r1,r2)
Error in compareRaster(x) : different extent
So, you should rebuid the r2 to match r1:
r22 <- raster(vals=values(r2),ext=extent(r1),crs=crs(r1),
nrows=dim(r1)[1],ncols=dim(r1)[2])
Now stack(r22,r1) will be successful.

How can I get the cell number of a raster using extent information?

I have a raster and I am using the raster package.
class : RasterLayer
dimensions : 103, 118, 12154 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -83.075, -82.09167, 34.95833, 35.81667 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:\fb.tif
names : fdr_fb
values : 1, 128 (min, max)
I know how to subset and all. But how can I find the cellnumber (preferred) or cellvalue by using the Lat-Long value?
For example, I can find cell value using lat/long:
extract(ras,SpatialPoints(cbind(-82.8,35.2)))
But I want to find the cell number (row,col) corresponding to (Say) Long= -82.1 and Lat= 35.0
Raster: https://www.dropbox.com/s/8nhfirxr2hm3l4v/fdr_fb.tif?dl=0
To get the cell number from a point, you can do:
cellFromXY(ras, cbind(-82.8, 35.2))
If you have an Extent object e you can do:
cellsFromExtent(ras, e)

Resources