5.9 Raster Data Casting

Raster data casting maps cell values from one data type to another.

In GeoRaster, there are two types of casting operations: one uses the cellDepth keyword in the storageParam parameter of operations, and the other uses the castingExpr operation in the GeoRaster raster algebra. (castingExpr is one of the arithmeticExpr operations, as described in Raster Algebra Language.)

Whenever you apply an operation which stores the raster data result into a new GeoRaster object, you can use the cellDepth keyword in the storageParam parameter of that operation. (The cellDepth keyword and its values are described in Table 1-1.) If the cellDepth is specified, the target GeoRaster object will be created using that cellDepth value, and the raster cell data will be automatically cast to that cellDepth value for storage. You can directly use cellDepth in the storageParam parameter to do the casting if the source data is in lower cell depth and the resulting data is in higher cell depth. In this case, the casting is transparent and fast.

However, if you specify a lower cell depth for data in higher cell depth, changing the cell depth using the cellDepth keyword in the storageParam parameter can cause loss or change of data and reduced precision or quality. To have better control of the precision and accuracy, you can use the Raster Algebra casting operator, castingExpr.

For example, assume you have a raster with a cell depth of 32BIT_REAL and a value range in [0.0, 100.0). You can use Example 5-23 to perform linear segmentation of the raster into 10 different classes, each of which has a cell value that is a multiple of 10 (0, 10, 20, …, 90), using the castint operator. This operation casts all cell values to their closest lower multiple of 10; for example, all numbers from 60 to 69 are cast to 60.

Example 5-23 Linear Segmentation of a Raster

DECLARE 
  geor1    SDO_GEORASTER;
  geor2    SDO_GEORASTER;
BEGIN
  --Source georaster object with cell value range [0.0,100.0) 
  select georaster into geor1 from georaster_table where georid = 1;
  --Target georaster object to store the output layer
  select georaster into geor2 from georaster_table where georid = 2 for update;
  --Linearly segment the source raster into 10 classes and store in 8BIT cell depth
  sdo_geor_ra.rasterMathOp(geor1,
    SDO_STRING2_ARRAY('(castint({0}/10)*10'),
    'celldepth=8BIT',
    geor2);
  --Commit changes to the output georaster object
  update georaster_table set georaster = geor2 where georid = 2;
  commit;
END;
/

As shown in Example 5-23, you can combine the usage of the cellDepth keyword in the storageParam parameter with the raster algebra casting operator, so that the result can be calculated correctly as well as stored in an appropriate and concise way. In Example 5-23, the output cell values are integers equal to or less than 90, so the resulting raster can be stored using 8BIT cell depth (instead of 32BIT_REAL), which saves storage space.