1

I have a nested con statement that I'd like to put into a script, as it's hard to follow in its current form. The raster it produces is incorrect, so it'd be easier to debug in a script.

The current set of statements looks like this:

EWR_2015 = Con("ministerial_wetlands">0," ministerial_wetlands ",
(Con("non_ministerial_wetlands ">0,("WTE_using_2001_min"-0.25),
(Con(("nat_veg_raster" >0) & ("dtgw_2001_min" <= 3) ,    
("WTE_using_2001_min"-0.75), 
(Con(("nat_veg_raster ">0) & (("dtgw_2001_min ")>3 & ("dtgw_2001_min " <= 6)) , ("WTE_using_2001_min"-1.0), 
(Con(("nat_veg_raster ">0) & (("dtgw_2001_min " > 6) & ("dtgw_2001_min "<= 10)),(" WTE_using_2001_min "-1.25)
)))))))))

I would like to use a series of if/ elif statements in Python to do a similar thing.

I tried this for the first statement:

ministerial_wetlands = Raster("ministerial_wetlands")
if ministerial_wetlands>0:
    outRaster = Raster(ministerial_wetlands)

This gives the following error message: ValueError: The truth value of a raster is ambiguous. Invalid use of raster with Boolean operator or function. Check the use of parentheses where applicable.

Not sure where to go from here.

2
  • Use con statement again instead of if. It is the same thing
    – FelixIP
    Oct 30, 2015 at 6:43
  • Would just "if ministerial_wetlands:" work? Or you could use arcpy.Describe and check the max value of the raster. Maybe a bit slow though.
    – jon_two
    Oct 30, 2015 at 8:44

2 Answers 2

3

Not sure if this helps, but I have a script with many nested Con statements, and I find just using indentation helps. For example:

EWR_2015 = Con(
    "ministerial_wetlands" > 0,
    " ministerial_wetlands ",
    Con(
        "non_ministerial_wetlands " > 0,
        "WTE_using_2001_min" - 0.25,
        Con(
            "nat_veg_raster" > 0 & "dtgw_2001_min" <= 3,
            "WTE_using_2001_min" - 0.75, 
            Con(
                "nat_veg_raster " > 0 & ("dtgw_2001_min " > 3 & "dtgw_2001_min " <= 6),
                "WTE_using_2001_min" - 1.0,
                Con(
                    "nat_veg_raster " > 0 & ("dtgw_2001_min " > 6 & "dtgw_2001_min " <= 10),
                    " WTE_using_2001_min " - 1.25
                )
            )
        )
    )
)

It does take up more space, but it's worth the extra legibility.

0

You can split it up into more readable/debug-able chunks using variables.

con_1 = Con(
    "nat_veg_raster " > 0 & ("dtgw_2001_min " > 6 & "dtgw_2001_min " <= 10),
    " WTE_using_2001_min " - 1.25
)

con_2 = Con(
    "nat_veg_raster " > 0 & ("dtgw_2001_min " > 3 & "dtgw_2001_min " <= 6),
    "WTE_using_2001_min" - 1.0,
    con_1
)

con_3 = Con(
    "nat_veg_raster" > 0 & "dtgw_2001_min" <= 3,
    "WTE_using_2001_min" - 0.75, 
    con_2
)

con_4 = Con(
    "non_ministerial_wetlands " > 0,
    "WTE_using_2001_min" - 0.25,
    con_3
)

EWR_2015 = Con(
    "ministerial_wetlands" > 0,
    " ministerial_wetlands ",
    con_4
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.