1

Having a problem in R's raster package -

I can't crop a raster across the 180° meridian - I'm trying to create a bounding box with longitude 120°-230° East

Simple enough, but the following code:

cropbox2 <-c(120,230,70,90)
crop2 <- crop(rt, cropbox2)

Doesn't return anything beyond 180E - it can't seem to cross the 180° meridian

Using minus

cropbox2 <-c(120,-130,70,90)

returns

Error in validityMethod(object) : invalid extent: xmin >= xmax

Surely this is a common problem people have run into? I'm only trying to crop the N Pacific!

1

Try @RobertHijmans' split-and-merge approach described here, which is based on (i) splitting global data along the prime meridian, (ii) changing the extent of the Western hemisphere, and (iii) merging the two images back into one. The following code uses 700 mb geopotential height data of the northern hemisphere for replication purposes (see also ?vdendool in remote). However, modifying it according to your needs should be rather straightforward:

## sample data
library(remote)
data(vdendool)

## see above for details
x1 <- crop(vdendool, extent(-180, 0, 20, 90)) # for global grid: -180, 0, -90, 90
x2 <- crop(vdendool, extent(0, 180, 20, 90))  # for global grid: 0, 180, -90, 90
extent(x1) <- c(180, 360, 20, 90)
m <- merge(x1, x2)

cropbox2 <-c(120, 230, 70, 90)
crop2 <- crop(m, cropbox2)

## verify extent
extent(crop2)
# class       : Extent 
# xmin        : 120 
# xmax        : 230 
# ymin        : 70 
# ymax        : 90

## display layers 1 to 4
spplot(crop2[[1:4]], scales = list(draw = TRUE))

enter image description here

  • Appreciate the answer - I've tried that but get Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE, : different origin Because the origins (and extents) are marginally different - floating point error. Adjusting the tolerance = argument doesn't help either - I have to adjust it to 0.5 for it to run, and it puts a gap line one pixel thick between the two rasters – Ndharwood Apr 19 '18 at 14:58
  • Hm, I see. Would it be possible to provide a sample of your data? – fdetsch Apr 20 '18 at 6:25
  • It's actually just standard ECMWF .75 x .75 NetCDF data - 850hPa temperature. I'm not sure how to send it? I'm thinking of getting a different scale - i.e. .5x.5 or 1x1 to see if the floating point (and therefore origin) problem goes away, what do you think? – Ndharwood Apr 20 '18 at 14:07

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.