1

I have the following raster:

> modis
class       : RasterLayer 
dimensions  : 1776, 4320, 7672320  (nrow, ncol, ncell)
resolution  : 0.08333333, 0.08333333  (x, y)
extent      : -180, 180, -64, 84  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : /Users/maps/MODIS_PANratios.tif 
names       : MODIS_PANratios 
values      : 1, 20  (min, max)
attributes  :
       ID       category
 from:  0               
 to  : 20 Woody savannas

Please note this raster contains categorical attributes.

I want to create a raster stack with modisand a second raster with different resolution and extent:

> rDif
class       : RasterLayer 
dimensions  : 720, 1440, 1036800  (nrow, ncol, ncell)
resolution  : 0.25, 0.25  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : /Users/maps/liuDif.tif 
names       : liuDif 
values      : 0, 4150641  (min, max)

I use project rasterto adapt extent and resolution from modis to rDifto be able to stack both:

modis.new <- projectRaster(from=modis, to=rDif, method="ngb")

The problem is that the new raster has lost its original attributes:

> modis.new
class       : RasterLayer 
dimensions  : 720, 1440, 1036800  (nrow, ncol, ncell)
resolution  : 0.25, 0.25  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : MODIS_PANratios 
values      : 1, 20  (min, max)

How can I standardise the extent and resolution of two rasters without loosing the original attributes?

  • With original attributes, do you mean dimensions and resolution? Or value 0? Seems that 0 has no class assigned – aldo_tapia Nov 22 '17 at 10:24
  • Indeed, 0's are missing values, reason there is no class assigned. Is this the reason the category names are lost? – fede_luppi Nov 22 '17 at 10:40
  • I think the process lost category names, I'm not sure. You can make a backup as a workaround of this problem backup <- levels(modis) ; levels(modis.new) <- backup – aldo_tapia Nov 22 '17 at 10:46
  • This is off-topic, but how can I convert 0's to NAs? I have tried modis[modis==0] <- NA but the 0 class with no name assigned still appears – fede_luppi Nov 22 '17 at 12:11
  • The output of levels(modis) consider value 0? An option could be erasing it from this list – aldo_tapia Nov 22 '17 at 15:38
2

As you have multiple setting to standardise the easiest option is spatial_sync_raster from the spatial.tools package.

This will match the extent, resolution, and crs. It does, however, take quite some time. As you have categorical data you need method ="ngb" to set the re sampling to nearest neighbor

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.