Nested statements with Con Error

1349
4
Jump to solution
04-26-2021 05:03 PM
KathleenHoenke
Occasional Contributor

I'm trying to get this short con statement to work. In the comments was the previous author's code, but that didn't work for me so I changed it, and am getting an error (below). Any help is appreciated!

 


File "<string>", line 32, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Functions.py", line 1631, in Con
where_clause)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Functions.py", line 1615, in Wrapper
["IfThen", in_conditional_raster, in_true_raster_or_constant])
RuntimeError: The parameter is incorrect.

 

 

outcon = Con((Newflowdir ==1,1, Con(Newflowdir == 64,1, Con(Newflowdir ==16,1, Con(Newflowdir ==4,1)))),1.41421356237)
outcon.save(r'E:\NC Lidar NEW 03052021\SARP_NCGS_Data_Share_cms20210305\LiDar\knickpoints\dxraster.tif')

 

 

0 Kudos
1 Solution

Accepted Solutions
Tomasz_Tarchalski
New Contributor III

Hello,

I  can suggest different approach to your question, but firstly, one error is comming from that you are trying to assign float (1.43...) and your raster might accept only integer...

as, your con expresion is quite complex, how about using 'raster-cell-iterator'  and raster_info.

For example sth like this:

from arcpy.sa import *

myRas = Raster(r"C:\Temp\flow_direction_raster")
#get raster information
rasInfo = myRas.getRasterInfo()
#get pixel type
print(rasInfo.getPixelType())

#in rasinfo object change one parameter - pixel type
rasInfo.setPixelType("F32")
# now create new, empty raster, but with defined extent, pixel type...
new_raster = Raster(rasInfo)

#run calculation to update values to new raster
with RasterCellIterator({'rasters':[myRas, new_raster]}) as rci:
    for i,j in rci:
        if myRas[i,j] in [1, 64, 16, 4]:
            new_raster[i,j] = 1
        else:
            new_raster[i,j] = 1.41421356237

# save new raster to your disk
new_raster.save(r'C:\Temp\test')

 

 

View solution in original post

4 Replies
DanPatterson
MVP Esteemed Contributor

Quick look

 

 

# Newflowdir == A
Con( A == 1, 1, Con(A == 64, 1, Con(A ==16, 1, Con(A == 4, 1.41421356237))))

 

 

Too many brackets... Were you just adding them until syntax errors vanished? 


... sort of retired...
0 Kudos
KathleenHoenke
Occasional Contributor

Lol maybe I was..... my hangup is that I need the 1.41 to be the false value for all of them. So then when I do this, I get another error.

 

outcon = Con(Newflowdir ==1,1,1.41421356237, Con(Newflowdir == 64,1,1.41421356237, Con(Newflowdir ==16,1,1.41421356237, Con(Newflowdir ==4,1,1.41421356237))))
outcon.save(r'E:\NC Lidar NEW 03052021\SARP_NCGS_Data_Share_cms20210305\LiDar\knickpoints\dxraster.tif')

 

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Functions.py", line 1631, in Con
where_clause)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Functions.py", line 1618, in Wrapper
["IfThenElse", in_conditional_raster, in_true_raster_or_constant, in_false_raster_or_constant])
RuntimeError: The parameter is incorrect.

0 Kudos
DanPatterson
MVP Esteemed Contributor

Are you using the Con tool in arctoolbox or trying it in code? (use the toolbox)

It also looks like you would be probably be better off reclassing the flow direction values that equal (1, 4, 16, 64) to 1 and everything else as 1.41421....)


... sort of retired...
Tomasz_Tarchalski
New Contributor III

Hello,

I  can suggest different approach to your question, but firstly, one error is comming from that you are trying to assign float (1.43...) and your raster might accept only integer...

as, your con expresion is quite complex, how about using 'raster-cell-iterator'  and raster_info.

For example sth like this:

from arcpy.sa import *

myRas = Raster(r"C:\Temp\flow_direction_raster")
#get raster information
rasInfo = myRas.getRasterInfo()
#get pixel type
print(rasInfo.getPixelType())

#in rasinfo object change one parameter - pixel type
rasInfo.setPixelType("F32")
# now create new, empty raster, but with defined extent, pixel type...
new_raster = Raster(rasInfo)

#run calculation to update values to new raster
with RasterCellIterator({'rasters':[myRas, new_raster]}) as rci:
    for i,j in rci:
        if myRas[i,j] in [1, 64, 16, 4]:
            new_raster[i,j] = 1
        else:
            new_raster[i,j] = 1.41421356237

# save new raster to your disk
new_raster.save(r'C:\Temp\test')