We help IT Professionals succeed at work.

Esri ArcMap Script for Raster Calculator/ Spatial Analyst

5,591 Views
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: https://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
gp.CheckOutExtension("spatial")

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

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

try:
      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")
            count=count+1
      filelist.close()
except:
      print gp.GetMessages()

-------
Thanks,
Comment
Watch Question

ramromconsultant

Commented:
So what is the problem?
consultant
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hello,
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

Commented:
I changed:

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

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

I changed:

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

to:
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

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.