1

I need to calculate certain field in every feature class within a file geodatabase in case this field exists. If it doesn't, I want iterator to skip appropriate feature class. I'm building a model, but cannot get it work. enter image description here

I know, that in ArcGIS Pro one can use "If field exists" model tool inside the builder. But I'm working in ArcGIS 10.3 (standard licence). And in my case the solution would be "Calculate value" tool (as long as I'm not good in Python). The code is:

#Expression:

hasField(r"<path>.gdb\%Name%")

#CodeBlock:

import arcpy,os
def hasField(fc):
  arcpy.env.workspace = os.path.dirname(fc)
  fieldList = [field.name for field in arcpy.ListFields(fc)]
  if "GLOBALID" in fieldList:
    b = False
  else:
    b = True
  return b

"GLOBALID" is a field name. I get errors, running code.

ERROR 000539: Error running expression: hasField(r".gdb\AdmBorder") Traceback (most recent call last): File "", line 1, in File "", line 4, in hasField File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy_init_.py", line 1131, in ListFields return gp.listFields(dataset, wild_card, field_type) File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\geoprocessing_base.py", line 344, in listFields self._gp.ListFields(*gp_fixargs(args, True))) IOError: ".gdb\AdmBorder" does not exist

So I cannot get "True" and "False" branches, to continue the model. The code for calculation ("Calculate field" tool): #Expression:

ID()

#CodeBlock:

def ID():
  import uuid
  return '{' + str(uuid.uuid4()) + '}'

When I changed the expression in the "Expression" window of the "Calculate Value" block to the destination of the first (in a row) iterated feature dataset: hasField(r"C:\Users\Vadim\Downloads\GML\esri\PetrovskySS.gdb\Adme\%Name%") And after running model got the error ""The process did not execute because the precondition is false" and "A column was specified that does not exist. Failed to execute (Calculate Field)". How should I change connections between blocks in the model and how to modify the expression in the "Calculate Value" block? All the featureclasses are in the feature datasets.

2
  • Please Edit the question to specify the exact errors you receive. Note that Python is case-sensitive in string comparison, so you really ought to use `.upper()`` in the fold name construction comprehension.
    – Vince
    Jun 18, 2021 at 12:23
  • <path> is mapping to an empty string. You can focus the Question on this, because the ID assignment code is irrelevant (though best practice in Python is to not use leading uppercase or all-caps in function names).
    – Vince
    Jun 18, 2021 at 13:07

1 Answer 1

1
  1. Connect the output of your iterator (AdmBorder) to the Input table parameter of the Calculate Field tool.
  2. Make the output of the Calculate Value tool a precondition to the Calculate Field tool.
  3. The expression in your calculate Value tool should be hasField(r"C:\Users\Vadim\Downloads\GML\esri\PetrovskySS.gdb\Adme\%Name%") assuming the input workspace to the iterator is C:\Users\Vadim\Downloads\GML\esri\PetrovskySS.gdb\Adme.
3
  • Hornbydd, thank you for reply! Actually I have many datasets to iterate through within my geodatabase. So the mentioned one (Adme) is not the only one - it is the first one in a row. How should I change the expression then?
    – Vadim
    Jun 22, 2021 at 14:27
  • You have edited your question with your new image but now my answer is referencing PetrovskySS.gdb not test.gdb. So people now reading this Q&A will not understand my answer! Also you have removed that important image that shows the data structure so Vinces comments are now obsolete. I suggest you roll back your edits then add additional information like your new screen shot of your model. Don't replace. Once you have done this I may be able to help you further.
    – Hornbydd
    Jun 22, 2021 at 15:21
  • Ok, @Hornbydd, I've just rolled it back. I made chanchages according to your suggestion. ![Valid XHTML] (ibb.co/QXXmTg6) I've got messages like "The process did not execute because the precondition is false" and "ERROR 999999: Error executing function. A column was specified that does not exist. A column was specified that does not exist." Failed to execute (Calculate Field)" . ![Valid XHTML] (ibb.co/jgptfPd) And it iterates only through one feature dataset, but I need this to be done for the others as well.
    – Vadim
    Jun 23, 2021 at 6:36

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.