[GIS] Arcpy Subtracting rasters error – TypeError: ‘unicode’ object is not callable

arcpyrastertypeerror

I have a script which subtracts 2 versions of the same raster from each other (one newer than the other) and then extracts the additions & deletions that have happened.

I created a simple test script which works:

# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")


# Local variables:
New = "C:\\Data\\JOB_13\\tq03\\ABRdsText.tif"
Old = "D:\\Data\\Job_Backups\\JOB_13\\tq03\\ABRdsText.tif"
Addition_Raster = "C:\\Testing\\District_Editor\\QC_ChangePlots\\JOB_13\\tq03\\Additions\\output.tif"
Deletion_Raster = "C:\\Testing\\District_Editor\\QC_ChangePlots\\JOB_13\\tq03\\Deletions\\output.tif"

arcpy.MakeRasterLayer_management(New, "N")
arcpy.MakeRasterLayer_management(Old, "O")


# Process: Raster Calculator
RR = Raster("N") - Raster("O")

# Process: Extract by Attributes
arcpy.gp.ExtractByAttributes_sa(RR, "VALUE =-255 OR VALUE =0", Addition_Raster)

# Process: Extract by Attributes (2)
arcpy.gp.ExtractByAttributes_sa(RR, "VALUE =255 or VALUE=0", Deletion_Raster

I then replicated this into my main script:

#import the necessary modules
import arcpy
from arcpy import env
from arcpy.sa import *
import os

#Checkout the necessary Arc licences
arcpy.CheckOutExtension("spatial")

#Ask the user for the job number
#Job_Number = arcpy.GetParameterAsText(0)
# MANUAL OVERIDE TO GET TEST JOB_NUMBER
Job_Number = 13
Job = 'JOB_'+str(Job_Number)

####################################################
#Folder where all data will be written to
OutputsDirectory = r"C:\Testing\District_Editor\QC_ChangePlots"

####################################################
#Folder with the backed up data pre-editing i.e. old data
BackupDirectory =r"D:\Data\Job_Backups"

####################################################
#Location of the QC Change plot Master mxd
ChangePlot_mxd = r"Y:\esriuk\OSBurnProcessGPTools\resources\mxdHolder\QC_ChangePlot.mxd"

####################################################
#Location of the Job folder
DataFolder = os.path.join("C:\\Data\\",Job)

####################################################

JobF = os.path.join(OutputsDirectory,Job)

for folder in os.listdir(DataFolder):
    arcpy.env.workspace = DataFolder + "\\" + str(folder)
    Ras = arcpy.ListRasters("*", "TIF")
    for Raster in Ras:
        #Set the output rasters
        Additions = os.path.join(JobF + "\\" + str(folder) + "\\Additions\\"+ str(Raster))
        Deletions = os.path.join (JobF + "\\" + str(folder) + "\\Deletions\\"+ str(Raster))

        #Set the file paths for the new and old rasters
        New = os.path.join(str(DataFolder) +"\\" + str(folder) + "\\" + str(Raster))
        Old = os.path.join(str(BackupDirectory) + "\\" + str(Job) + "\\" + str(folder) + "\\" + str(Raster))

        #Convert the Raster datasets into a temporary Raster Layer so that they can be used for a calculaion
        arcpy.MakeRasterLayer_management(New, "N")
        arcpy.MakeRasterLayer_management(Old, "O")


        #Raster Calculator - subtract the old raster from the new raster ( new - old)
        CalculatedRast = Raster("N") - Raster("O")



        #Extract the Additions
        arcpy.gp.ExtractByAttributes_sa(CalculatedRast, "VALUE =-255 OR VALUE =0", Additions)


        #Extract the deletions
        arcpy.gp.ExtractByAttributes_sa(CalculatedRast, "VALUE =255 or VALUE=0", Deletions)

However, I get the error

Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.1\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "S:\Development\Dan\GIS\Code\District_Editor\QC_ChangePlots.py", line 204, in <module>
    CalculatedRast = Raster("N") - Raster("O")
TypeError: 'unicode' object is not callable

Anybody able to suggest where I am going wrong and how to fix it? I'm fairly new to python, so it's probably something quite simple!

The error seems to be here

CalculatedRast = Raster("N") - Raster("O")

Best Answer

I think you have to make the addition and subtraction rasters as raster objects as shown in the latter example of the help files Subtracting rasters

Related Question