1

I am trying to use a conditional statement to generate a raster with binary values from a raster with probability values (floating point raster). This is easily done within ArcMap by batching the process, but I need to do it >300 times with a different 'threshold' value each time.

Here is my code:

# Import arcpy module
    import arcpy

# Import environment and spatial analyst modules
    from arcpy import env
    from arcpy.sa import *
    arcpy.CheckOutExtension("Spatial")

# Set Workspace for Rasters 
    env.workspace = "C:\May_2014\python_example.gdb"
    Rasters = arcpy.ListRasters("*", "All")

# Local Variables
    MyTable = "table_for_python"

    with arcpy.da.SearchCursor (MyTable, ["threshold"]) as mycursor:
        counter = 0
        for row in mycursor:
            raster = Rasters[counter]
            outCon = Con(raster >= mycursor, 1,0)
            outCon.save("C:\May_2014\python_example_2.gdb\Con_" + raster)
            counter = counter + 1

I keep getting the following error message after the line:

outCon.save('Con_' + raster)

AttributeError: 'int' object has no attribute 'save'.

Any suggestions would be most welcome. Thanks in advance!

1
  • it looks like your outCon variable is getting assigned an integer value rather than a raster. try adding a print outCon statement between outCon = and outCon.save to make sure outCon is getting assigned a raster value as it should be.
    – sfletche
    May 24, 2014 at 3:33

2 Answers 2

2

I just ran into a similar problem and used the idea here to tell Python that my layers are rasters using Raster(). However, I did this not in the Con() function itself, but rather before that, when specifying the in_conditional raster, in_true_raster and in_false raster. E.g.,

inRaster1 = Raster("mydata1")
inRaster2 = Raster("mydata2")

outCon = Con((inRaster1 == 3) & (inRaster2 < 5), inRaster1, 6)
0

I take it counter is for the output, the first threshold is 0, the next is 1 and so on for each row in the table.

# Import arcpy module
    import arcpy

# Import environment and spatial analyst modules
    from arcpy import env
    from arcpy.sa import *
    arcpy.CheckOutExtension("Spatial")

# Set Workspace for Rasters 
    env.workspace = "C:\May_2014\python_example.gdb"
    Rasters = arcpy.ListRasters("*", "All")

# Local Variables
    MyTable = "table_for_python"

    with arcpy.da.SearchCursor (MyTable, ["threshold"]) as mycursor:
        counter = 0
        for ThisRaster in Rasters:
          for row in mycursor:
              # ThisRaster is a string, make it a raster by Raster(ThisRaster)
              outCon = Con((Raster(ThisRaster) >= float(row[0])), 1,0)
              outCon.save("C:\May_2014\python_example_2.gdb\Con_" + raster + str(counter))
              counter = counter + 1

Your iteration was a bit funny, the pythonic way is to for var in list:

6
  • Thanks, Michael. I have tried running it using this new code, but I still get the same error message. I really appreciate your help. I am a Python rookie.
    – Henrik
    May 24, 2014 at 2:03
  • It's nothing to do with the counter, it's the Con function returning a int for some reason. Do a type(outCon) to see what the return type is.
    – Nathan W
    May 24, 2014 at 6:59
  • for some reason the con function isn't recognizing ThisRaster as a raster, rather as a string. Using Raster(ThisRaster) will make it into a raster object. That should work. May 24, 2014 at 8:53
  • The docs for .ListRasters say it returns a list of strings, not rasters. That, after all, is why evaluating an expression like "C:\May...\Con_" + raster could even succeed in the OP's code. (Incidentally, Con is superfluous: the inequality will return a 0-1 grid.) However, although I think you have identified the fundamental problem, your code obviously won't work: you haven't initialized raster!
    – whuber
    May 24, 2014 at 18:59
  • Isn't that what Raster(ThisRaster) does? I've seen this syntax used to initialize a raster at the time of the Con in examples. I agree that the Con is superfluous, but it allows the op to change values 0,1 to 255,1 at a whim. May 25, 2014 at 1:36

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.