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")
env.scratchWorkspace = env.workspaceand tried this set up
try: outAggreg = Aggregate(catchmentRasterProj, 10, "MEDIAN", "TRUNCATE", "DATA") outAggreg.save(catchment100) except Exception as e: print e.message
It now gives the error 'failed to open raster dataset'. I'm not sure why it won't open it.
if not arcpy.exists(catchmentRasterProj): raise Exception, "%s does not exist" % catchmentRasterProj
outFill.save(folderAdd + catchment100Fill)
outFill.save(catchment100Fill)
as well as an error it creates a new folder within the workspace called 'aggrega_catc1' but I thought that setting the scratch workspace to the env workspace would stop this?
I had been trying to do something similar with the SnapPointPoint. I am converting from the gp to arcpy.
What I found was I was trying to create a grid and it didn't like it.
So I created a filegeodatabase for my results to go into. This seems to have solved my issue.
All of my original data is in GRID from arcgis 9.3 I plan to convert it, but I wanted to get the python scripts working first.
import arcpy import os import time import sys from arcpy import env from arcpy.sa import * env.workspace = "C:/temp/testIt" env.scratchWorkspace = env.workspace 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 = "C:/temp/testIt" folderAdd = "C:/temp/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 "Didn't work!" 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(catchmentRaster, projection) arcpy.ProjectRaster_management(catchmentRaster, catchmentRasterProj, projection, "BILINEAR", "#", "#","#",projection) except Exception as e: print e.message time.sleep(3) print "Changing resolution" try: outAggreg = Aggregate(folderAdd + catchmentRasterProj, 10, "MEDIAN", "TRUNCATE", "DATA") outAggreg.save(catchment100) except Exception as e: print e.message time.sleep(3) print "Filling sinks" try: outFill = Fill(catchment100) outFill.save(catchment100Fill) except Exception as e: print e.message print "Calculating fow direction" time.sleep(3) try: outFlowDirection = FlowDirection(catchment100Fill, "NORMAL") outFlowDirection.save(catchment100Flow) except Exception as e: print e.message print "Calculating flow accumulation" time.sleep(3) try: outFlowAccumulation = FlowAccumulation(catchment100Flow) outFlowAccumulation.save(catchment100Acc) except Exception as e: print e.message print "Creating pour point snap" time.sleep(3) try: outSnapPour = SnapPourPoint(outlet, catchment100Acc, 5, "FID") outSnapPour.save(catchment100PP) except Exception as e: print e.message time.sleep(3) print "Creating watershed" try: outWatershed = Watershed(catchment100Flow,catchment100PP) outWatershed.save(catchment100WS) except Exception as e: print e.message time.sleep(3) print "Creating files for SHETRAN prepare" try: arcpy.RasterToASCII_conversion(catchment100, catchmentName + "100DEM.txt") arcpy.RasterToASCII_conversion(catchment100WS, catchmentName + "100Mask.txt") except Exception as e: print e.message time.sleep(3) print "Done!" raw_input("close to exit")