1

I have a script that finds points outside of selected areas and creates a feature class in a file geodatabase. However, sometimes there are no points outside of the selected area. Currently, the script will create an empty feature class. I would like the script not to create the feature class if there are no points and then continue back to the top of the loop. Below is the last part of the script.

I'm using ArcGIS 10.2 Desktop.

                #Below is the process to select from previous service area and civic address selections to find Outliers

                # Process: Select Layer By Location
            SubCivic_SubPD = arcpy.SelectLayerByLocation_management(CivicPoint_lay, "INTERSECT", SAoutputPolygons, "", "NEW_SELECTION")
            print "loc 1 success"
                # Process: Select Layer by Attribute
            Switch_SCivicPD = arcpy.SelectLayerByAttribute_management(SubCivic_SubPD,"SWITCH_SELECTION")
            print "Switch success"
            SwitchCivicPD_fld = ['OBJECTID']
            Outlier_test = "\outlier"+PD_NOrow+"_"+PDPNTIDrow
            whereNotEmpty = "OBJECTID"' > ' '0'
            print "SC success"
            # Process: Make Feature Layer
            OutlierMakLay = arcpy.MakeFeatureLayer_management(Switch_SCivicPD, Switch_SCivicPD_lay)
            print "Outlier Make layer success"
            arcpy.FeatureClassToFeatureClass_conversion(OutlierMakLay, SA, Outlier_test, whereNotEmpty)
            print("Outlier Script completed successfully")
  • 1
    You could use the GetCount as a check before creating the feature class. – Eok Mar 3 '16 at 17:55
  • I think I would need to use an if statement after the getCount. I'm new to python and I don't know how I would frame that statement. – MMcInnis Mar 3 '16 at 18:03
  • 1
    yes, you'll need an if statement to check. Something like... result = arcpy.GetCount_management(my_lyr) count = int(result.getOutput(0)) if count > 0: # Feature Class to Feature Class statement You may want to learn the foundations of general Python coding if you're going to make more changes to that code. – Eok Mar 3 '16 at 18:21
2

Try adding a GetCount towards the end of your script:

print "Outlier Make layer success"
outlierCount = int(arcpy.GetCount_management(OutlierMakLay).getOutput(0))
if outlierCount > 0:
    arcpy.FeatureClassToFeatureClass_conversion(OutlierMakLay, SA, Outlier_test, whereNotEmpty)
    print("Outlier Script completed successfully")
else:
    print("No outliers found")

The outlierCount counts the number of features in your feature layer. The if outlierCount > 0: performs the Feature Class to Feature Class if there are features in the outlierCount (i.e. there is one or more features found), otherwise it returns a message to tell you there were no outliers found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.