Esri ArcMap Script for Raster Calculator/ Spatial Analyst

Posted on 2007-10-12
Medium Priority
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: http://www.experts-exchange.com/Database/GIS_and_GPS/Q_22885465.html to make into a 'batch' file
I am trying to run the code below with the command line:
C:\Python24\python "C:\test.py" "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
  • 3
  • 2
LVL 17

Expert Comment

ID: 20071602
So what is the problem?
LVL 17

Accepted Solution

ramrom earned 1500 total points
ID: 20071610
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

ID: 20088894
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\test.py" "C:\test\filelist.txt"

& I get this error message:

Traceback (most recent call last):
  File "C:\test\test.py", 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

ID: 20089038
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

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is designed to assist GIS (Geographic Information System) and GPS (Global Positioning System) developers using ESRI ArcGIS and other spatial information management systems.   For the uninitiated the concept of projectio…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…
Suggested Courses

850 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