如何执行 raster::rotate 的倒数

how to perform inverse of raster::rotate

栅格包提供了一个名为 rotate 的函数,该函数

"rotates a Raster* object that has x coordinates (longitude) from 0 to 360, to standard coordinates between -180 and 180 degrees. Longitude between 0 and 360 is frequently used in global climate models."

但是,如果我们希望执行此函数的反函数,将 -180 到 180 度的经度转换为 0 到 360 度的经度,则该函数不起作用,因为它所做的只是丢弃源自于的任何数据小于零的经度:

library(maps)
library(maptools)
library(raster)
world = map("world", fill=TRUE, col="transparent", plot=FALSE)
world = map2SpatialPolygons(world, world$names, CRS("+proj=longlat +ellps=WGS84"))
world = rasterize(world, raster(nrows=100,ncols=200, ext=extent(-180,180,-90,90)))

plot(world)

plot(rotate(world))

我们如何执行 rotate 的逆运算,将 -180 到 180 的 "standard coordinates" 转换为 0 到 360 坐标?

这应该可以解决问题:

w2 <- shift(rotate(shift(world, 180)), 180)
plot(w2)