How to properly project and plot raster in R

问题


I have a raster in an equal area Behrmann projection and I would like to project it to the Mollweide projection and plot.

When I do this with the following code, however, the plotting doesn't seem right, as the map extends to the sides, and there are outlines of various landmasses where I wouldn't expect them.Also, the map extends beyond the plot window.

Can anyone please help me get this to plot nicely?

Thanks!

The data file used can be downloaded from this link.

Here is the code I have so far:

require(rgdal)
require(maptools)
require(raster)

data(wrld_simpl)

mollCRS <- CRS('+proj=moll')
behrmannCRS <- CRS('+proj=cea +lat_ts=30')

sst <- raster("~/Dropbox/Public/sst.tif", crs=behrmannCRS)

sst_moll <- projectRaster(sst, crs=mollCRS)
wrld <- spTransform(wrld_simpl, mollCRS)

plot(sst_moll)
plot(wrld, add=TRUE)

enter image description here


回答1:


Alright, since the example at this page seems to work, I tried to mimic it as much as possible. I think problems arise because the far left and far right side of the raster image overlap. Cropping and an intermediate reprojection to Lat-Lon as in the example seem to solve your problem.

Perhaps this workaround can be a basis for a more elegant solution that directly addresses the problem, as it is not benificial to reproject a raster twice.

# packages
library(rgdal)
library(maptools)
library(raster)

# define projections
mollCRS <- CRS('+proj=moll')
behrmannCRS <- CRS('+proj=cea +lat_ts=30')

# read data
data(wrld_simpl)
sst <- raster("~/Downloads/sst.tif", crs=behrmannCRS)

# crop sst to extent of world to avoid overlap on the seam
world_ext = projectExtent(wrld_simpl, crs = behrmannCRS)
sst_crop = crop(x = sst, y=world_ext, snap='in')

# convert sst to longlat (similar to test file)
# somehow this gets rid of the unwanted pixels outside the ellipse
sst_longlat = projectRaster(sst_crop, crs = ('+proj=longlat'))

# then convert to mollweide
sst_moll <- projectRaster(sst_longlat, crs=mollCRS, over=T)
wrld <- spTransform(wrld_simpl, mollCRS)

# plot results
plot(sst_moll)
plot(wrld, add=TRUE)

enter image description here



来源:https://stackoverflow.com/questions/27535047/how-to-properly-project-and-plot-raster-in-r

标签

更多相关内容:请点击查看