<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Stop loop when empty feature class is generated. in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/stop-loop-when-empty-feature-class-is-generated/m-p/534316#M41868</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&amp;nbsp; I don't see anything about that in the &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001200000009000000" rel="nofollow" target="_blank"&gt;documentation&lt;/A&gt;&lt;SPAN&gt;, nor in the info that comes up with help(arcpy.conversion.RasterToPolyline) in IDLE.&amp;nbsp; However, it'll make things a little easier for you.&amp;nbsp; Instead of an if statement, you can just use a try/except situation:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#Raster to Polyline arcpy.AddMessage("Saving Least Cost Path {0}..." .format(count + 1)) try: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RasterToPolyline_conversion(get_LCP(count), Output_polyline, "ZERO", "0", "SIMPLIFY", "") except: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Empty feature class") &amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT: just a little more &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://help.arcgis.com/EN/arcgisdesktop/10.0/help/index.html#//00vq0000000m010151" rel="nofollow" target="_blank"&gt;info&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 03 Jun 2014 16:24:20 GMT</pubDate>
    <dc:creator>AdamCox1</dc:creator>
    <dc:date>2014-06-03T16:24:20Z</dc:date>
    <item>
      <title>Stop loop when empty feature class is generated.</title>
      <link>https://community.esri.com/t5/python-questions/stop-loop-when-empty-feature-class-is-generated/m-p/534313#M41865</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to stop the script below once an empty feature class is generated.&amp;nbsp; I have worked out the following which I imagine to be close? But still receive an error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ExecuteError: ERROR 010151: No features found in G:\Xcel\Route Tool\Southwest\Results.gdb\LCP_4. Possible empty feature class&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think the script should explain what I am trying to accomplish?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; count = 0&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; while count &amp;lt; 10:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Output_polyline =&amp;nbsp; os.path.join(savepath&amp;nbsp; + "\\Results.gdb"&amp;nbsp; + "\\LCP_" + str(count)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End_Raster =&amp;nbsp; os.path.join(savepath&amp;nbsp; + "\\Scratch.gdb"&amp;nbsp; + "\\End_Raster_" + str(count))&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Raster to Polyline &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Saving Least Cost Path {0}..." .format(count + 1)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RasterToPolyline_conversion(get_LCP(count), Output_polyline, "ZERO", "0", "SIMPLIFY", "")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # List line feature classes in Results.gdb &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fclist = arcpy.ListFeatureClasses("*","Line",savepath&amp;nbsp; + "\\Results.gdb") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for fc in fclist: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lcpcount = arcpy.GetCount_management(fc)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lcpcount == 0: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Jun 2014 13:40:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stop-loop-when-empty-feature-class-is-generated/m-p/534313#M41865</guid>
      <dc:creator>NoahHuntington</dc:creator>
      <dc:date>2014-06-03T13:40:00Z</dc:date>
    </item>
    <item>
      <title>Re: Stop loop when empty feature class is generated.</title>
      <link>https://community.esri.com/t5/python-questions/stop-loop-when-empty-feature-class-is-generated/m-p/534314#M41866</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you modify &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;lcpcount = arcpy.GetCount_management(fc)&lt;/PRE&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;to&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;lcpcount = int(arcpy.GetCount_management(fc).getOutput(0))&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;it should work fine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
count = 0

while count &amp;lt; 10:

&amp;nbsp;&amp;nbsp;&amp;nbsp; Output_polyline =&amp;nbsp; os.path.join(savepath&amp;nbsp; + "\\Results.gdb"&amp;nbsp; + "\\LCP_" + str(count))
&amp;nbsp;&amp;nbsp;&amp;nbsp; End_Raster =&amp;nbsp; os.path.join(savepath&amp;nbsp; + "\\Scratch.gdb"&amp;nbsp; + "\\End_Raster_" + str(count))

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Raster to Polyline
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Saving Least Cost Path {0}..." .format(count + 1))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RasterToPolyline_conversion(get_LCP(count), Output_polyline, "ZERO", "0", "SIMPLIFY", "")

