5.7 Logical Operations

A major use of raster algebra is to apply logical models to raster layers from different sources; that is, you can apply logical operations on one or more layers, from one or more GeoRaster objects, to generate a new GeoRaster object.

To apply logical operations, you can either use raster algebra procedures with logical expressions, which is more flexible and powerful and mostly be used for some complicated raster logical operations, or use raster algebra procedures only, which are straightforward and do not require constructing complicated logical expressions. However, using raster algebra procedures only (that is, without logical expressions) has some limitations and is mainly used for some specific raster logical operations.

5.7.1 Using Raster Algebra Procedures with Logical Expressions

GeoRaster logical expressions can be conditional expressions, boolean expressions, or both, which can take any combination of unary and binary boolean operators (!, &, |, ^) and comparison operators (=, <, >, <=, >=, !=).

To apply logical expressions on the raster data, you must use raster algebra procedures defined in the SDO_GEOR_RA package and specify appropriate parameters with your constructed logical expressions.

Example 5-16 Using SDO_GEOR_RA.rasterMathOp with condition operators

This example implements logic described in the following pseudocode to implement 3–band raster data segmentation:

 if ( (layer1 < 100) 
      & (layer2< 1000) 
      & (layer3< 500)) 
 then output = 10
 elsif ( (layer1 < 200) 
         & (layer2< 2000) 
         & (layer3< 1000))
 then output = 20
 elsif ( (layer1 < 300) 
         & (layer2< 3000) 
         & (layer3< 1500))
 then output = 30
 elsif ( (layer1 < 400) 
         & (layer2< 4000) 
         & (layer3< 2000))
 then output = 40
 elsif ( (layer1 < 500) 
         & (layer2< 5000) 
         & (layer3< 2500))
 then output = 50
 else
      output = 0

The example calls the SDO_GEOR_RA.rasterMathOp procedure, as follows

DECLARE 
  geor       SDO_GEORASTER;
  geor1      SDO_GEORASTER;
  mycursor   sys_refcursor;
  expr       varchar2(1024);
BEGIN
  select georaster into geor from georaster_table where georid = 100;
  select georaster into geor1 from georaster_table where georid = 101 for update;
  --construct logical expression 
  expr :='condition((({0}<100)&({1}<1000)&({2}<500)),'||
                   '10,'||
                   'condition((({0}<200)&({1}<2000)&({2}<1000)),'||
                             '20,'||
                             'condition((({0}<300)&({1}<3000)&({2}<1500)),'||
                                       '30,'||
                                       'condition((({0}<400)&({1}<4000)&({2}<2000)),'||
                                                 '40,'||
                                                 'condition((({0}<500)&({1}<5000)&({2}<2500)),'||
                                                           '50,'||
                                                           '0)'||
                                                ')'||
                                      ')'||
                            ')'||
                  ')';
                
  sdo_geor_ra.rasterMathOp(geor, sdo_string2_array(expr),null, geor1, 'true', 0, 'parallel=4');
  update georaster_table set georaster = geor1 where georid = 101;
  commit;
END;
/

Example 5-17 Using SDO_GEOR_RA.rasterMathOp with a condition operator

This example uses statistical functions and arithmetic operations to implement the simple logic described in the following pseudocode:

if (sum()>min()*3) 
then
   output = sqrt(layer0+layer2)
else
   output = layer1*1.5

The example calls the SDO_GEOR_RA.rasterMathOp procedure, as follows

DECLARE 
  geor       SDO_GEORASTER;
  geor1      SDO_GEORASTER;
  mycursor   sys_refcursor;
  expr       varchar2(1024);
BEGIN
  select georaster into geor from georaster_table where georid = 100;
  select georaster into geor1 from georaster_table where georid = 101 for update;
  --construct logical expression 
  expr :='condition(sum()>min()*3,sqrt({0}+{2}),{1}*1.5)';               
  sdo_geor_ra.rasterMathOp(geor, sdo_string2_array(expr),null, geor1, 'true', 0, 'parallel=4');
  update georaster_table set georaster = geor1 where georid = 101;
  commit;
END;
/

5.7.2 Using Raster Algebra Functions Only

