Select to view content in your preferred language

Stand alone script fails using spatial analyst tools

2331
16
03-05-2012 12:18 AM
ElizabethLewis
Emerging Contributor
Hi,

I'm trying to create a stand alone script to automate watershed analysis. So far it works up until it gets to the spatial analyst
tools but then fails. Can anyone show me where I have gone wrong please? Here is my code:

import arcpy
import os
import time
import sys
from arcpy import env
from arcpy.sa import *

env.workspace = "H:/PhD/PythonLessons/Scripts/testIt"
catchmentName = "CatchmentName"
catchmentRaster = catchmentName + ".tif"
catchmentRasterProj = catchmentName + "proj.tif"
catchment100 = catchmentName + "100.tif"
catchment100Fill = catchmentName + "100Fill.tif"
catchment100Flow = catchmentName + "100Flow.tif"
catchment100Acc = catchmentName + "100Acc.tif"
catchment100PP = catchmentName + "100PP.tif"
catchment100WS = catchmentName + "100WS.tif"
folder = "H:/PhD/PythonLessons/Scripts/testIt"
folderAdd = "H:/PhD/PythonLessons/Scripts/testIt/"
outlet = "outlet.shp"
projection = "C:/Program Files (x86)/ArcGIS/Desktop10.0/Coordinate Systems/Projected Coordinate Systems/National Grids/Europe/British National Grid.prj"
# identify guage
#create pourpoint shapefile
# identify catchment outline
# select appropriate testIt

# have a folder with all of the little ascii testIt for a catchment. This folder will be called *catchment name*
print "checking extensions"

try:
 arcpy.CheckExtension("Spatial") == "Available"
 arcpy.CheckOutExtension("Spatial")
 print "done"
except:
 print "piddle"
 

print "converting the ascii files to raster files"

asciiList = os.listdir(folder)    

for x in range(len(asciiList)-1, -1, -1):
 if not asciiList.endswith(".asc"):
   asciiList.pop(x)


for file in asciiList:  
 print folderAdd + file
 print folderAdd + file[:-4]
 
 try:
  arcpy.ASCIIToRaster_conversion(folderAdd+ file, folderAdd+ file[:-4] + ".tif", "INTEGER")
 except:
  print "Processing " + file + "FAILED"
  
 time.sleep(3)
 
tifList = os.listdir(folder)

print tifList

for x in range(len(tifList) -1, -1, -1):
    if not tifList.endswith(".tif"):
        tifList.pop(x)

print tifList

tifString = ""
for tif in tifList:
 tifString += tif + ";"
 
tifString = tifString[:-1]

print tifString

print "mosaic to new raster"

try:
 arcpy.MosaicToNewRaster_management(tifString, folderAdd, catchmentRaster, "#", "16_BIT_SIGNED", "#", "1" , "MEAN")
except:
 print "Error with mosaic to new raster"
 
time.sleep(3)


print "Giving it the right coordinates"
try:
 arcpy.DefineProjection_management(folderAdd + catchmentRaster, projection)
 arcpy.ProjectRaster_management(folderAdd + catchmentRaster, folderAdd + catchmentRasterProj, projection, "BILINEAR", "#", "#","#",projection)
except: 
 print "Error giving it the right coordinates"

time.sleep(3)
 
print "Changing resolution"

try:
 outAggreg = Aggregate(catchmentRasterProj, 10, "MEDIAN", "TRUNCATE", "DATA")
 outAggreg.save(folderAdd + catchment100)
except:
 print "Error changing resolution"
 
time.sleep(3)
 
print "Filling sinks"

try:
 outFill = Fill(catchment100)
 outFill.save(folderAdd + catchment100Fill)
except:
 print "error filling sinks"
 
print "Calculating fow direction"

time.sleep(3)

try:
 outFlowDirection = FlowDirection(catchment100Fill, "NORMAL")
 outFlowDirection.save(folderAdd + catchment100Flow)
except:
 print "Error calculating flow direction"
 
 
print "Calculating flow accumulation"

time.sleep(3)

try:
 outFlowAccumulation = FlowAccumulation(catchment100Flow)
 outFlowAccumulation.save(folderAdd + catchment100Acc)
except:
 print "Error calculating flow accumulation"

print "Creating pour point snap"

time.sleep(3)

