5

I have some data in a raster stack (CCSM4 climate projections from 2010 - 2300) that I've done some processing on, but now I would like to change the extent. I'm not 100% sure how to do this in R. So far I've tried Extent, Origin, projectExtent, projectRaster, but haven't had any luck. Below are two images showing the issue I'm having and the results of what I've tried, and a reproducible example

Here is my original dataset, the extent is

> extent(pr_ccsm4_2.5deg)
class       : Extent 
xmin        : -0.625 
xmax        : 359.375 
ymin        : -89.5288 
ymax        : 90.4712 

Original Dataset

What I would like to do is get the data to have an extent of

> extent(ras25)
class       : Extent 
xmin        : -180 
xmax        : 180 
ymin        : -90 
ymax        : 90 

I thought that I could simply change the extent using extent(pr_ccsm4_2.5deg) <- extent(ras25), however this results in my data looking like this altered extent

So now, while the extent is correct, the data is not projected properly.

Here is a reproducible example

library(rworldmap)
library(cleangeo)
library(raster)
library(rasterVis)

# continental shapefile
cont <- getMap()
cont <- clgeo_Clean(cont)
cont <- sapply(levels(cont$continent),
               FUN = function(i){
                 poly <- gUnionCascaded(subset(cont, continent == i))
                 poly <- spChFIDs(poly, i)
                 SpatialPolygonsDataFrame(poly, data.frame(continent = i, row.names = i))
               }, USE.NAMES = TRUE)
cont <- Reduce(spRbind, cont)

ras <- raster(xmn = 0, xmx = 360, ymn = -90, ymx = 90, res = 2.5)
values(ras) <- runif(n = ncell(ras), 0, 1)
ras
> ras
class       : RasterLayer 
dimensions  : 72, 144, 10368  (nrow, ncol, ncell)
resolution  : 2.5, 2.5  (x, y)
extent      : 0, 360, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 2.22486e-05, 0.9998961  (min, max)
# Wrong extent
rasterVis::levelplot(ras, margin = NA, par.settings = RdBuTheme) + layer(sp.polygons(cont))

wrong extent raster

ras2 <- raster(xmn = -180, xmx = 180, ymn = -90, ymx = 90, res = 2.5)
values(ras2) <- runif(n = ncell(ras2), 0, 1)
ras2
> ras2
class       : RasterLayer 
dimensions  : 72, 144, 10368  (nrow, ncol, ncell)
resolution  : 2.5, 2.5  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 0.0001686567, 0.9998712  (min, max)

# Correct extent
rasterVis::levelplot(ras2, margin = NA, par.settings = RdBuTheme) + layer(sp.polygons(cont))

correct extent raster

rasPrj <- projectRaster(ras, ras2, over = FALSE)
rasPrj
# Correct extent, but missing data
rasterVis::levelplot(rasPrj, margin = NA, par.settings = RdBuTheme) + layer(sp.polygons(cont))

correct extent, missing data

  • 1
    Try raster::rotate(pr_ccsm4_2.5deg) which is for precisely this task. Note that the after shifting the hemisphere is probably extent(-180, 180, -90, 90) so you can just set that afterwards, but you should check that carefully - there's often an ambiguity about cell edge versus cell centre, but it's not always that straightforward. – mdsumner May 1 '17 at 23:03
  • Thanks a lot @mdsumner, I have no idea how I missed that when reading through the raster documentation. – KaanKaant May 1 '17 at 23:29
  • rotate is a non-obvious name when thinking about an image! – mdsumner May 1 '17 at 23:39
6

Try raster::rotate which is for precisely this task.

I.e.

raster::rotate(pr_ccsm4_2.5deg)

Note that the after shifting the hemisphere is probably extent(-180, 180, -90, 90) so you can just set that afterwards, but you should check that carefully. There's often an ambiguity about cell-edge versus cell-centre, but it's not always that straightforward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.