1

Still working on cleaning this up but it runs fine. Below is the rough code for a tool that iterates through field rows and clips accordingly to that row's shape. Everything outputs nicely but there are some outputs that do not contain data. What can I add to have arcmap check for shapefiles containing no data and delete them?

Added an if statement to check for empty SHP's but not sure how to define where to have it check the output folder?

import arcpy
from arcpy import env
arcpy.env.workspace = r'path\folder'
field = "PARISH"
fc = arcpy.GetParameterAsText(0)
Name = arcpy.GetParameterAsText(1)
Path = arcpy.GetParameterAsText(2)
rows = arcpy.SearchCursor(arcpy.env.workspace)
for row in rows:
 feat = row.Shape
 outputName = Name.replace(" ", "_")
 outputField = row.getValue(field)
 outputFieldmod = outputField.replace(" ", "_")
 arcpy.Clip_analysis(fc, feat, str(Path) + r'/' + str(outputName) + "_" + str(outputFieldmod))
for file in arcpy.ListFiles():
 if arcpy.management.GetCount(file)[0] == "0":
 arcpy.management.Delete(file)
 arcpy.AddMessage('Deleted "{}" because it contains no features!'.format(file))
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked May 5, 2015 at 18:35
0

2 Answers 2

3

You could add GetCount to your code:

import arcpy
import os
arcpy.env.workspace = r'path\folder'
field = "PARISH"
fc = arcpy.GetParameterAsText(0)
Name = arcpy.GetParameterAsText(1)
Path = arcpy.GetParameterAsText(2)
rows = arcpy.SearchCursor(arcpy.env.workspace)
for row in rows:
 feat = row.Shape
 outputName = Name.replace(" ", "_")
 outputField = row.getValue(field)
 outputFieldmod = outputField.replace(" ", "_")
 if int(arcpy.management.GetCount(fc).getOutput(0)):
 arcpy.Clip_analysis(fc, feat, os.path.join(Path, outputName + "_" + str(outputFieldmod)))
 arcpy.AddMessage('Clipped: "{}"'.format(fc))
 else:
 arcpy.management.Delete(fc)
 arcpy.AddMessage('Deleted "{}" because it contains no features!'.format(fc))
answered May 5, 2015 at 18:45
4
  • It runs but it still clips everything, regardless of if there is data in the fc area being clipped or not. Commented May 5, 2015 at 19:26
  • Weird try explicitly checking to see if the count is greater than 0: if int(arcpy.management.GetCount(fc).getOutput(0)) > 0: Commented May 5, 2015 at 19:40
  • ah wait. It's checking the input fc for the feature number which will always have at least one feature. I need ti to remove the shapefiles that have been outputted with zero features to be removed. I think that's the most simplistic approach without adding an overcomplicated check when selecting by location within the row shape....at least that's my opinion but I'm still new to most of this. I added some code from the previously solved problem but not sure what to use for defining where ListFiles has to look? Commented May 5, 2015 at 19:54
  • Did you ever figure this out? I see that the above is marked as the answer despite your comment that it isn't doing what you want. Commented Dec 8, 2015 at 22:22
0

I'm going to attempt to answer this by posting some code I wrote that does what I think it is you're asking: delete empty files after they've been created, so that you can bypass a time consuming intersect. This is from code that clips one fc by each feature in another fc.

#set workspace to check if features are empty later
arcpy.env.workspace = outputDir
#make iterable
outFCs = arcpy.arcpy.ListFeatureClasses()
#set up list to check for delete later
fcList = []

 for row in rows:
 feat = row.Shape
 outputName = Name.replace(" ", "_")
 outputField = row.getValue(field)
 outputFieldmod = outputField.replace(" ", "_")
 arcpy.Clip_analysis(fc, feat, str(Path) + r'/' + str(outputName) + "_" + str(outputFieldmod))
 arcpy.Clip_analysis(inFeatures, feat, out_feature_class, "")

 for fc in outFCs:
 #make lyr so GetCount works
 arcpy.MakeFeatureLayer_management(fc, "fc_lyr")
 count = int(arcpy.GetCount_management("fc_lyr").getOutput(0))
 arcpy.Delete_management("fc_lyr")
 if count == 0:
 if fc in fcList:
 arcpy.AddMessage(fc + "has no features")
 arcpy.AddMessage(fc + "is being deleted")
 arcpy.Delete_management(fc)

When I ran my code using the intersect method it took 7 minutes 20 seconds (the data sets were pretty large).

This new version with the delete afterward took only 50.71 seconds.

I hope this helps (someone...)

answered Dec 8, 2015 at 23:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.