Model builder get count and delete

7364
7
03-13-2013 01:16 PM
leewinslow1
New Contributor II
Hello,

I've built a model that should work as far as I can tell.  I'm using the get count to check a shape file for entries. Then using the calculate value to serve as a precondition to the delete tool.  The calculate value tool takes in the Row Count and puts out a false if there are values and a true if there are zero values.  This breaks the iteration before the delete tool if values exist  Essentially I'm iterating through a directory and want to delete any shape files that have 0 features in them.  When I run the model features with 0 rows are still created and in the dialogue box it says all the inputs are not current when it runs over a feature that's supposed to be deleted.

My calculate value code is

expression

fn("%Row Count%")

block

def fn(rowcount):
    if rowcount == 0:
       return "true"
    else:
       return "false"

data type

boolean


What am I missing?

Thanks
0 Kudos
7 Replies
leewinslow1
New Contributor II
Edit--

i figured out the not all inputs are current error.  setting the get count as pre condition to calculate vale tool caused the error.  When a feature with that returned a zero count was run the get count puts out a zero which causes the pre condition to fail before it ever reached the calculate value or delete tool.  I've got that changed  now but my calculate value is still returning a false value when zero features are present. This means the shapfile never gets deleted.

Thanks
0 Kudos
curtvprice
MVP Esteemed Contributor

Yes, this will get you!

The trick here is to do the GetCount inside the calculate value. A little Python goes a long way in ModelBuilder!

"Output feature class" is the model element for your shapefile that you want to delete if it is empty.

You should have Output feature class be precondition for this Calculate Value tool so it will run at the right time.

expression:

DeleteIfZeroCount(r"%Output feature class%")


code block:

def DeleteIfZeroCount(inFC):
  import arcpy
  if int(arcpy.GetCount_management(inFC).getOutput(0)) == 0:
    arcpy.Delete_management(inFC)
    return True
  else:
    return False
0 Kudos
leewinslow1
New Contributor II
Hello!

Running into an error now.  Getting error code 00539 error running expression failed to execute parameters are not valid.
followed by error 000732 input rows for my "target shapfile"  does not exist or is not supported failed to execute (getcount)

thanks
0 Kudos
curtvprice
MVP Esteemed Contributor

followed by error 000732 input rows for my "target shapfile"  does not exist or is not supported failed to execute (getcount)


It's very important that you use this syntax to pass dataset model elements to Calculate Value so the pathname will get through to Python correctly:

r + " + % + elementname + % + "

for example:

r"%Output shapefile%"

It's also very important that you spell the element name correctly, otherwise the variable isn't recognized and passed on to the code block; and the function then will see something like "%Target Shapfile%". This is so easy to do that my usual method to do this is to a) right click the model element, b) pick Rename, c) copy, cancel, d) open the Calculate Value tool and paste the name between the %%'s in the expression field.
0 Kudos
leewinslow1
New Contributor II
Hi Curtis

I ended up having someone explain some python to me and we weren't able to get the calculate value tool to work.  Instead we created a python script doing the same thing and added that to the model.  Now it works like a charm.

Python script was

import arcpy

invalue = arcpy.Getparameter (0)

if inValue == 0:
  outValue = True
Else:
  out Value = False

Arcpy.SetParameter(1, outValue)


Thanks for the help though
0 Kudos
AndreasElmelund_Hass1
New Contributor

Hi Lee

I know it's been a long time since you posted this question and solution, but do you mind sending a picture of your model? -If you still have it?

I am having the same problem as you where I am not able to delete empty feature classes created by my model. I've tried many things and contacting you is sort of my last resort.

Kind regards

Andreas

0 Kudos
curtvprice
MVP Esteemed Contributor

I think the easiest way may be to do the delete inside the Calculate Value tool. This is up-thread, but here's another shot at it.

As I said above it's very important that you use the "r" and quotes and percent signs as shown - and that the name matches the name of the model element exactly.

Expression:

DeleteIfEmpty(r"%Shapefile output%")

Code block:

import arcpy
def DeleteIfEmpty(ds):
  fcount =  int(arcpy.GetCount_management(ds).getOutput(0))
  if fcount == 0:
    arcpy.Delete_management(ds)
0 Kudos