Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
image_expression_null_value_handling [2022/08/21 21:42]
admin
image_expression_null_value_handling [2022/08/22 04:54] (current)
admin [Null Evaluation]
Line 38: Line 38:
   else i2   else i2
  
-If you need to retrieve the number corresponding to image null value, you can use the ''​null( iX )''​ expression. For instance, ''​null(i1)''​ returns the number corresponding to the null value of image ''​i1''​. So if, so some reason, you need to retrieve the image value or its corresponding numeric null value, you can use ''​i1 ? null(i1)''​ or ''​if isnull(i1) then null(i1) then i1''​. Note that ''​null(i1)''​ is very different from expression ''​null'',​ since ''​null(i1)''​ always returns a valid numeric value and ''​null''​ returns a special construct that, if not contained, invalidates the expression evaluation producing null as a result.+If you need to retrieve the number corresponding to an image null value, you can use the ''​null( iX )''​ expression. For instance, ''​null(i1)''​ returns the number corresponding to the null value of image ''​i1''​. So if, so some reason, you need to retrieve the image value or its corresponding numeric null value, you can use ''​i1 ? null(i1)''​ or ''​if isnull(i1) then null(i1) then i1''​. Note that ''​null(i1)''​ is very different from expression ''​null'',​ since ''​null(i1)''​ always returns a valid numeric value and ''​null''​ returns a special construct that, if not contained, invalidates the expression evaluation producing null as a result.
  
 ===== Proper Way of Testing for Image Values -- Guarding Against Null Values ===== ===== Proper Way of Testing for Image Values -- Guarding Against Null Values =====
Line 46: Line 46:
 Now, lets assume the we want do combine two images, a landscape map and road map, as a single map, where roads in the road map are represent by the value 1 and everything else is represented using the null value. Assuming the ''​i1''​ is the landscape map and ''​i2''​ is the road map, we might be tempted to write an expression as ''​if i2 = 1 then 1 else i1''​. Unfortunately that is not going to work, since the expression is internally equivalent to ''​if isNull(i2) then null else if i2 = 1 then 1 else i1''​ (we not guarding against null values). As you can see, all occurrences of null values on the road map will be replicated in the output map. Now, lets assume the we want do combine two images, a landscape map and road map, as a single map, where roads in the road map are represent by the value 1 and everything else is represented using the null value. Assuming the ''​i1''​ is the landscape map and ''​i2''​ is the road map, we might be tempted to write an expression as ''​if i2 = 1 then 1 else i1''​. Unfortunately that is not going to work, since the expression is internally equivalent to ''​if isNull(i2) then null else if i2 = 1 then 1 else i1''​ (we not guarding against null values). As you can see, all occurrences of null values on the road map will be replicated in the output map.
  
-So, the proper way of doing the calculation is using a ''​isNull( iX )''​ to protect the ''​if then else'' ​test against the null: ''​if not isNull(i2) and i2 = 1 then 1 else i2''​. Now we only test for the value in the road map if we are sure that value is not going to contaminate the whole if-then-else,​ turning the result into null.+So, the proper way of doing the calculation is using a ''​isNull( iX )''​ to protect the if-then-else test against the null: ''​if not isNull(i2) and i2 = 1 then 1 else i2''​. Now we only test for the value in the road map if we are sure that value is not going to contaminate the whole if-then-else,​ turning the result into null.