I have 365 rasters with the same extent, and I am trying to sum them together by month. I have the script below, which was working until I added the line out1 = Con(IsNull(raster), 0, raster) which is supposed to get rid of any nodata cells in each raster I am processing. Now my script keeps raising the exception:

NameError: name 'Con' is not defined.

I am using the Con function exactly as it is used in all of the ArcGIS help documentations, so I understand why it doesn't recognize Con.

Anyone have any ideas as to what is causing this error? My script is below.

#script to create scaled habitat maps

#import  modules
import arcpy

#check out spatial analyst extension
if arcpy.CheckExtension("Spatial") == "Available":
    arcpy.AddError("Spatial Analyst extension is unavailable. Please check to see if you have the Spatial Analyst License.")

from arcpy import env
rasters = "Z:/sharks/Models/Summer2013/RF/ALL/SB_RF/NSMPpresRFPdh/AR_NSMPpresRFPdh/outputRasters"
env.workspace = rasters
listRasters = arcpy.ListRasters()
janRasters = listRasters[0:30]
febRasters = listRasters[31:58]
marRasters = listRasters[59:89]
aprRasters = listRasters[90:119]
mayRasters = listRasters[120:150]
junRasters = listRasters[151:180]
julRasters = listRasters[181:211]
augRasters = listRasters[212:242]
sepRasters = listRasters[243:272]
octRasters = listRasters[273:303]
novRasters = listRasters[304:333]
decRasters = listRasters[334:364]

rasterList = [janRasters, febRasters, marRasters, aprRasters, mayRasters, junRasters, julRasters, augRasters, sepRasters, octRasters, novRasters, decRasters]
nameList = ["janHabitat.img", "febHabitat.img", "marHabitat.img", "aprHabitat.img", "mayHabitat.img", "junHabitat.img", "julHabitat.img", "augHabitat.img", "sepHabitat.img", "octHabitat.img", "novHabitat.img", "decHabitat.img"]

counter = 0            
for list in rasterList:
    i = 0
    print ("processing list: %s" % (list,))
    for raster in list:
        print ("processing raster: %s" %(raster,))
        #convert nodata to zero
        out1 = Con(IsNull(raster), 0, raster)

        #sum rasters together
        if i == 0:
            outSUM = arcpy.Raster(raster)
            i += 1
            outSUM = outSUM + raster
            i += 1

    #save final output to the current workspace
    outSUM.save("Z:/sharks/Models/Summer2013/RF/ALL/SB_RF/NSMPpresRFPdh/AR_NSMPpresRFPdh/monthlyHabitat/" + nameList[counter])
    counter +=1
  • 2
    Might I suggest you emulate the example in the manual page for CON? I notice immediately that it imports arcpy.sa whereas your code does not. – whuber Dec 15 '13 at 17:54

I don't use arcpy but I know Python. This only means that you need to import the Python module with Con (and it is not arcpy alone)

If I look at, Con (Spatial Analyst), I see that it is a function of the Spatial Analyst module and the examples provide the solution. You have forgotten to import the module:

from arcpy.sa import *

But the "pythonic" way to do is:

from arcpy.sa import Con
  • Oh okay thank you so much, I am really sorry about asking a question that was a careless mistake on my part! – Liza Dec 15 '13 at 23:30
  • 2
    @Liza, That is what this site is here for. It's how we learn. – Fezter Dec 16 '13 at 6:01

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.