Search code examples
rrastershapefilereprojection-error

Reproject Raster from xmin=0 to x=-180


I am working with NOAA Files: https://psl.noaa.gov/data/gridded/data.cpc.globaltemp.html

They have the projection EPSG:9001.

> extent(raster.nc)
class      : Extent 
xmin       : 0 
xmax       : 360 
ymin       : -90 
ymax       : 90 

So the central point is somewhere in the Pacific. All other files I work with are EPSG:4326, like Shapefiles of the World: https://hub.arcgis.com/datasets/esri::world-countries-generalized

extent(world.shape)
class      : Extent 
xmin       : -180 
xmax       : 180 
ymin       : -89 
ymax       : 83.6236 

I cannot seem to get those two together.

projectRaster(raster.nc, crs=CRS(projection(world.shape)),method = "bilinear") 

leads to

Error in if (yn == yx) { : missing value where TRUE/FALSE needed
In addition: Warning message:
In `dim<-`(`*tmp*`, value = c(nr, nc)) :
  NAs introduced by coercion to integer range

(also if I do not use CRS())

How can I solve this problem? I would like to work with EPSG:4326


Solution

  • You can use terra::rotate (or its outdated "raster" equivalent)

    Some example data

    library(terra)
    url <- "https://downloads.psl.noaa.gov/Datasets/cpc_global_temp/tmax.1979.nc"
    f <- basename(url)
    download.file(url, f, mode="wb")
    x <- rast(f)
    

    Solution

    y <- rotate(x)
    

    Illustration

    ext(x)
    #SpatExtent : 0, 360, -90, 90 (xmin, xmax, ymin, ymax)
    ext(y)
    #SpatExtent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
    
    plot(y, 1)