2

I am facing this error time and again when I am try to read my img rasters and running a conditional statement. I am new to Python. The code ran for one raster that is without for loop but not later. Here is the code:

import arcpy, os
from arcpy import env
from arcpy.sa import *
env.overwriteOutput = True
arcpy.env.workspace = r"C:\Users\libpub\Desktop\try"
arcpy.CheckOutExtension('Spatial')

rasterlist = arcpy.ListRasters()
for raster in rasterlist:
    out1 = Con(('Raster')>0.2, 1, 0)
    print out1
    out1.save("C:\Users\libpub\Desktop\try")

print "done!"
2
  • 1
    welcome to GIS SE! Please use this guide to indent block you code: meta.stackexchange.com/questions/22186/… Also, please post the exact error message you are receiving.
    – NULL.Dude
    Dec 14, 2017 at 13:56
  • 1
    Add an r to the path out1.save(r"...
    – BERA
    Dec 14, 2017 at 14:13

1 Answer 1

3

The arcpy.ListRasters function returns a list of the raster names in your workspace. The Con function, in the format you are using, requires an input Raster object which can be created with:

rast = Raster('raster_name')

If you change out1 = Con(('Raster')>0.2, 1, 0) to out1 = Con(Raster(raster)>0.2, 1, 0) you should find it works. This is shown in the Con documentation in code example 1.

0

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.