&amp;nbsp;&amp;nbsp;&amp;nbsp; #check to see if the new Output_polyline is empty, and break if so:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if int(arcpy.management.GetCount(Output_polyline).getOutput(0)) == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #you could also delete the empty feature class if you want
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #arcpy.management.Delete(Output_polyline)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:12:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stop-loop-when-empty-feature-class-is-generated/m-p/534314#M41866</guid>
      <dc:creator>AdamCox1</dc:creator>
      <dc:date>2021-12-11T23:12:55Z</dc:date>
    </item>
    <item>
      <title>Re: Stop loop when empty feature class is generated.</title>
      <link>https://community.esri.com/t5/python-questions/stop-loop-when-empty-feature-class-is-generated/m-p/534315#M41867</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Adam! I think your right on.&amp;nbsp; Somehow I still generated an empty dataset and script failed? Here was the error message.&amp;nbsp; Any thoughts?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Error:&amp;nbsp; Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "G:\Xcel\Route Tool\Southwest\Least Cost Paths(2).py", line 319, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; main()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "G:\Xcel\Route Tool\Southwest\Least Cost Paths(2).py", line 259, in main&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RasterToPolyline_conversion(get_LCP(count), Output_polyline, "ZERO", "0", "SIMPLIFY", "")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\conversion.py", line 288, in RasterToPolyline&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise e&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ExecuteError: ERROR 010151: No features found in G:\Xcel\Route Tool\Southwest\Results.gdb\LCP_4. Possible empty feature class.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (RasterToPolyline).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
count = 0

while count &amp;lt; 10:

&amp;nbsp;&amp;nbsp;&amp;nbsp; Output_polyline =&amp;nbsp; os.path.join(savepath&amp;nbsp; + "\\Results.gdb"&amp;nbsp; + "\\LCP_" + str(count))
&amp;nbsp;&amp;nbsp;&amp;nbsp; End_Raster =&amp;nbsp; os.path.join(savepath&amp;nbsp; + "\\Scratch.gdb"&amp;nbsp; + "\\End_Raster_" + str(count))

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Raster to Polyline
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Saving Least Cost Path {0}..." .format(count + 1))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RasterToPolyline_conversion(get_LCP(count), Output_polyline, "ZERO", "0", "SIMPLIFY", "")

&amp;nbsp;&amp;nbsp;&amp;nbsp; #check to see if the new Output_polyline is empty, and break if so:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if int(arcpy.management.GetCount(Output_polyline).getOutput(0)) == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #you could also delete the empty feature class if you want
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #arcpy.management.Delete(Output_polyline)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:12:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stop-loop-when-empty-feature-class-is-generated/m-p/534315#M41867</guid>
      <dc:creator>NoahHuntington</dc:creator>
      <dc:date>2021-12-11T23:12:58Z</dc:date>
    </item>
    <item>
      <title>Re: Stop loop when empty feature class is generated.</title>
      <link>https://community.esri.com/t5/python-questions/stop-loop-when-empty-feature-class-is-generated/m-p/534316#M41868</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&amp;nbsp; I don't see anything about that in the &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001200000009000000" rel="nofollow" target="_blank"&gt;documentation&lt;/A&gt;&lt;SPAN&gt;, nor in the info that comes up with help(arcpy.conversion.RasterToPolyline) in IDLE.&amp;nbsp; However, it'll make things a little easier for you.&amp;nbsp; Instead of an if statement, you can just use a try/except situation:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#Raster to Polyline arcpy.AddMessage("Saving Least Cost Path {0}..." .format(count + 1)) try: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RasterToPolyline_conversion(get_LCP(count), Output_polyline, "ZERO", "0", "SIMPLIFY", "") except: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Empty feature class") &amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT: just a little more &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://help.arcgis.com/EN/arcgisdesktop/10.0/help/index.html#//00vq0000000m010151" rel="nofollow" target="_blank"&gt;info&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Jun 2014 16:24:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stop-loop-when-empty-feature-class-is-generated/m-p/534316#M41868</guid>
      <dc:creator>AdamCox1</dc:creator>
      <dc:date>2014-06-03T16:24:20Z</dc:date>
    </item>
    <item>
      <title>Re: Stop loop when empty feature class is generated.</title>
      <link>https://community.esri.com/t5/python-questions/stop-loop-when-empty-feature-class-is-generated/m-p/534317#M41869</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Adam! I like that. I have a tendency to try things the hard way:)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Jun 2014 20:57:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stop-loop-when-empty-feature-class-is-generated/m-p/534317#M41869</guid>
      <dc:creator>NoahHuntington</dc:creator>
      <dc:date>2014-06-05T20:57:41Z</dc:date>
    </item>
  </channel>
</rss>

