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))
2 Answers 2
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))
-
It runs but it still clips everything, regardless of if there is data in the fc area being clipped or not.TacoB0t– TacoB0t2015年05月05日 19:26:08 +00:00Commented 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:
crmackey– crmackey2015年05月05日 19:40:15 +00:00Commented 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?TacoB0t– TacoB0t2015年05月05日 19:54:49 +00:00Commented 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.CSB– CSB2015年12月08日 22:22:22 +00:00Commented Dec 8, 2015 at 22:22
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...)