import gc # ... #enable garbage collection gc.enable()
It would be greatly appreciated if you would be willing to email me a copy for ArcGIS 10.
Peter - please email me (contact info in in my sig below)
I got an error at first that I had no FID field, my zonal feature had ObjectID instead of FID fields,...
strFID = '"FID" =' ... arcpy.SelectLayerByAttribute_management(zone, newSelection, strFID + str(inRow.FID))
strFID = arcpy.Describe(inputFeatureZone).OIDFieldName ... strQry = '"%s" = %s' % (strFID, inRow.GetValue(strFID)) arcpy.SelectLayerByAttribute_management(zone, newSelection, strQry)
#create overlapping polygons with intersect tool (intersect with self to get overlapping) arcpy.Intersect_analysis([inputParcelLayer],FeatureZone,"NO_FID","","")
Hi Curtis,It is the garbage collection module. We were running into some memory issues after several iterations of the tool. The garbage collection module clean up unreferenced objects in memory. Take a look t this link.http://docs.python.org/library/gc.html
#create overlapping polygons with intersect tool (intersect with self, only creates partial parcels where overlapping) arcpy.Intersect_analysis([inputParcelLayer],FeatureIntersect,"NO_FID","","") #make feature layer, join with self intersected parcels, use result as overlapping parcels for zonal stats arcpy.MakeFeatureLayer_management(inputParcelLayer,"parcelsoverlap") arcpy.AddJoin_management("parcelsoverlap","APN",FeatureIntersect,"APN","KEEP_COMMON") arcpy.CopyFeatures_management("parcelsoverlap", FeatureZone) tempDIR = arcpy.GetSystemEnvironment("TEMP") #create scratch workspace name tempWorkspace = "tempWorkspaceForZonalStats" if not arcpy.Exists(tempDIR + os.sep + tempWorkspace): arcpy.CreateFolder_management(tempDIR, tempWorkspace) arcpy.env.workspace = tempDIR + os.sep + tempWorkspace #input variables strFIDname = arcpy.Describe(FeatureZone).OIDFieldName zone = "zone" arcpy.env.cellSize = 2 #needed to reduce cell size because zonal stats crashes if the polygon is smaller than raster resolution gc.enable() #enable garbage collection arcpy.CheckOutExtension("Spatial") cellAssign = "CELL_CENTER" priority = "NONE" newSelection = "NEW_SELECTION" # Make a layer from the feature class arcpy.MakeFeatureLayer_management(FeatureZone, zone) #Loop through features numFeats = int(arcpy.GetCount_management(FeatureZone).getOutput(0)) groups = [] bracket = 30 while(bracket < numFeats): groups.append(bracket) bracket = bracket + 30 groups.append(numFeats+1) for x in groups: whileClause = '"OBJECTID" >= ' + str(x-30) + ' and "OBJECTID" < ' + str(x) print whileClause inRows = arcpy.SearchCursor(FeatureZone,whileClause) #inRows = arcpy.SearchCursor(FeatureZone) inRow = inRows.next() while inRow: #selct the fid to process strID = inRow.getValue(strFIDname) print ' processing objID: ' + str(strID) + 'of ' + str(numFeats) + ' total features.' strQry = '"%s" = %s' % (strFIDname, strID) arcpy.SelectLayerByAttribute_management(zone, newSelection, strQry) #create a unique name for the zone uniqueZone = arcpy.CreateUniqueName("zone.shp", arcpy.env.workspace) uniqueTable = arcpy.CreateUniqueName("ZSasT", arcpy.env.workspace) #create a temporary feature to use for zonal stats as table arcpy.CopyFeatures_management(zone, uniqueZone) outZSaT = ZonalStatisticsAsTable(uniqueZone, joinField, valueRaster, uniqueTable, "NODATA", "ALL") #move to next record. inRow = inRows.next() del inRow del inRows
Interestingly enough, a "zone" is processed in about 20 seconds at the outset, and then towards the end of the 800 features, it slows down to about 5 minutes to process one zone.
for x in groups: #create the tempworkspace tempWS = tempWorkspacePrefix + str(tempWorkspaceSuffix) if not arcpy.Exists(tempDIR + os.sep + tempWS): arcpy.CreateFolder_management(tempDIR, tempWS) arcpy.env.workspace = tempDIR + os.sep + tempWS while......................(rest of the loop as in my previous post) tempWorkspaceSuffix = tempWorkspaceSuffix + 1;
import arcpy, os, tempfile, shutil, sys, time, traceback from arcpy import env from arcpy.sa import * # Check out any necessary licenses arcpy.CheckOutExtension("spatial") # Set the current workspace, this is folder that contains the .tif files for climate variables arcpy.env.workspace = "F:\\Data\\Reg09\\tifs" # Set Geoprocessing environments arcpy.env.overwriteOutput = True #Set Local variables region =('F:\\Data\\NHD\\grids\\reg09_lambi') # this is the folder with the NHD catchment grid (raster) out = "F:\\Data\\Reg09\\update\\" rasterList = arcpy.ListRasters("*", "TIF") for raster in rasterList: name = os.path.basename(raster).strip(".tif") # removes .tif from the file name arcpy.env.cellSize = "MINOF" arcpy.gp.ZonalStatisticsAsTable_sa(region, "VALUE", raster, out+name+".dbf","DATA", "ALL") print" Raster " + str(raster) + " processing completed at " + time.asctime( time.localtime(time.time()) )
gp/arcpy scratch name methods can be very slow when the workspace in which you are generating scratchnames gets full of many files
#create scratch workspace name tempDIR = "F:\\Data\\Reg09" tempWorkspacePrefix = "update" tempWorkspaceSuffix = 0 count = 0; for raster in rasterList: if count%30 == 0: # this is modulus operator, so should be true the first iteration and every 30 iterations thereafter. tempWorkspaceSuffix = tempWorkspaceSuffix + 1 tempWS = tempWorkspacePrefix + str(tempWorkspaceSuffix) if not arcpy.Exists(tempDIR + os.sep + tempWS): arcpy.CreateFolder_management(tempDIR, tempWS) out = tempDIR + os.sep + tempWS + os.sep name = os.path.basename(raster).strip(".tif") # removes .tif from the file name arcpy.env.cellSize = "MINOF" arcpy.gp.ZonalStatisticsAsTable_sa(region, "VALUE", raster, out+name+".dbf","DATA", "ALL") print" Raster " + str(raster) + " processing completed at " + time.asctime( time.localtime(time.time()) ). count = count + 1
My python code is below....any suggestions on how to modify this code?I should add that sometimes this script runs and sometimes not.
import gc gc.enable()
os.path.basename(os.path.splitext(path)[0])
File "F:\Data\Reg09\attributetifsReg09.py", line 56, in <module> arcpy.gp.ZonalStatisticsAsTable_sa(region, "VALUE", raster, out+name+".dbf","DATA", "ALL") <class 'arcgisscripting.ExecuteError'>: ERROR 999999: Error executing function. Workspace or data source is read only. Workspace or data source is read only. [The F:\Data\Reg09\update1\ workspace is read only.] ERROR 010067: Error in executing grid expression. Zonal statistics program failed Failed to execute (ZonalStatisticsAsTable). ERROR 999999: Error executing function. Workspace or data source is read only. Workspace or data source is read only. [The F:\Data\Reg09\update1\ workspace is read only.] ERROR 010067: Error in executing grid expression. Zonal statistics program failed Failed to execute (ZonalStatisticsAsTable).
[The F:\Data\Reg09\update1\ workspace is read only.]ERROR 010067: Error in executing grid expression.Failed to execute (ZonalStatisticsAsTable).
if count%30 == 0: # this is modulus operator, so should be true the first iteration and every 30 iterations thereafter.
We recently published a toolbox (first prize at the ESRI UC 2011 AppFair!) that includes tools that do overlap processing for statistics and area tabulations, among other things.USGS Open-File Report 2010�??1268National Water-Quality Assessment (NAWQA) Area-Characterization ToolboxBy Curtis V. Price, Naomi Nakagaki, and Kerie J. Hitthttp://pubs.usgs.gov/of/2010/1268/The published version has only limited support for 10.x, but I have an updated version that will be posted there soon. If you are interested, let me know and I can send it your way.
Is there a link to an updated tool that we could download?
> I am interested in checking out the tool for use with 10. Is it available on the web?
A version of the NAWQA toolbox that runs under 10.x is available in ArcGIS Online here:
http://www.arcgis.com/home/item.html?id=cbb59504f59f4e18b23817fb0ef40e56
サインインしたメンバーは投稿、更新のフォローなどができます。初めてですか?無料アカウントを登録してください。
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.