Stop loop when empty feature class is generated.

1045
4
Jump to solution
06-03-2014 06:40 AM
NoahHuntington
Occasional Contributor
I am trying to stop the script below once an empty feature class is generated.  I have worked out the following which I imagine to be close? But still receive an error:

ExecuteError: ERROR 010151: No features found in G:\Xcel\Route Tool\Southwest\Results.gdb\LCP_4. Possible empty feature class

I think the script should explain what I am trying to accomplish?

         count = 0      while count < 10:          Output_polyline =  os.path.join(savepath  + "\\Results.gdb"  + "\\LCP_" + str(count))         End_Raster =  os.path.join(savepath  + "\\Scratch.gdb"  + "\\End_Raster_" + str(count))          #Raster to Polyline         arcpy.AddMessage("Saving Least Cost Path {0}..." .format(count + 1))         arcpy.RasterToPolyline_conversion(get_LCP(count), Output_polyline, "ZERO", "0", "SIMPLIFY", "")              # List line feature classes in Results.gdb         fclist = arcpy.ListFeatureClasses("*","Line",savepath  + "\\Results.gdb")         for fc in fclist:             lcpcount = arcpy.GetCount_management(fc)              if lcpcount == 0:                 break
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
AdamCox1
Occasional Contributor II
Oh, wait, I'm not familiar with the RasterToPolyline tool, but taking a closer look I'm thinking that it throws an exception if it will result in an empty feature class.  I don't see anything about that in the documentation, nor in the info that comes up with help(arcpy.conversion.RasterToPolyline) in IDLE.  However, it'll make things a little easier for you.  Instead of an if statement, you can just use a try/except situation:
#Raster to Polyline arcpy.AddMessage("Saving Least Cost Path {0}..." .format(count + 1)) try:     arcpy.RasterToPolyline_conversion(get_LCP(count), Output_polyline, "ZERO", "0", "SIMPLIFY", "") except:     arcpy.AddMessage("Empty feature class")     break

The only thing to be careful of is that now any problem with the raster to polyline process will look like it's an empty feature class, even if it's a problem with a different parameter, so you'll have to be aware of that.

EDIT: just a little more info.

View solution in original post

0 Kudos
4 Replies
AdamCox1
Occasional Contributor II
If you modify
lcpcount = arcpy.GetCount_management(fc)

to
lcpcount = int(arcpy.GetCount_management(fc).getOutput(0))

it should work fine.

EDIT: I may not be understanding it correctly, but looking at this more closely it seems like if you just want it to break after you have created an empty feature class you could shorten the script like so:
count = 0

while count < 10:

    Output_polyline =  os.path.join(savepath  + "\\Results.gdb"  + "\\LCP_" + str(count))
    End_Raster =  os.path.join(savepath  + "\\Scratch.gdb"  + "\\End_Raster_" + str(count))

    #Raster to Polyline
    arcpy.AddMessage("Saving Least Cost Path {0}..." .format(count + 1))
    arcpy.RasterToPolyline_conversion(get_LCP(count), Output_polyline, "ZERO", "0", "SIMPLIFY", "")

    #check to see if the new Output_polyline is empty, and break if so:
    if int(arcpy.management.GetCount(Output_polyline).getOutput(0)) == 0:
        #you could also delete the empty feature class if you want
        #arcpy.management.Delete(Output_polyline)
        break
0 Kudos
NoahHuntington
Occasional Contributor
Thanks Adam! I think your right on.  Somehow I still generated an empty dataset and script failed? Here was the error message.  Any thoughts?

Error:  Traceback (most recent call last):
  File "G:\Xcel\Route Tool\Southwest\Least Cost Paths(2).py", line 319, in <module>
    main()
  File "G:\Xcel\Route Tool\Southwest\Least Cost Paths(2).py", line 259, in main
    arcpy.RasterToPolyline_conversion(get_LCP(count), Output_polyline, "ZERO", "0", "SIMPLIFY", "")
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\conversion.py", line 288, in RasterToPolyline
    raise e
ExecuteError: ERROR 010151: No features found in G:\Xcel\Route Tool\Southwest\Results.gdb\LCP_4. Possible empty feature class.
Failed to execute (RasterToPolyline).


count = 0

while count < 10:

    Output_polyline =  os.path.join(savepath  + "\\Results.gdb"  + "\\LCP_" + str(count))
    End_Raster =  os.path.join(savepath  + "\\Scratch.gdb"  + "\\End_Raster_" + str(count))

    #Raster to Polyline
    arcpy.AddMessage("Saving Least Cost Path {0}..." .format(count + 1))
    arcpy.RasterToPolyline_conversion(get_LCP(count), Output_polyline, "ZERO", "0", "SIMPLIFY", "")

    #check to see if the new Output_polyline is empty, and break if so:
    if int(arcpy.management.GetCount(Output_polyline).getOutput(0)) == 0:
        #you could also delete the empty feature class if you want
        #arcpy.management.Delete(Output_polyline)
        break
0 Kudos
AdamCox1
Occasional Contributor II
Oh, wait, I'm not familiar with the RasterToPolyline tool, but taking a closer look I'm thinking that it throws an exception if it will result in an empty feature class.  I don't see anything about that in the documentation, nor in the info that comes up with help(arcpy.conversion.RasterToPolyline) in IDLE.  However, it'll make things a little easier for you.  Instead of an if statement, you can just use a try/except situation:
#Raster to Polyline arcpy.AddMessage("Saving Least Cost Path {0}..." .format(count + 1)) try:     arcpy.RasterToPolyline_conversion(get_LCP(count), Output_polyline, "ZERO", "0", "SIMPLIFY", "") except:     arcpy.AddMessage("Empty feature class")     break

The only thing to be careful of is that now any problem with the raster to polyline process will look like it's an empty feature class, even if it's a problem with a different parameter, so you'll have to be aware of that.

EDIT: just a little more info.
0 Kudos
NoahHuntington
Occasional Contributor
Thanks Adam! I like that. I have a tendency to try things the hard way:)
0 Kudos