try:
 outSnapPour = SnapPourPoint(outlet, catchment100Acc, 5, "FID")
 outSnapPour.save(folderAdd + catchment100PP)
except:
 print "Error snapping pour point"
 
time.sleep(3)

print "Creating watershed"

try:
 outWatershed = Watershed(catchment100Flow, catchment100PP)
 outWatershed.save(folderAdd + catchment100WS)
except:
 print "Error creating watershed"
 
time.sleep(3)
 
print "Creating files for SHETRAN prepare"

try:
 arcpy.RasterToASCII_conversion(catchment100, folderAdd + catchmentName + "100DEM.txt")
 arcpy.RasterToASCII_conversion(catchment100WS, folderAdd + catchmentName + "100Mask.txt")
except:
 print "error creating files for SHETRAN prepare"
 
time.sleep(3)
 
print "Done!"

raw_input("close to exit")



Thanks very much
Tags (2)
0 Kudos
16 Replies
DanPatterson_Retired
MVP Emeritus
have you tried appending _sa to all of the spatial analyst tools as you have done with the _management etc tools?
0 Kudos
FabianBlau
Deactivated User
Please post the error message.
Appending _sa should not help with your way to import the functions, but try it anyway. Try also to call arcpy.Fill_sa(...) etc.. I think this would be also better readable.
0 Kudos
ElizabethLewis
Emerging Contributor
Hi,

I've tried arcpy.Aggregate_sa(...) but it still doesn't work. I don't get an error message apart from my own:

"Giving it the right coordinates"
"Changing resolution"
"Error changing resolution"
"error filling sinks"
"Calculating fow direction"
"Error calculating flow direction"
"Calculating flow accumulation"
"Error calculating flow accumulation"
"Creating pour point snap"
"Error snapping pour point"
"Creating watershed"
"Error creating watershed"
"Creating files for SHETRAN prepare"
"error creating files for SHETRAN prepare"
"Done!"

Any other ideas?

Thanks
0 Kudos
FabianBlau
Deactivated User
It would be better to print the arcgis-errormessages. Dont use try/except in this way. When any error occurs your script dont stop. The following code will crash also (in your case).
Do you have an arcinfo-license? aggregate needs it.
0 Kudos
ElizabethLewis
Emerging Contributor
I've changed the script so that it now looks like this:

try:
 outAggreg = arcpy.Aggregate_sa(catchmentRasterProj, 10, "MEDIAN", "TRUNCATE", "DATA")
 arcpy.outAggreg.save_sa(folderAdd + catchment100)
except Exception as e:
    print e.message
 


The error message is: 'module' object has no attribute 'Aggregate_sa@

Thanks
0 Kudos
FabianBlau
Deactivated User
I tried aggregate in my python window.

This didnt work (also the AttributeError :mad:):
arcpy.Aggregate_sa('ws', 'Aggrega_ws2')

This was ok:
arcpy.sa.Aggregate("ws",5,"MAXIMUM","EXPAND","DATA")
0 Kudos
ElizabethLewis
Emerging Contributor
arcpy.sa.Aggregate(...) gave this error message:

ERROR 999999: Error executing function.
failed to open raster dataset
Failed to execute <aggregate>.

Also, I'm trying to do this as a stand alone script and not in the python window.

Thanks!
0 Kudos
curtvprice
MVP Alum
have you tried appending _sa to all of the spatial analyst tools as you have done with the _management etc tools?


Dan, this changed at 10.x. The Spatial Analyst licensed functions need to be access the new python-esque way:

arcpy.sa.Con(...)
from arcpy.sa import *
out = Con(..) 


If you have a 3D license, you can access the raster tools included in 3D using the _3d suffix.
0 Kudos
curtvprice
MVP Alum
I'm trying to do this as a stand alone script and not in the python window.


Should not matter.

My guess is your raster is getting saved somewhere (scratch workspace) besides where you think it is. Your script seems to imply that your processing results are expected in "H:/PhD/PythonLessons/Scripts/testIt" but you haven't set the scratch workspace to that location.

For best results when using Spatial Analyst tools its best when they are sent to the same folder:

from arcpy.env import ENV
ENV.workspace = r"E:\work"
ENV.scratchWorkspace = ENV.workspace


Setting the current and scratch to the same folder allows temporary grids to be renamed instead of copied - this can save a lot of time with big rasters.
0 Kudos