How do I stop a process if results of a layer is empty?

5002
7
Jump to solution
11-05-2014 06:05 AM
StephenEldridge
New Contributor III


Been looking at all the Python libraries I can find and still have not been able to figure this out.  I have script that creates a layer but sometimes the end result of that later returns no records.  How do I tell Python to stop the process if this layer is empty?

 

Thanks

0 Kudos
1 Solution

Accepted Solutions
StephenEldridge
New Contributor III

I found another example that I am trying as well which worked for half of what I am doing so I made need to do 2 seperate scripts to make it work the way I want it to work.

# Name: DeleteFeatures_Example2.py

# Description: Delete features from a feature class based on an expression

# Import system modules

import arcpy

from arcpy import env

# Set environment settings

env.workspace = "C:/data/airport.gdb"

# Set local variables

inFeatures = "parcels"

outFeatures = "C:/output/output.gdb/new_parcels"

tempLayer = "parcelsLayer"

expression = arcpy.AddFieldDelimiters(tempLayer, "PARCEL_ID") + " = 'Cemetery'"

try:

    # Execute CopyFeatures to make a new copy of the feature class

    arcpy.CopyFeatures_management(inFeatures, outFeatures)

    # Execute MakeFeatureLayer

    arcpy.MakeFeatureLayer_management(outFeatures, tempLayer)

    # Execute SelectLayerByAttribute to determine which features to delete

    arcpy.SelectLayerByAttribute_management(tempLayer, "NEW_SELECTION",

                                            expression)

    # Execute GetCount and if some features have been selected, then

    #  execute DeleteFeatures to remove the selected features.

    if int(arcpy.GetCount_management(tempLayer).getOutput(0)) > 0:

        arcpy.DeleteFeatures_management(tempLayer)

        

except Exception as e:

    # If an error occurred, print line number and error message

    import traceback

    import sys

    tb = sys.exc_info()[2]

View solution in original post

0 Kudos
7 Replies
Luis_ÁngelPascual_Camino
New Contributor III

Hi Stephen.

In ArcGIS 9.3 (python 2.6) I had this code:

gp = arcgisscripting.create(9.3) # You can change it for import arcpy in v10

intable = gp.GetParameter(0) # this is the layer. If you have it already in a variable, it's done.

# open cursor

rows = gp.searchcursor(intable) # ...or arcpy instead of gp,

# maybe the correct sintax of this function in v10 is SearchCursor, read the help reference below

row = rows.next()

And now, check the row variable and decide if continue or not.

Good luck!
I hope it helps

Help reference of this function:
Ayuda de ArcGIS 10.1

Luis

0 Kudos
StephenEldridge
New Contributor III


Thanks Luis but unfortunately that didn't solve my issue.

0 Kudos
TedKowal
Occasional Contributor III

Stephen,

I am not a Python guy but in VB and C  I generally use a Record Count.  If the count is zero then exit the process else carry on.

These Links my help you to do this in Python

http://forums.esri.com/Thread.asp?c=93&f=1729&t=292293

https://community.esri.com/message/78638?sr=search&searchId=dee6a734-2d0e-42c9-8303-aab21bb2c2dc&sea...

https://community.esri.com/message/56574?sr=search&searchId=46b59d5c-a090-4485-8904-48ce486d82c0&sea...

0 Kudos
StephenEldridge
New Contributor III

Thanks Ted but that shows me how to count rows but doesn't tell me how to make the script stop running if the count is zero.

0 Kudos
StephenEldridge
New Contributor III

Let me be a little more clear on what I am trying to do.  I am running queries that seek out differences in two features and creating a point file with the differences from one to the other and adding it to a mxd all using Python script and I have this first part working great but many times the layer that is created has no records because there are no differences when the comparison is run and I do not want the script to add empty layers to my map.  Hope that clarifys a bit.

0 Kudos
TedKowal
Occasional Contributor III

....?.... 

Some ways of exiting a python script albeit not gracefully... 

sys.exit()

quit()

os.exit()

I am not a python programmer, but some pseudo code would go along these lines.  Ok, if at the point you are going to write the point file (differences)... It sound like you need to break up your python script into discrete functions then there would be no need to stop?

....

def CheckForDifferences(args to do you differences):

   ....

   ...

   ... get your record count

return CountOfRecords

def CreateFile( your args):

   ...

return

def AddToMDX( your args):

   ...

return

# Main part of your script if there are no differences then no file will be created no need to stop or exit prematurely.

Difference = CheckForDifferences(args);

if Difference > 0:

  CreateFile(your args):

  AddToMDX(your args);

0 Kudos
StephenEldridge
New Contributor III

I found another example that I am trying as well which worked for half of what I am doing so I made need to do 2 seperate scripts to make it work the way I want it to work.

# Name: DeleteFeatures_Example2.py

# Description: Delete features from a feature class based on an expression

# Import system modules

import arcpy

from arcpy import env

# Set environment settings

env.workspace = "C:/data/airport.gdb"

# Set local variables

inFeatures = "parcels"

outFeatures = "C:/output/output.gdb/new_parcels"

tempLayer = "parcelsLayer"

expression = arcpy.AddFieldDelimiters(tempLayer, "PARCEL_ID") + " = 'Cemetery'"

try:

    # Execute CopyFeatures to make a new copy of the feature class

    arcpy.CopyFeatures_management(inFeatures, outFeatures)

    # Execute MakeFeatureLayer

    arcpy.MakeFeatureLayer_management(outFeatures, tempLayer)

    # Execute SelectLayerByAttribute to determine which features to delete

    arcpy.SelectLayerByAttribute_management(tempLayer, "NEW_SELECTION",

                                            expression)

    # Execute GetCount and if some features have been selected, then

    #  execute DeleteFeatures to remove the selected features.

    if int(arcpy.GetCount_management(tempLayer).getOutput(0)) > 0:

        arcpy.DeleteFeatures_management(tempLayer)

        

except Exception as e:

    # If an error occurred, print line number and error message

    import traceback

    import sys

    tb = sys.exc_info()[2]

0 Kudos