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 have a folder that contains around 191 GeoTIFF files (each file is a different DEM (elevation) tile of a much larger area). I want to merge all the tiles into one raster file. I am using the terra package and was successfully able to load each raster and aggregate them from 2 metre resolution to 30 metre resolution. However, when running the mosaic function to merge them all, I run into an error (see error message below). I have been able to run the mosaic function on a smaller subset of just three tiles, but when I scale up to all the files, this becomes an issue.
By calling the summary of the rasters (see below), the aggregation does slightly change the extent - could this be the issue? resample might be an option, but each individual raster has a different extent and I'm not exactly sure how to implement this fix.
Not sure a sample data set would help since I know the functions work. I am running this code on a high-performance cluster so it's not very efficient to run small batches of code.
library(terra)
files <- as.list(list.files("./DEM_tiles", full.names = TRUE))
raster.list <- lapply(files, rast)
for(i in 1:length(raster.list)){
raster.list[[i]] <- aggregate(raster.list[[i]], fact = 15)
}
raster.mosaic <- do.call(mosaic, raster.list)
> Error: [mosaic] internal error: extents do not match ()
> Execution halted
Below is an example of what two of the tiles look like:
### Before Aggregation
[[1]]
class : SpatRaster
dimensions : 25000, 25000, 1 (nrow, ncol, nlyr)
resolution : 2, 2 (x, y)
extent : -1800000, -1750000, -6e+05, -550000 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
source : 35_23_1_1_2m_v3.0_reg_dem.tif
name : 35_23_1_1_2m_v3.0_reg_dem
[[2]]
class : SpatRaster
dimensions : 25000, 25000, 1 (nrow, ncol, nlyr)
resolution : 2, 2 (x, y)
extent : -1800000, -1750000, -550000, -5e+05 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
source : 35_23_1_2_2m_v3.0_reg_dem.tif
name : 35_23_1_2_2m_v3.0_reg_dem
### After Aggregation
[[1]]
class : SpatRaster
dimensions : 1667, 1667, 1 (nrow, ncol, nlyr)
resolution : 30, 30 (x, y)
extent : -1800000, -1749990, -600010, -550000 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
source : memory
name : 35_23_1_1_2m_v3.0_reg_dem
min value : -15.62178
max value : 233.6489
[[2]]
class : SpatRaster
dimensions : 1667, 1667, 1 (nrow, ncol, nlyr)
resolution : 30, 30 (x, y)
extent : -1800000, -1749990, -550010, -5e+05 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
source : memory
name : 35_23_1_2_2m_v3.0_reg_dem
min value : -15.27713
max value : 243.0772
The error was because the rasters were not aligned, and due to a bug. I now get
library(terra)
#terra version 1.2.1
crs <- "+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m"
r1 <- rast(nrow=1667, ncol=1667, ext=c(-1800000, -1749990, -600010, -550000), crs=crs)
r2 <- rast(nrow=1667, ncol=1667, ext=c(-1800000, -1749990, -550010, -5e+05), crs=crs)
values(r1) <- 1:ncell(r1)
values(r2) <- 1:ncell(r2)
m <- mosaic(r1, r2)
#Warning message:
#[mosaic] rasters did not align and were resampled
m
#class : SpatRaster
#dimensions : 3334, 1667, 1 (nrow, ncol, nlyr)
#resolution : 30, 30 (x, y)
#extent : -1800000, -1749990, -600010, -499990 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
#source : memory
#name : lyr.1
#min value : 1
#max value : 2778889
Also
#Elia's suggestion is a good work-around:
r1 <- writeRaster(r1, "test1.tif", overwrite=TRUE)
r2 <- writeRaster(r2, "test2.tif", overwrite=TRUE)
v <- vrt(c("test1.tif", "test2.tif"), "test.vrt", overwrite=TRUE)
v
#class : SpatRaster
#dimensions : 3334, 1667, 1 (nrow, ncol, nlyr)
#resolution : 30, 30 (x, y)
#extent : -1800000, -1749990, -600020, -5e+05 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
#source : test.vrt
#name : test
#min value : 1
#max value : 2778889
I have some MODIS images downloaded. When I try to create a SpatRaster from the hdf files, using terra rast function, it works perfectly for "MOD09A1" but it doesn´t work for "MOD09GA".
terra::rast("C:/Users/User/AppData/Local/Temp/_modis/MOD09GA.A2011025.h08v06.006.2015216102410.hdf")
Error: [rast] number of rows and/or columns do not match
What is the problem? Is there any other function I could use? Thanks!
The problem is that the file has subdatasets with different resolutions.
To get the file your are using
# remotes::install_github("rspatial/luna")
aoi <- c(-106, -105, 26, 27)
f <- luna::getModis("MOD09GA", "2011-01-25", "2011-01-26", aoi, download=TRUE, path=".", user="*", password="*")
f
[1] "./MOD09GA.A2011025.h08v06.006.2015216102410.hdf"
To see the subdatasets:
library(terra)
describe_sds(f)
id name desc nrow ncol nlyr
1 HDF4_EOS:EOS_GRID:...MODIS_Grid_1km_2D:num_observations_1km [1200x1200] num_observations_1km MODIS_Grid_1km_2D (8-bit integer) 1200 1200 1
(...)
12 HDF4_EOS:EOS_GRID:...MODIS_Grid_500m_2D:sur_refl_b01_1 [2400x2400] sur_refl_b01_1 MODIS_Grid_500m_2D (16-bit integer) 2400 2400 1
(...)
So you need to access the different subdatasets seperately, like this
b1 <- rast(f, 12)
b2 <- rast(f, 13)
b1
# class : SpatRaster
# dimensions : 2400, 2400, 1 (nrow, ncol, nlyr)
# resolution : 463.3127, 463.3127 (x, y)
# extent : -11119505, -10007555, 2223901, 3335852 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs
# data source : MOD09GA.A2011025.h08v06.006.2015216102410.hdf:MODIS_Grid_500m_2D:sur_refl_b01_1
# names : sur_refl_b01_1
Perhaps followed by
bb <-c(b1, b2)
Or create a SpatDataSet like this
x <- sds(f, 12:22)
In the development version of terra you can then proceed and do
y <- collapse(x)
or something like the below to get a particular set of subdatasets (that have the same spatial resolution)
r <- rast(f, 12:18)
r
#class : SpatRaster
#dimensions : 2400, 2400, 7 (nrow, ncol, nlyr)
#resolution : 463.3127, 463.3127 (x, y)
#extent : -11119505, -10007555, 2223901, 3335852 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs
#source(s) : MOD09GA.A2011025.h08v06.006.2015216102410.hdf:MODIS_Grid_500m_2D:sur_refl_b01_1
MOD09GA.A2011025.h08v06.006.2015216102410.hdf:MODIS_Grid_500m_2D:sur_refl_b02_1
MOD09GA.A2011025.h08v06.006.2015216102410.hdf:MODIS_Grid_500m_2D:sur_refl_b03_1
... and 4 more source(s)
#names : sur_refl_b01_1, sur_refl_b02_1, sur_refl_b03_1, sur_refl_b04_1, sur_refl_b05_1, sur_refl_b06_1, ...
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 having a problem while converting a raster object as NetCDF (.nc) file, with keeping the CRS information in the output file.
Here is a reproducible code:
require(raster)
require(ncdf4)
CurrTemp <- tempfile()
download.file(url = "https://seaice.uni-bremen.de/data/amsre/asi_daygrid_swath/s6250/2003/feb/Antarctic/asi-s6250-20030214-v5.hdf", destfile = CurrTemp, mode = "wb", quiet = T)
r <- raster(CurrTemp)
r <- flip(r,2)
extent(r) <- c(-3950000, 3950000, -3950000, 4350000)
crs(r) <- "+proj=stere +lat_0=-90 +lat_ts=-70 +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378273 +b=6356889.449 +units=m +no_defs "
r
# class : RasterLayer
# dimensions : 1328, 1264, 1678592 (nrow, ncol, ncell)
# resolution : 6250, 6250 (x, y)
# extent : -3950000, 3950000, -3950000, 4350000 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=stere +lat_0=-90 +lat_ts=-70 +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378273 +b=6356889.449 +units=m +no_defs
# data source : in memory
# names : layer
# values : 0, 100 (min, max)
So far the raster object reads well, with CRS information included.
However, when I try to save it as .nc file, R prints "coord. ref. : NA", and the produced file does not contain the CRS information.
writeRaster(r, filename = "O:/Ahmed/r001.nc", varname="IceConc",
overwrite=TRUE, format="CDF",
xname="Longitude", yname="Latitude")
# class : RasterLayer
# dimensions : 1328, 1264, 1678592 (nrow, ncol, ncell)
# resolution : 6250, 6250 (x, y)
# extent : -3950000, 3950000, -3950000, 4350000 (xmin, xmax, ymin, ymax)
# coord. ref. : NA
# data source : O:/Ahmed/r001.nc
# names : IceConc
# zvar : IceConc
raster("O:/Ahmed/r001.nc")
# class : RasterLayer
# dimensions : 1328, 1264, 1678592 (nrow, ncol, ncell)
# resolution : 6250, 6250 (x, y)
# extent : -3950000, 3950000, -3950000, 4350000 (xmin, xmax, ymin, ymax)
# coord. ref. : NA
# data source : O:/Ahmed/r001.nc
# names : IceConc
# zvar : IceConc
Thanks