3

I am trying to create a script that creates a new list of rasters by searching through the file names then using that new list to execute the weighted sum function in spatial analyst. I am having trouble with executing the weighted sum.

Here is my code and error messaage:

#Import System Modules
import arcpy
from arcpy import env
from arcpy.sa import *

#set environment setting
env.workspace = "E:/AlexandraOrrego/GIS_2017/RasterData/clipped.gdb"
arcpy.env.mask = "E:/AlexandraOrrego/GIS_2017/RasterData/forest_xcap"

#checks out ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

#Runs through rasters and sums based on DCA
rasList = arcpy.ListRasters()

#initialize an empty list 
matchedRasters = []

for raster in rasList:
    outRasterName = raster[7:12]    
    if outRasterName.startswith('11004'):
        print outRasterName
        matchedRasters.append(raster) 

#executes weighted sum for of all matchedRasters with using value field and 
given a weight of 1        
for raster in matchedRasters():
    WSumTableObj = WSTable([raster, "VALUE", 1])
    outWeightedSum = WeightedSum(WSumTableObj)  
    #Saves the output
    outWeightedSum.save("output folder")

Here is what is printed and my error message:

11004
11004
11004
Runtime error 
Traceback (most recent call last):
  File "<string>", line 29, in <module>
TypeError: 'list' object is not callable
>>> 
0

1 Answer 1

2

You need to remove the parenthesis in: for raster in matchedRasters():

Also you are creating a WSumTableObject for each raster. Arent you supposed to create one WSumTableObject containing all the rasters?

If so could you try replacing the last for block with:

#executes weighted sum for of all matchedRasters with using value field and given a weight of 1  
WSumTableObj = WSTable([[raster,"VALUE", 1] for raster in matchedRasters])
outWeightedSum = WeightedSum(WSumTableObj)
outWeightedSum.save(r"C:\folder\outraster")
1
  • I have edited my code to your suggestion and the script worked perfectly. Suddenly after running other processes in arcmap none of my arcpy scripts will run. For example, I load the arcpy script file into the python window in arcmap and hit enter to run, arcmap seems to load for a few seconds then returns >>> with no output, error, message and no processes were run.
    – Allie
    Commented Jun 30, 2017 at 19:31

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.