Learn R Programming

ursa (version 3.9.4)

as.Raster: Coercion to package 'raster' objects

Description

as.Raster converts singe-band ursaRaster object to raster, multi-band ursaRaster object to brick and list of ursaRaster objects to stack. S4 classes “raster”, “brick”, and “stack” are defined in package raster.

Usage

as.Raster(obj)

# S3 method for ursaRaster as.Raster(obj)

# S3 method for list as.Raster(obj)

# S3 method for ursaStack as.Raster(obj)

# S3 method for NULL as.Raster(obj)

Arguments

obj

Object of class ursaRaster or list of ursaRaster objects

Value

Either RasterLayer, RasterBrick, or RasterStack object.

If package raster is not installed then return value is NULL

Details

Package raster is required for conversions.

The uppercase as.Raster is important, because as.raster is used in internal functions for coercion to object of class raster.

Single-banded ursaRaster object (with or without colortable) is coerced to RasterLayer. Colortables are kept.

Multi-banded ursaRaster object is coerced to RasterBrick. Colortables are destroyed.

Multi-layered object (list of ursaRaster objects) is coerced to RasterStack. Colortables are destroyed.

Examples

Run this code
# NOT RUN {
session_grid(NULL)
if (requireNamespace("raster")) {
   session_grid(regrid(mul=1/4))
   msk <- ursa_dummy(1,min=0,max=100)>40
   a1 <- ursa_dummy(1,min=200,max=500)[msk]
   a2 <- colorize(a1,ramp=FALSE)
   a3 <- as.integer(ursa_dummy(3,min=0,max=255.99))
   a4 <- ursa_stack(a3[msk])
   if (isLayer <- TRUE) {
      print(a1)
      r1 <- as.Raster(a1)
      message(as.character(class(r1)))
      print(r1)
      print(raster::spplot(r1))
      b1 <- as.ursa(r1)
      print(c(exported=a1,imported=b1,failed=b1-a1))
      print(c(theSameValue=identical(ursa_value(a1),ursa_value(b1))
             ,rheSameGrid=identical(ursa_grid(a1),ursa_grid(b1))))
   }
   if (isLayerColortable <- TRUE) {
      r2 <- as.Raster(a2)
      message(as.character(class(r2)))
      print(r2)
      print(raster::spplot(r2))
      b2 <- as.ursa(r2)
      print(c(theSameValue=identical(ursa_value(a2),ursa_value(b2))
             ,rheSameGrid=identical(ursa_grid(a2),ursa_grid(b2))))
   }
   if (isBrickOrRGB <- TRUE) {
      r3 <- as.Raster(a3)
      message(as.character(class(r3)))
      print(r3)
      print(raster::spplot(r3))
      raster::plotRGB(r3)
      b3 <- as.ursa(r3)
      print(c(theSameValue=identical(ursa_value(a3),ursa_value(b3))
             ,rheSameGrid=identical(ursa_grid(a3),ursa_grid(b3))))
   }
   if (isStack <- TRUE) {
      r4 <- as.Raster(a4)
      message(as.character(class(r4)))
      print(r4)
      print(raster::spplot(r4))
      b4 <- as.ursa(r4)
      print(c(theSameValue=identical(ursa_value(a4),ursa_value(b4))
             ,theSameGrid=identical(ursa_grid(a4),ursa_grid(b4))))
   }
}
# }

Run the code above in your browser using DataLab