3

My feature class contains two fields: an identifying field U_ID, and a descriptive text field DESC. This field currently has <Null> for all features. I would like to select all the features with the same ID and populate this field with sequential values (using code I have previously used here), only if there are <Null> values in DESC.

My model receives the feature class as input into a Feature Selection Iterator, grouping by U_ID. I then want to check for null values in the DESC field for those Selected Features. If a <Null> is found in DESC for any of the Selected Features, return true and populate the field with values. If there are values for all the features, the condition is false.

This is what the model looks like:

enter image description here

For example: the iterator selects U_ID = AA01. There are ten features in the feature class with that ID, so Selected Features contains those 10 features. The Calculate Value tool contains the following:

checkIfNull("%DESC%")

def checkIfNull(f):
  return "true" if not f else "false"

Seeing as DESC is <Null> for those ten features (or even just one of the features), the precondition is true, and Calculate Field DESC should be executed. However, the precondition returns false and the model ends. Changing the code to

return "true" if f is None else "false"

yields the same result. I don't think I'm accessing the values in DESC correctly, because my test for <Null> seems to be right, judging by the answers to other questions here.

5
  • These answers may help: gis.stackexchange.com/questions/13308/…
    – Rayner
    Sep 20, 2012 at 6:33
  • I had taken a look at that question before I posted mine, but I tried the code anyway. My added field simply remains blank, so it didn't pick up the null values in field1. Sep 20, 2012 at 7:17
  • Can you describe at a high level what you are trying to accomplish? Because all I see in the question are preconceived means to an undetermined end. It may be that another approach entirely is more appropriate.
    – blah238
    Sep 20, 2012 at 8:39
  • OK I will edit the question now. Sep 20, 2012 at 10:15
  • 1
    I am still a little hazy on the desired behavior, so maybe a few examples of the before and after states would help (be sure your examples capture all of the desired behavior, such as what you want to happen when some of the DESC values are non-null, but perhaps not the first -- as well as any other corner case you forsee being an issue). This logic may be a little too complex for ModelBuilder so you might end up doing some or all of it in Python.
    – blah238
    Sep 20, 2012 at 21:30

2 Answers 2

2

Using a Field Value Iterator in a submodel, I used the Selected Features as input and iterated over the ten values in the field. I had to set the Null value as Null:

enter image description here

and collect the values as a multivalue. In my main model, I then check that list and if it contains what I deemed as invalid values, it returns true.

isValid("%List%")

def isValid(l):
  try:
    map(int, l.split(";"))
    return "true"
  except:
    return "false"
1

As far as I can tell the logic and syntax of your function is correct although the function can be simplified to return the boolean values True and False and just perform a simple test of f is None:

def checkIfNull(f):
    return(f is None)

This returns True is the condition is met and False if it is not.

This suggests that the problem is in your iteration through the rows, or in the assignment of your Null values. In python there are 3 types of 'null' value, the builtin type None as used above, a blank string "" and a missing variable, which is not covered in this case. Depending on how the null values were assigned you may need to change None to "" in the function.

5
  • It definitely is not the blank string, but I have tried the code using all 3 types just in case, and used your return code above, and it still returns false. Sep 20, 2012 at 10:27
  • It looks like the problem is with the reading of the values in the attribute table then. Are the percentages around DESC wildcards? I don't know how the python interpreter will deal with them.
    – sgrieve
    Sep 20, 2012 at 10:52
  • %DESC is the inline variable used in the Calculate Value. It refers to the DESC variable in my model which contains the field name. Sep 20, 2012 at 10:55
  • In that case, %DESC% will always expand to the field name, not the current field value, and thus checkIfNull will always return false.
    – blah238
    Sep 20, 2012 at 21:33
  • So it seems that instead of checking the current value in the field for that feature, it's simply receiving the field name as a string so will always be false as the field is not null? Sep 21, 2012 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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