Link to home
Start Free TrialLog in
Avatar of mgjust
mgjust

asked on

Help with Python Script Syntax / Clean up ( ESRI ArcMap - Raster Calculator)

Hello,
I am trying to create a python script to automate some functions in ESRI ArcMap. (I previously recieved assistance with a more complicated, but similar script https://www.experts-exchange.com/questions/23068228/Modify-Python-Script-for-more-automation.html).

I am trying to use a module in ArcGis Called "Int_sa". A sample scripting command from ESRI:
____________________
# Import system modules
import arcgisscripting

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

try:
    # Set the input raster dataset
    inRaster = "C:/data/ras_1"

    # Set the output raster name
    outRaster = "C:/data/final_1"

    # Check out ArcGIS Spatial Analyst extension license
    gp.CheckOutExtension("Spatial")

    # Process: Int
    gp.Int_sa(inRaster, outRaster)

except:
    # If an error occurred while running a tool, then print the messages.
    print gp.GetMessages()
___________________

The script I created (by hacking up another working script), below, tries to make this to run for more than one iteration. For clarification, 'f' is a text file with the locations of 'dem.'

I ran it from the command line as such:
C:\Python24\python "C:\dem.py" "C:\dem.txt"

I get the following error message:
Traceback (most recent call last):
  File "C\dem.py", line 38, in ?
    gp.Int_sa(inRaster, outRaster)
RuntimeError: error in executing tool

My log went to 'legit/n'
If I uncomment try and except, I get not errors and no output.
What does try and except do? And am I using the log feature correctly?

Please advise & I appreciate annotated code,
Thank you kindly,
MJ

P.S.
Someone who got the same error as me for a different process was told on an ESRI forum to:

put the processes within a try/except block with appropriate
gp.addmessage("error here at step .... " + gp.getmessages())
lines throughout to see further details of errors

But I don't really know what this is telling me to do and there was no follow up on the post.
# Multi_Output_Map_Algebra_sample.py
# Description: 
#   Runs an expression built with the Map Algebra language.
# Requirements: None
# Author: ESRI
# Date: Sept 6, 2005
# Import system modules
import sys, arcgisscripting, os
#import sys
 
def log(s):
      f = open("C:/DEM_ERROR.txt","w")
      f.write(s)
      f.close()
 
# Create the Geoprocessor object
gp = arcgisscripting.create()
inRaster = open(sys.argv[1],'r')
log("1/n")
count = 1
tmpdir = 'c:\\temp\\'
 
##try:
for dem in inRaster:
			
	# Set local variables
	outRaster = os.path.join(tmpdir, 'dem', str(count))
	log("outRaster")
	inExpression = "Int_sa(" + dem.strip() + "+ 0.5), outRaster"
	log("inExpression")
	print inExpression
	log("4/n")
	# Check out Spatial Analyst extension license
	gp.CheckOutExtension("Spatial")
	log("legit/n")
 
	# Process: MapAlgebra
	gp.Int_sa(inRaster, outRaster)
	log("5/n")
	count = count + 1
	log("6/n")
	f.close
		
##except:
    # If an error occurred while running a tool, then print the messages.
	print gp.GetMessages()

Open in new window

SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mgjust
mgjust

ASKER

Hello,
Thanks for the reply.  I have a couple of questions.

What does the try and except 'functions' do?
Is there a way to use the template code I posted but have it run through a list of files, or look in a folder? To provide more automation to the script? In the excerpt below can the inRaster or outRaster = a list of files or a folder? Could the the glob module be used to this? Is there a generic method that might work often that I could use to replace a single input with a list or folder?

# Set the input raster dataset
    inRaster = "C:/data/ras_1"
   # Set the output raster name
    outRaster = "C:/data/final_1"


Many thanks,
MJ
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mgjust

ASKER

Thanks again for the python help, very thorough answers.