rrasterr-rasterextent

change extent in map from 0, 360, 0, 300 to -180, 180, -90, 90


how can I change the extent of a netcdf file from 0, 360, 0, 300 to -180, 180, -90, 90

Would the solution be the same if the original extents are

0, 320, 0, 384

0, 362, 0, 294

0, 720, 0, 576

0, 362, 0, 332

0, 360, 0, 256

0, 802, 0, 404

class      : RasterLayer 
dimensions : 300, 360, 108000  (nrow, ncol, ncell)
resolution : 1, 1  (x, y)
extent     : 0, 360, 0, 300  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : memory
names      : layer 
values     : -1.728468, 35.60058  (min, max)

Solution

  • You can change the extent of raster data with the raster package like this

    extent(x) <- c(0,1,0,1)
    

    or with the terra package like this

    ext(x) <- c(0,1,0,1)
    

    What you are showing looks like row and column numbers, not coordinates. So how do you get these extents in the first place? How do you read the ncdf files?

    Generally, the easiest way would be

    library(terra)
    x <- rast("ncdffile.nc")
    

    if longitude is between 0 and 360 instead of -180 and 180, you can then do

    y <- rotate(x)