To perform logical operations using only raster algebra functions, you have the following options

  • Use the SDO_GEOR_RA.diff procedure.

    For example, if a cell value in raster A is different from the cell value in raster B, the cell value in raster A is returned. If the cell values are the same, the value 0 (zero) is returned.

  • Use the SDO_GEOR_RA.over procedure.

    For example, if a cell value in raster A is not equal to 0 (zero), the cell value in raster A is returned. If the cell value in raster A is equal to 0, the cell value in raster B is returned.

Example 5-18 Using SDO_GEOR_RA.diff

This example calls the SDO_GEOR_RA.diff procedure to generate a new GeoRaster object from two 3–layer source GeoRaster objects.

DECLARE
  geor       SDO_GEORASTER;
  geor1      SDO_GEORASTER;
  geor2      SDO_GEORASTER;
  geom       sdo_geometry;
BEGIN
  select georaster into geor from georaster_table where georid = 100;
  select georaster into geor1 from georaster_table where georid = 101;
  select georaster into geor2 from georaster_table where georid = 102 for update;
  geom:=null;
  sdo_geor_ra.diff(geor,geor1,geom,null,geor2);
  update georaster_table set georaster = geor2 where georid = 102;
END;
/
 
PL/SQL procedure successfully completed.
 
SELECT sdo_geor.getcellvalue(georaster,0,100,100,'') FROM georaster_table WHERE georid=100;
 
SDO_GEOR.GETCELLVALUE(GEORASTER,0,100,100,'')
--------------------------------------------------------------------------------
SDO_NUMBER_ARRAY(181, 163, 159)
 
1 row selected.

SELECT sdo_geor.getcellvalue(georaster,0,100,100,'') FROM georaster_table WHERE georid=101;
 
SDO_GEOR.GETCELLVALUE(GEORASTER,0,100,100,'')
--------------------------------------------------------------------------------
SDO_NUMBER_ARRAY(181, 122, 159)
 
1 row selected.

--  In the results of the next SELECT statement, note:
--  181 =181  ==>  0
--  163!=122  ==>  163
--  159 =159  ==>  0

SELECT sdo_geor.getcellvalue(georaster,0,100,100,'') FROM georaster_table WHERE georid =102;
SDO_GEOR.GETCELLVALUE(GEORASTER,0,100,100,'')
--------------------------------------------------------------------------------
SDO_NUMBER_ARRAY(0, 163, 0)
 
1 row selected.

Example 5-19 Using SDO_GEOR_RA.over

This example calls the SDO_GEOR_RA.over procedure to generate a new GeoRaster object from two 3–layer source GeoRaster objects.

DECLARE
  geor       SDO_GEORASTER;
  geor1      SDO_GEORASTER;
  geor2      SDO_GEORASTER;
  geom       sdo_geometry;
BEGIN
  select georaster into geor from georaster_table where georid = 102;
  select georaster into geor1 from georaster_table where georid = 101;
  select georaster into geor2 from georaster_table where georid = 100 for update;
  geom:=null;
  sdo_geor_ra.over(geor,geor1,geom,null,geor2);
  update georaster_table set georaster = geor2 where georid = 100;
END;
/
 
PL/SQL procedure successfully completed.
 
SELECT sdo_geor.getcellvalue(georaster,0,100,100,'') FROM georaster_table WHERE georid=102;
 
SDO_GEOR.GETCELLVALUE(GEORASTER,0,100,100,'')
--------------------------------------------------------------------------------
SDO_NUMBER_ARRAY(0, 163, 0)
 
1 row selected.

SELECT sdo_geor.getcellvalue(georaster,0,100,100,'') FROM georaster_table WHERE georid=101;
 
SDO_GEOR.GETCELLVALUE(GEORASTER,0,100,100,'')
--------------------------------------------------------------------------------
SDO_NUMBER_ARRAY(181, 122, 159)
 
1 row selected.

--  In the results of the next SELECT statement, note:
--  0 =0    ==>  181      result from georid=101
--  163!=0  ==>  163      result from georid=102
--  0 =0    ==>  159      result from georid=101

SELECT sdo_geor.getcellvalue(georaster,0,100,100,'') FROM georaster_table WHERE georid =100;
SDO_GEOR.GETCELLVALUE(GEORASTER,0,100,100,'')
--------------------------------------------------------------------------------
SDO_NUMBER_ARRAY(181, 163, 159)
 
1 row selected.