Arcpy script hanging after ExtractbyAttributes

542
3
04-16-2012 12:13 PM
AnneRiddle
New Contributor
This code that I've written runs fine up until Extract by Attributes, then hangs (continues running, with no output). What did I do wrong?


import arcpy
arcpy.CheckOutExtension ("Spatial")
from arcpy import env
from arcpy.sa import *

#Allows overwriting
arcpy.env.overwriteOutput = True

##Set workspace; necessary for arcpy scripts
##note that following commands only work for the defined workspace: i.e., the ExtractbyAttributes command automatically convert all rasters in the geodatabase specified 
arcpy.env.workspace = r"S:\FIRE\year1992to2000"

####iteratively defines each subfolder as a workspace; for each raster in each subfolder, converts to specified geodatabase 
workspaces = arcpy.ListWorkspaces()
for workspace in workspaces:
    arcpy.env.workspace = workspace
    for raster in arcpy.ListRasters(): 
        arcpy.RasterToGeodatabase_conversion (raster, r"S:\FIRE\t92to00.gdb")

#resets workspace to geodatabase mentioned above
#the "r" before the pathname tells python that this is an absolute pathname. Hard to explain, but some combinations of slashes and characters are read as commands (for example, /g means the end of a line) by arcgis and will cause it to parse the pathname incorrectly. The "r" prevents this.
arcpy.env.workspace = r"S:\FIRE\t92to00.gdb"

#adds table for final append operation
#table created manually--you could create one within the program if you wanted, using the CreateTable and AddField commands
outtable = r"S:\FIRE\73to80.gdb\y92to00"

#for each raster in geodatabase, extract by attributes to include only pixels with the correct values, in this case, 6 and 2
#rename resulting rasters; "af" refers to conversions from forest and ag only 
for raster in arcpy.ListRasters(): 
    outpoint = raster + "af"
    extract = arcpy.sa.ExtractByAttributes (raster, " VALUE = 6 OR VALUE = 8")
    extract.save (outpoint)
    
#"for" loop calls only rasters with "af" in the name
#RastertoPoint_conversion converts rasters to point files to enable use of Mean Center
#rename resulting shapefiles; keep original file information
for raster in arcpy.ListRasters ("af", "*"):
    outpoint= raster[:8] + "p" 
    arcpy.RastertoPoint_Conversion (raster, outpoint)

#"for" loop calls only point feature classes in the workspace
#MeanCenter_stats creates a new point shapefile with a single point, the mean center of the points extracted in the previous step
#rename the resulting shapefiles 
for fc in arcpy.ListFeatureClasses ("p", "Point"): 
    outmean= fc[:8] + "m"
    arcpy.MeanCenter_stats (fc, outmean)
    uid = fc[1:8]
    arcpy.AddField_management(outmean, "UID", "TEXT")
    arcpy.CalculateField_management(outmean, "UID", '"'+uid+'"')

#"for" loop calls only point feature classes with the phrase "mean" in their filename
#Near_analysis adds new columns to the mean center point file 
#"infeat" is a single shapefile containing the borders of public lands in California
for fc in arcpy.ListFeatureClasses ("m", "Point"):
    infeat= "S:\FIRE\ben_data\NPSFSBLM"
    arcpy.Near_analysis (fc, infeat)
    arcpy.MakeTableView (fc, "tv")
    arcpy.AppendManagement ("tv", outtable, "NO_TEST", "", "")

Tags (2)
0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus
If memory serves an equality check requires 2 equal signs
" VALUE = 6 OR VALUE = 8"
might be
" VALUE == 6 OR VALUE == 8"
or the checks need to be enclosed in brackets
(VALUE = 6) OR (VALUE = 8")
or
(VALUE == 6) OR (VALUE == 8")
try it manually in the raster calculator then copy the syntax
0 Kudos
AnneRiddle
New Contributor
Extract by Attributes is working fine--it is hanging after that, on the Raster to Point conversion. The extracted rasters are being created correctly, but the script doesn't continue to move forward after that. Sorry if that was unclear.
0 Kudos
GeordieHobart
New Contributor
Has anyone found a solution to this?? I am constantly running in to issues where arcpy hangs when running scripts that have thousands of iterations. YES, I'm deleting all my objects. In the good old 9.3 days I could delete my gp object and then create a new one every five iterations or so but this isn't an option in 10.0. Does anyone have a hack to get around this serious show stopping issue?
0 Kudos