1

I am running Arc 10.5 on Windows 10 and am having issues with the Spatial Analyst tool ZonalStatisticsAsTable that appears to crash my python script for no reason. I have used this script before and it ran fine for many other inputs but for some reason when I use it on this particular set of inputs, it randomly crashes. Basically what I am trying to do is calculate a "MEDIAN" while using a mask. All inputs are integer grids, with projection defined the same, and attribute tables constructed for each input raster. For this particular set of inputs ( using a mask which is denoted by my list, 81 and 82 which is a variable to call the raster: arcpy.env.mask = r"U:/NAWQA/GIS/FlowDist/nlcd11"+id) my script will randomly crash. It will run and process the first 4 inputs and then crash. I can manually do the operation on these sets of inputs in ArcMap with no problem but I cannot get myscript to run properly in Pyscripter. I am including my script below.

import os, sys, arcpy
from arcpy import env
from arcpy import sa
from arcpy.sa import *
from datetime import datetime
wks = r"U:/NAWQA/GIS/FlowDist/Distances"
env.workspace = wks
startTime = datetime.now()
# Check out Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True
env.qualifiedFieldNames = False

# Set the Geoprocessing environment...
arcpy.scratchWorkspace = "U:/NAWQA/GIS/FlowDist/Distances"
arcpy.env.geographicTransformations = ""
arcpy.env.outputCoordinateSystem =                 

arcpy.env.pyramid = "NONE"
NHD_dir = "Q:/WORK/NHDPlusV2"
list = ['81' '82']
for id in list:
    tv = "tv"
    tvStat = "tvStat"
    tmpTable2 = "temp2.dbf"
    #Local variables...

    inputs = {'CA':['18'],'CO':['14','15'],'GB':['16'],'GL':['04'],'MA':    ['02'],'MS':['05','06','07','08','10L','10U','11'],'NE':['01'],'PN':['17'],\
          'RG':['13'],'SA':['03N','03S','03W'],'SR':['09'],'TX':['12']}
    for regions in inputs.keys():
        for hydro in inputs[regions]:
            print 'Region: ' + regions + ' Production unit: ' + hydro
            inZoneData = "%s/NHDPlus%s/NHDPlus%s/NHDPlusCatchment/cat"%    (NHD_dir,regions,hydro)
            print inZoneData
            zoneField = "VALUE"
            print zoneField
        valGrid = r"Q:/GIS/Flow/National_Flow_Dist_Down/flwdist"
        print valGrid
        outTable = "temp0.dbf"
        print outTable
        arcpy.env.cellSize = inZoneData
        arcpy.env.Extent = inZoneData
        arcpy.env.mask = r"U:/NAWQA/GIS/FlowDist/nlcd11"+id
        outTable1 = "U:/NAWQA/GIS/FlowDist/Distances/D"+id+"/Median_DIST_PU%s.dbf"%(hydro)
        outTable1 = os.path.join(wks,outTable1)
        catShape= "%s/NHDPlus%s/NHDPlus%s/NHDPlusCatchment/catchment.shp"%(NHD_dir,regions,hydro)
        print catShape
        print outTable1
        outZSaT = ZonalStatisticsAsTable(inZoneData, zoneField, valGrid, outTable, "DATA", "MEDIAN")
# join catchment shape attributes to stats and write out
        arcpy.MakeTableView_management(catShape,tv)
        arcpy.MakeTableView_management(outTable,tvStat)
        #arcpy.AddIndex_management(tvStat, "VALUE","IDX")
        arcpy.AddJoin_management (tv, "GRIDCODE", tvStat, "VALUE")
        arcpy.CopyRows_management(tv,tmpTable2)
        print catShape
# Set up desired fields and order for output
        arcpy.CreateTable_management(os.path.dirname(outTable1), os.path.basename(outTable1))
        arcpy.AddField_management(outTable1,"FEATUREID","LONG")
        arcpy.AddField_management(outTable1,"AREASQKM","DOUBLE")
        arcpy.AddField_management(outTable1,"basin_area","DOUBLE")
        arcpy.AddField_management(outTable1,"nodata_are","DOUBLE")
        arcpy.AddField_management(outTable1,"NODATA","DOUBLE")
        arcpy.AddField_management(outTable1,"AREA","DOUBLE")
        arcpy.AddField_management(outTable1,"MEDIAN","DOUBLE") # statistic value
        arcpy.AddField_management(outTable1,"DIST"+id,"DOUBLE") # raster name #statistic value
        arcpy.AddField_management(outTable1,"COMID","LONG")
# delete dummy field added by CreateTable
        arcpy.DeleteField_management(outTable1,"ID")
# Copy matching fields to the output table
        arcpy.Append_management(tmpTable2,outTable1,"NO_TEST")
# tag non-matches with the zonal stat = -9999
        arcpy.MakeTableView_management(outTable1,tv)
        arcpy.CalculateField_management(tv,'COMID', '!FEATUREID!', "PYTHON_9.3")
        arcpy.CalculateField_management(tv,'DIST'+id, '!MEDIAN!', "PYTHON_9.3")
        arcpy.CalculateField_management(tv,'basin_area', '!AREASQKM! * 1000000', "PYTHON_9.3")
        arcpy.CalculateField_management(tv,'nodata_are', '!basin_area! - !AREA!', "PYTHON_9.3")
        arcpy.CalculateField_management(tv,'NODATA', '100 * !nodata_are! / !basin_area!', "PYTHON_9.3")
        where = "AREA = 0" # if output is GDB use "AREA IS NULL"
        arcpy.SelectLayerByAttribute_management(tv,"",where)
        arcpy.CalculateField_management(tv,"DIST"+id,"-9999", "PYTHON_9.3")
  • What are the precise symptoms of your "crash". – PolyGeo Sep 14 '17 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.