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
Related
x <- raster::stack('D:/MODIS/MCD19A2.A2008355.h24v06.006.2018043191140.hdf')
band1 <- x[[1]]
band1
#class : RasterLayer
#band : 1 (of 16 bands)
#dimensions : 1200, 1200, 1440000 (nrow, ncol, ncell)
#resolution : 926.6254, 926.6254 (x, y)
#extent : 6671703, 7783654, 2223901, 3335852 (xmin, xmax, ymin, ymax)
#crs : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs
#source : MCD19A2.A2008355.h24v06.006.2018043191140.hdf
#names : Optical_Depth_047_1
Does the extent means the MODIS data contained in hdf4 files?
"extent" refers to the spatial extent of the data. It shows the minimum and maximum x and y coordinates of the data.
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 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)