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)
Related
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 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
Hi i been trying to reproject a raster image from Equirectangular to EPSG:4326 (Latlon), the issue is that every time i run my code on R, i get the wrong coordinates on the new image; i don´t know where is the error in the code, also i do the same process with Qgis, and i got the same result, it´s strange, i got the opportunity to do the same reprojection process in ENVI, and the result was succesful, help please!!!
a <- raster("C:/Users/<username>/Documents/imageexample.tif")
> a
class : RasterLayer
dimensions : 1800, 1800, 3240000 (nrow, ncol, ncell)
resolution : 1100, 1100 (x, y)
extent : -988900, 991100, 1677577, 3657577 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=eqc +lat_ts=0 +lat_0=24 +lon_0=-112 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:/Users/<username>/Documents/imageexample.tif
names : imageexample
g1 <- projectRaster(a, crs="+init=epsg:4326")
> g1
class : RasterLayer
dimensions : 1810, 1810, 3276100 (nrow, ncol, ncell)
resolution : 0.00988, 0.00988 (x, y)
extent : -120.9328, -103.05, 39.02317, 56.90597 (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0
data source : in memory
names : imageexample.tif
values : -5.000117, 39.87529 (min, max)
The correct coordinates should be like this:
class : RasterLayer
dimensions : 1793, 1803, 3232779 (nrow, ncol, ncell)
resolution : 0.0108098, 0.009931556 (x, y)
extent : -121.735, -102.245, 15.08612, 32.8934 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:/Users/<username>/Documents/CORRECTimageexample.tif
names : CORRECTimageexample
Thanks!!!
This is a bit of a special case but generally there is not defined (by the data) extent and resolution for raster reprojection. You need to specify these. For example, you can do:
library(raster)
r <- raster(xmn=-121.735, xmx=-102.245, ymn=15.08612, ymx=32.8934, nrow=1793, ncol=1803, crs='+proj=longlat +ellps=WGS84')
g2 <- projectRaster(a, r)
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], ...)
Hi i been trying to reproject a raster image from Equirectangular to EPSG:4326 (Latlon), the issue is that every time i run my code on R, i get the wrong coordinates on the new image; i don´t know where is the error in the code, also i do the same process with Qgis, and i got the same result, it´s strange, i got the opportunity to do the same reprojection process in ENVI, and the result was succesful, help please!!!
a <- raster("C:/Users/<username>/Documents/imageexample.tif")
> a
class : RasterLayer
dimensions : 1800, 1800, 3240000 (nrow, ncol, ncell)
resolution : 1100, 1100 (x, y)
extent : -988900, 991100, 1677577, 3657577 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=eqc +lat_ts=0 +lat_0=24 +lon_0=-112 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:/Users/<username>/Documents/imageexample.tif
names : imageexample
g1 <- projectRaster(a, crs="+init=epsg:4326")
> g1
class : RasterLayer
dimensions : 1810, 1810, 3276100 (nrow, ncol, ncell)
resolution : 0.00988, 0.00988 (x, y)
extent : -120.9328, -103.05, 39.02317, 56.90597 (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0
data source : in memory
names : imageexample.tif
values : -5.000117, 39.87529 (min, max)
The correct coordinates should be like this:
class : RasterLayer
dimensions : 1793, 1803, 3232779 (nrow, ncol, ncell)
resolution : 0.0108098, 0.009931556 (x, y)
extent : -121.735, -102.245, 15.08612, 32.8934 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:/Users/<username>/Documents/CORRECTimageexample.tif
names : CORRECTimageexample
Thanks!!!
This is a bit of a special case but generally there is not defined (by the data) extent and resolution for raster reprojection. You need to specify these. For example, you can do:
library(raster)
r <- raster(xmn=-121.735, xmx=-102.245, ymn=15.08612, ymx=32.8934, nrow=1793, ncol=1803, crs='+proj=longlat +ellps=WGS84')
g2 <- projectRaster(a, r)