I have an image with 4 channels and I separate each channel into a separate file. When I combine them into one plus NDVI, why does rasterstack show an error? Whereas in other images stacking can be done.
This is the information about my data
> diff_ndvi_1
class : RasterLayer
dimensions : 9219, 9280, 85552320 (nrow, ncol, ncell)
resolution : 3, 3 (x, y)
extent : 667737, 695577, 68127, 95784 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=47 +datum=WGS84 +units=m +no_defs
source : memory
names : layer
values : -0.8249686, 1.061913 (min, max)
> imgdif_1_2
class : RasterStack
dimensions : 9219, 9279, 85543101, 1 (nrow, ncol, ncell, nlayers)
resolution : 3, 3 (x, y)
extent : 667740, 695577, 68127, 95784 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=47 +datum=WGS84 +units=m +no_defs
names : layer
min values : -6226
max values : 5012
> imgdif_1_4
class : RasterStack
dimensions : 9219, 9280, 85552320, 1 (nrow, ncol, ncell, nlayers)
resolution : 3, 3 (x, y)
extent : 667737, 695577, 68127, 95784 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=47 +datum=WGS84 +units=m +no_defs
names : layer
min values : -6475
max values : 6590
> imgdif_1_6
class : RasterStack
dimensions : 9219, 9280, 85552320, 1 (nrow, ncol, ncell, nlayers)
resolution : 3, 3 (x, y)
extent : 667737, 695577, 68127, 95784 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=47 +datum=WGS84 +units=m +no_defs
names : layer
min values : -7441
max values : 9369
> imgdif_1_8
class : RasterStack
dimensions : 9219, 9280, 85552320, 1 (nrow, ncol, ncell, nlayers)
resolution : 3, 3 (x, y)
extent : 667737, 695577, 68127, 95784 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=47 +datum=WGS84 +units=m +no_defs
names : layer
min values : -5951
max values : 10349
I've tried restarting from scratch and looking at the extents and projection system via QGIS but the result is the same, it still doesn't work.
Related
I would like to project one raster to another.
While doing so all the values get lost.
This is the original raster rb I would like to project:
class : RasterLayer
dimensions : 900, 900, 810000 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : -523.4622, 376.5378, -4658.645, -3758.645 (xmin, xmax, ymin, ymax)
crs : +proj=stere +lat_0=90 +lon_0=10 +k=0.93301270189 +x_0=0 +y_0=0 +ellps=WGS84 +units=km +no_defs
source : memory
names : layer
values : 0, 2500 (min, max)
So I create a dest_raster:
dest_raster_crs<- "+proj=longlat +datum=WGS84 +zone=34 +no_defs +ellps=WGS84 +towgs84=0,0,0"
dest_raster<-raster(ncols=900,nrows=900,crs= dest_raster_crs)
extent(dest_raster) <- extent(-523.4622, 376.5378, -4658.645, -3758.645)
dest_raster looks like this:
class : RasterLayer
dimensions : 900, 900, 810000 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : -523.4622, 376.5378, -4658.645, -3758.645 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
Now if i do the projection with :
new_raster<-projectRaster(rb, dest_raster,res=c(1,1), dest_raster_crs,over=T)
I get this error:
Warning message:
In rgdal::rawTransform(projto_int, projfrom, nrow(xy), xy[, 1], :
29700 projected point(s) not finite
And if i look at new_raster all the values are NA
class : RasterLayer
dimensions : 900, 900, 810000 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : -523.4622, 376.5378, -4658.645, -3758.645 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : memory
names : layer
values : NA, NA (min, max)
What am i doing wrong?
Thank you!
My mistake was that i used wrong system while doing the extension
Correct way would be:
extent(dest_raster) <- extent(3.5889,14.6209, 47.0705, 54.7405)
I am using the terra library with harmonized HLS data (https://hls.gsfc.nasa.gov/). These are HDF4_EOS files (you can download an image here: https://hls.gsfc.nasa.gov/data/v1.4/S30/2020/11/T/M/M/).
Once downloading a scene and attempting to load in R with the sds function, (i.e., datat=sds(ImageIn)) it works but the coord.ref is unnamed, as shown below:
datat
class : SpatRasterDataset
subdatasets : 14
dimensions : 3660, 3660 (nrow, ncol)
nlyr : 1, 1, 1, 1, 1, 1, 1, 1, 1
resolution : 30, 30 (x, y)
extent : 399960, 509760, 5090220, 5200020 (xmin, xmax, ymin,
ymax)
coord. ref. : unnamed
source(s) : HLS.S30.T11TMM.2019310.v1.4.hdf
names : B01, B02, B03, B04, B05, B06, B07, B08, B8A, B09, B10,
B11, B12, QA
When I use terra::crs(ImageIn), I get the following error message:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘crs’ for signature
‘"character"’
If I use the function get_subdatasets() from the gdalUtils library and the function raster() from the raster library (i.e. raster(imageIn[2])) the crs is found (i.e. +proj=utm +zone=11 +datum=WGS84 +units=m +no_defs).
class : RasterLayer
dimensions : 3660, 3660, 13395600 (nrow, ncol, ncell)
resolution : 30, 30 (x, y)
extent : 399960, 509760, 5090220, 5200020 (xmin, xmax, ymin,
ymax)
crs : +proj=utm +zone=11 +datum=WGS84 +units=m +no_defs
source : HLS.S30.T11TMM.2019310.v1.4.hdf:Grid:B02
names : HLS.S30.T11TMM.2019310.v1.4.hdf.Grid.B02
values : -32768, 32767 (min, max)
Is there a way to get the terra library to find the projection of the raster?
Thanks!!
That was not intended and this has been fixed.
library(terra)
r <- rast("HLS.S30.T11TMM.2020002.v1.4.hdf")
r
#class : SpatRaster
#dimensions : 3660, 3660, 14 (nrow, ncol, nlyr)
#resolution : 30, 30 (x, y)
#extent : 399960, 509760, 5090220, 5200020 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=utm +zone=11 +ellps=WGS84 +units=m +no_defs
#sources : HLS.S30.T11TMM.2020002.v1.4.hdf:Grid:B01
# HLS.S30.T11TMM.2020002.v1.4.hdf:Grid:B02
# HLS.S30.T11TMM.2020002.v1.4.hdf:Grid:B03
# ... and 11 more source(s)
#names : Grid:B01, Grid:B02, Grid:B03, Grid:B04, Grid:B05, Grid:B06, ...
You can also use the crs method like below
crs(r, proj=TRUE)
#[1] "+proj=utm +zone=11 +ellps=WGS84 +units=m +no_defs"
There is no crs method for a SpatRasterDataset (yet), but you can do
s <- sds("HLS.S30.T11TMM.2020002.v1.4.hdf")
crs(s[[1]], proj=TRUE)
#[1] "+proj=utm +zone=11 +ellps=WGS84 +units=m +no_defs"
I have two rasters :
raster1
class : RasterLayer
band : 6 (of 6 bands)
dimensions : 2803, 5303, 14864309 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 60.85, 105.0417, 15.95833, 39.31667 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : XXXX
names : GPP
values : 0, 435.5 (min, max)
raster2
class : RasterLayer
dimensions : 2803, 5303, 14864309 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 60.85, 105.0417, 15.95833, 39.31667 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : memory
names : GPP
values : 0, 506.4 (min, max)
I want to plot a scatterplot between these two rasters according to landcover. My raster file for land cover is :
landcover
class : RasterLayer
dimensions : 2803, 5303, 14864309 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 60.85, 105.0417, 15.95833, 39.31667 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : XXXX
names : landuse
values : 1, 12 (min, max)
How can I plot a scatterplot between raster1 and raster2 for the variable GPP, for the landcover for lets say evergreen forests which is represented by the value of 1 in the raster file of "landcover".
first step is to subset each raster by the land use you identified. Then you can plot them directly:
plot(raster1[landcover==1],raster2[landcover==1])
I am trying to calculate the mean of a rasterbrick over specific timesteps in R.
Subsetting the dataset shows a lay-out that appears to be correct (to me), however R does not allow any computations to follow up.
> r_sub <- b[[1699:1862]]
> r_sub
class : RasterStack
dimensions : 127, 147, 18669, 164 (nrow, ncol, ncell, nlayers)
resolution : 5, 5 (x, y)
extent : 603299.4, 604034.4, 6615598, 6616233 (xmin, xmax, ymin, ymax)
crs : NA
names : X2019.02.09.19, X2019.02.09.20, X2019.02.09.21, X2019.02.09.22, X2019.02.09.23, X2019.02.09.24, X2019.02.10.1, X2019.02.10.2, X2019.02.10.3, X2019.02.10.4, X2019.02.10.5, X2019.02.10.6, X2019.02.10.7, X2019.02.10.8, X2019.02.10.9, ...
> r_mean <- calc(r_sub, mean)
Error in Rsx_nc4_get_vara_double: NetCDF: Index exceeds dimension bound
Var: SWIT Ndims: 3 Start: 1698,0,0 Count: 1,127,147
Error in ncvar_get_inner(ncid2use, varid2use, nc$var[[li]]$missval, addOffset, :
C function R_nc4_get_vara_double returned error
How to solve this specific error?
I am guessing that this is related to a problem with your file, not with the code.
I did this and it worked well
library(raster)
b <- brick("filename.nc")
r_sub <- b[[1699:1862]]
r_sub
#class : RasterStack
#dimensions : 160, 320, 51200, 164 (nrow, ncol, ncell, nlayers)
#resolution : 1.125, 1.121277 (x, y)
#extent : -0.5625, 359.4375, -89.70216, 89.70216 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
mean(r_sub)
#class : RasterLayer
#dimensions : 160, 320, 51200 (nrow, ncol, ncell)
#resolution : 1.125, 1.121277 (x, y)
#extent : -0.5625, 359.4375, -89.70216, 89.70216 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
#source : memory
#names : layer
#values : 3.033032e-07, 0.0002482847 (min, max)
Also, you might want to look into using stackApply
I have two geotiff raster files, one has all the metadata information and in the other the metadata information was lost. I know that all the metadata information was exactly the same so I want to copy it from one file to the other. I tried to use raster, because I made all the processing in R.
This is the file with metadata
af1_patch <-raster(a_files[6])
af1_patch
class : RasterLayer
dimensions : 38400, 38400, 1474560000 (nrow, ncol, ncell)
resolution : 231.656, 231.656 (x, y)
extent : -2223901, 6671703, -4447802, 4447802 (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
data source : Forest_patches_AF_1.tif
names : Forest_patches_AF_1
values : 0, 255 (min, max)
And this is the file without the metadata
af1_area <-raster(a_files[1])
af1_area
class : RasterLayer
dimensions : 38400, 38400, 1474560000 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 0, 38400, 0, 38400 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : africa_AF_1.tif
names : africa_AF_1
values : 0, 255 (min, max)
I tried to copy the metadata using:
res(af1_area) <- res(af1_patch)
crs(af1_area) <- crs(af1_patch)
extent(af1_area) <- extent(af1_patch)
but it doesn't work, dimensions and resolution are incorrect and the data values are lost:
af1_area
class : RasterLayer
dimensions : 166, 166, 27556 (nrow, ncol, ncell)
resolution : 53588, 53588 (x, y)
extent : -2223901, 6671703, -4447802, 4447802 (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
hist(af1_area)
Error in .hist1(x, maxpixels = maxpixels, main = main, plot = plot, ...) :
cannot make a histogram; need data on disk or in memory
Thanks!
I think that this is occuring because you are changing the resolution before changeing the extent, because this, the extent is bound by the resolution that you have assigned to it. I was able to reproduce your problem and solve it by changing the order of the process. Hope it works for you!
library(raster)
x <- raster(matrix(1:10))
proj4string(x) <- CRS("+proj=longlat")
extent(x) <- extent(-10,10,-10,10)
y <- raster(matrix(1:10))
z <- raster(matrix(1:10))
#Current Process
res(y) <- res(x)
crs(y) <- crs(x)
extent(y) <- extent(x)
#Working Process
extent(z) <- extent(x)
res(z) <- res(x)
crs(z) <- crs(x)
Output:
> x
class : RasterLayer
dimensions : 10, 1, 10 (nrow, ncol, ncell)
resolution : 20, 2 (x, y)
extent : -10, 10, -10, 10 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84
data source : in memory
names : layer
values : 1, 10 (min, max)
> y
class : RasterLayer
dimensions : 1, 1, 1 (nrow, ncol, ncell)
resolution : 20, 20 (x, y)
extent : -10, 10, -10, 10 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84
> z
class : RasterLayer
dimensions : 10, 1, 10 (nrow, ncol, ncell)
resolution : 20, 2 (x, y)
extent : -10, 10, -10, 10 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84
data source : in memory
names : layer
values : 1, 10 (min, max)
I would just reassign the values which will preserve the metadata, and re-write the file:
af1_patch <-raster(a_files[6])
af1_area <-raster(a_files[1])
af1_patch[] <- af1_area[]
#writeRaster(af1_patch, a_files[1], ...)