2

I loaded a shapefile in R, and cropped and masked it with the same mask, but I get different extents. Any suggestions?

Code below:

library(raster)
library(rgdal)
library(GISTools)
library(sp)
library(maptools)

##Loading the first mask
Mask <- readOGR("C:\\Users\\rameshv\\Downloads\\Climate Stability\\SPV_Mask\\mask.shp")
proj4string(Mask) <- CRS("+init=epsg:4326")

#Loading the Rasters For Present and LGM and clipping them to the right extent
bio5 <- raster("C:\\Users\\rameshv\\Downloads\\Climate Stability\\Data_LGM_Present\\Present\\1_RawData\\bio_5")
proj4string(bio5) <- CRS("+init=epsg:4326")
lg5 <- raster("C:\\Users\\rameshv\\Downloads\\Climate Stability\\Data_LGM_Present\\LGM\\1_RawData\\cclgmbi5.tif")
proj4string(lg5) <- CRS("+init=epsg:4326")

##Cropping by using the Crop and Mask functions
cr<- crop(bio5, extent(Mask))
bio5 <- mask(cr, Mask)

cr2<- crop(lg5, extent(Mask))
lg5<- mask(cr2, Mask)

> bio5
 class       : RasterLayer 
 dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
 resolution  : 0.04166667, 0.04166667  (x, y)
 extent      : 72.45835, 82.70835, 8.083337, 22.20834  (xmin, xmax, ymin, ymax)
 coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
 data source : in memory
 names       : bio_5 
 values      : 207, 432  (min, max)
 attributes  :
    ID COUNT
 from: -86     1
 to  : 489    38

 > lg5
 class       : RasterLayer 
 dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
 resolution  : 0.04166667, 0.04166667  (x, y)
 extent      : 72.45833, 82.70833, 8.083333, 22.20833  (xmin, xmax, ymin, ymax)
 coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
 data source : in memory
 names       : cclgmbi5 
 values      : 187, 392  (min, max)

EDIT

Following a projection, I am able to align extents of the rasters, however, if I coerce them to a vector, I am getting differences in number of elements. Any thoughts?

new.lg5 <- projectRaster(lg5,bio5)

x <- as(bio5,"SpatialPixelsDataFrame")
y <- as(new.lg5, "SpatialPixelsDataFrame")

> str(x)
 Formal class 'SpatialPixelsDataFrame' [package "sp"] with 7 slots
  ..@ data       :'data.frame': 54202 obs. of  1 variable:

> str(y)
  Formal class 'SpatialPixelsDataFrame' [package "sp"] with 7 slots
  ..@ data       :'data.frame': 54747 obs. of  1 variable:
  • What are the resolutions of the two original input rasters? Are they the same or slightly different? – scabecks Mar 14 '17 at 23:01
  • They are exactly the same. To give you more information, these are two rasters that span the entire globe, and I clipped it based on my mask for my region of choice. – Vijay Ramesh Mar 14 '17 at 23:29
  • Try wrapping extent(Mask) with alignExtent(x, targetraster, snap='out')? – obrl_soil Mar 15 '17 at 7:48
  • alignExtent does not work unfortunately. Still getting rasters of different extents. – Vijay Ramesh Mar 15 '17 at 12:37
  • Can you provide an example @obrl_soil – Vijay Ramesh Mar 15 '17 at 13:07
0

You could try this:

library(raster)
new.lg5 <- projectRaster(lg5, bio5)

This will use the projection of one of your layers and applies it to the others, but it also sets the extent of a raster file.

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.