caitlinboon

Eliminate loop in Python

Discussion created by caitlinboon on Jun 6, 2012
Latest reply on Jun 6, 2012 by mzcoyle
Hi,

I'm running ArcGIS 9.3.1 & Python 2.5.

We've got hundreds of shapefiles with tiny polygons ( < 0.025 acres). Some of them require running an eliminate multiple times to get all of the small polygons gone (for whatever reason).

So, I know the needed steps are:

- Calculate area (acres)
- Select by attribute ( <0.025 acres)
- Eliminate selected features.

This needs to repeat (recalculate area, again select small polygons, and eliminate) until there are no small polygons left.

Here's the code we're working with right now (the last section of a longer code that adds some fields, runs clips and updates, and other things).

It gets to where it prints "Done Calculating Acres" and then nothing, not even an error message.

How does this need to change?

Thanks!

eliminate_file = elim_path + infile + "_elim"
try_file = update_path + infile + "_update.shp" 
# Calculate Acres...'
#if fnmatch.fnmatch(infile, + '*.shp') & (infile not in fail_list):
try_file = update_path + infile + "_update.shp"  
print "\n" + infile + "--------------"
print try_file + "\n"
print "Calculating Acres...\n"
try:
    gp.CalculateField_management(try_file, "ACRES", "!SHAPE.AREA@ACRES!", "PYTHON", "")
except:
    fail_list.append(infile)
    print "Error calculating acres for " + infile
    print gp.AddError(gp.GetMessages())
   
print "Done Calculating Acres"
# SelectbyAttributes
try:
    if infile not in fail_list:
        gp.MakeFeatureLayer_management(try_file,outtemp)
        gp.SelectLayerByAttribute_management(outtemp,"NEW_SELECTION", "\"ACRES\" < '0.025'")
        countdat = gp.GetCount_management(outtemp)
        print countdat.getOutput(0)
        print countdat.getoutput(0) + " features selected from " + infile
        print "Selection Complete..."
        print "Eliminating"
        # Process: Eliminate...
        try:
            if infile not in fail_list:
                gp.Eliminate_management(outtemp, eliminate_file, "LENGTH", "", "")
                elim_count = 0
                while countdat > 0:
                    elim_count = elim_count + 1
                    print "Calculating Acres..."
                    try:                
                        gp.CalculateField_management(eliminate_file, "ACRES", "Double(!SHAPE.AREA@ACRES!)", "PYTHON", "")
                        try:
                            print "seleting attributes..."
                            gp.MakeFeatureLayer_management(eliminate_file,outtempdat2,)
                            gp.SelectLayerByAttribute_management(outtempdat2,"NEW_SELECTION", "\"ACRES\" < '0.025'")
                            countdat = gp.GetCount_management(outtempdat2)
                            print countdat.getoutput(0) + " features selected from " + infile               
                            try:
                                print "eliminating..." + infile
                                gp.Eliminate_management(outtempdat, eliminate_file, "LENGTH", "", "")
                            except:
                                print infile + " elimination unsuccessful..."
                        except:
                            print gp.GetMessages(0)
                            eliminate_file = eliminate_file + elim_count
                    except:
                        fail_list.append(infile)
                        print "----------Error calculating acres for " + infile + "-------------"
                        print gp.GetMessages(0)                                  
        except:
            print "Error " + infile
except:
    print "FAILURE on " + infile

Outcomes