Esri ArcMap Script for Raster Calculator/ Spatial Analyst

Posted on 2007-10-12
Last Modified: 2010-04-21
I am trying to create a python script that will allow me to perform spatial analyst / raster calculator functions with an loop or batch capability.

I have tried modifying the code I posted here that I created with model builder (a drag and drop module) in ArcMap: to make into a 'batch' file
I am trying to run the code below with the command line:
C:\Python24\python "C:\" "C:\filelist"
# Import system modules
import sys, string, os, win32com.client

# Create the Geoprocessor object
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

# Check out any necessary licenses

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")

filelist = open(sys.arg[1], 'r')

      for afile in filelist.readline():
            bfile = filelist.readline()
            dfile = filelist.readline()
            # Local variables...
            cfile_out = "C:\\cfile_" + ctr
            efile_out = "C:\\efile_" + ctr
            #gr_lc_0101 = "C:\\gr_lc_0101"
            #geo01 = "C:\\geo01"
            #gr_lc01_b_01 = "C:\\gr_lc01_b_01"
            ffile_out = "C:\\ffile_" + ctr

            # Process: Single Output Map Algebra...
            gp.SingleOutputMapAlgebra_sa(afile + bfile * 10, cfile_out)
            #, "geo01;gr_lc01_b_01")

            # Process: Con...
            gp.Con_sa(cfile_out, cfile_out, dfile, efile_out, "VALUE = 21")

            # Process: Reclassify...
            gp.Reclassify_sa(efile_out, "Value", "1 1;2 2;3 3;4 4;5 5;21 2", ffile_out, "DATA")
      print gp.GetMessages()

Question by:mgjust
    LVL 17

    Expert Comment

    So what is the problem?
    LVL 17

    Accepted Solution

    Perhaps this is the cause of the problem:

    for afile in filelist.readline():

    will read the first line in the file and loop through the characters in that line. I don't think that's what you want.
    Instead try:

    for afile in filelist:

    Author Comment

    I tried to the run script like this from the command prompt in Windows Xp:

    C:\Documents and Settings\user>C:\Python24\python "C:\test\" "C:\test\filelist.txt"

    & I get this error message:

    Traceback (most recent call last):
      File "C:\test\", line 2, in ?
        import sys, string, os, win32com.client
    ImportError: No module named win32com.client

    Also, in python how important are the placement/spacing of tabs and such. And there is a way to check if mine script is legit?

    Thank you kindly,

    Author Comment

    I changed:

    # Create the Geoprocessor object
    gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

    # Create the Geoprocessor object
    gp = arcgisscripting.create()

    I changed:

    filelist = open(sys.arg[1], 'r')

    filelist = open(sys.argv[1], 'r')

    I also tried this with and without the changes suggested by ramrom and now I get no error messages, but I also get no output.

    Please advise,

    Author Closing Comment

    Hello, thanks for trying. I gave the solution a B, because the issue isn't solved. But you tried. Thanks again.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
    Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now