0

I am using Raster Calcuator in ModelBuilder with ArcMap 10.5.

I used Get Raster Properties to extract Standard Deviation and Mean from a previously calculated raster.

Snip from ModelBuilder showing raster calc step where failing

I would like to use Raster Calc again to create a raster where raster = (mean + 3*(SD)) or (mean -3*(SD)). I attempted to use a Con statement with a boolean or (|) operator. Con statement I am using is as follows:

Con(("difference_mean_normalized_.tif" < (float(%Mean%) - 3 * (float(%Standard Deviation%)))) | ("difference_mean_normalized_.tif" > (float(%Mean%) + 3 * (float(%Standard Deviation%)))), 0, 1)

Where difference_mean_normalized_.tif is my output raster.

raster calc expression on model builder

This fails and I am getting a 000539 Error:

Start Time: Wed Oct 17 07:12:01 2018
Con(("difference_mean_normalized_.tif"  <  (float(-4.6467203950133E-06) - 3 * (float(0.036299477806278)))) | ("difference_mean_normalized_.tif"  > (float(-4.6467203950133E-06) + 3 * (float(0.036299477806278)))), 0, 1)
ERROR 000539: Error running expression: rcexec() 
Traceback (most recent call last):
  File "<expression>", line 1, in <module>
  File "<string>", line 5, in rcexec
  File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\sa\Functions.py", line 263, in Con
    where_clause)
  File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\sa\Utils.py", line 53, in swapper
    result = wrapper(*args, **kwargs)
  File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\sa\Functions.py", line 250, in Wrapper
    ["IfThenElse", in_conditional_raster, in_true_raster_or_constant, in_false_raster_or_constant])
RuntimeError: ERROR 999998: Unexpected Error.

Failed to execute (Raster Calculator (3)).

Some notes: I double-click or drag the variables in from the list in Raster Calc, and "float" is automatically added to the expression for each variable. I manually type in the output raster name. I have tried wrapping the raster name with the % symbol as well, although this is not shown in the screenshot. Also, my original bands were integer, and I had to use the float() operator to convert.

How do I use Raster Calc in ModelBuilder to create a raster +/- 3SD from mean?

  • 2
    Are difference_mean_normalized.tif_ & difference_mean_normalized_.tif supposed to refer to the same raster? – Bjorn Oct 17 '18 at 12:46
  • 1
    Yes, thank you. I made the correction in the expression and re-ran the tool and got same error. Revised question, error, and screenshots. Also, my final raster is a model parameter, and I am placing a band's suffix at the end to name it when I run the tool (i.e. difference_mean_normalized_blue.tif). That is why the "_" follows the raster name in the expression. – Anna Riling Oct 17 '18 at 13:21
1

I can't see all bubbles in your model builder screenshot, so might be missing something, but the python version of your model is this

inputRaster = "difference_mean_normalized_.tif"
outRaster = "c:/Temp/RasterCalcOutput.tif"

std = arcpy.GetRasterProperties_management(in_raster=inputRaster, property_type="STD", band_index="Band_1")
mean = arcpy.GetRasterProperties_management(in_raster=inputRaster, property_type="MEAN", band_index="Band_1")

calcString = 'Con("{0}" <  (float({1}) - 3 * (float({2}))) | ("{0}" > (float({1}) + 3 * (float({2})))), 0, 1)'.format(inputRaster, mean, std)

arcpy.gp.RasterCalculator_sa(calcString, outRaster)

which will work if you have that input tif in your ArcMap mxd and copy/paste the code to the ArcMap python window. If you try to run it outside of Arcmap, you'll need to modify the code a bunch to do that, like use arcpy.CheckOutExtension("Spatial") and use the Raster() function, which I'm assuming you don't need/want to do.

It worked for me using a test raster.

1

The underlying problem is in:

File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\sa\Functions.py", line 250, in Wrapper ["IfThenElse", in_conditional_raster, in_true_raster_or_constant, in_false_raster_or_constant]) RuntimeError: ERROR 999998: Unexpected Error.

because of a preceding problem in:

File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\sa\Utils.py", line 53, in swapper result = wrapper(*args, **kwargs)

because of a preceding problem in:

File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\sa\Functions.py", line 263, in Con where_clause)

Basically, ArcPy cannot parse your Con statement's where clause. I have seen this problem and variations of it many times before in ArcPy because ArcPy has a flaky argument parser. If you make any syntax errors in your where clause, then the argument parser will fail and the ArcPy documentation is very poor on how to do the correct syntax. Over the years, I have seen posts similar to yours in GIS Stack Exchange about problems with where clauses in ArcPy and ModelBuilder.

If you copy and paste your where clause TEXT into this thread, then I can look at it, but I cannot run it as I do not have ArcGIS at home after retiring last December after 28 years in GIS.

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.