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.
1 Answer 1
- Connect the output of your iterator (AdmBorder) to the Input table parameter of the Calculate Field tool.
- Make the output of the Calculate Value tool a precondition to the Calculate Field tool.
- 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 isC:\Users\Vadim\Downloads\GML\esri\PetrovskySS.gdb\Adme
.
-
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– Vadim2021年06月22日 14:27:26 +00:00Commented Jun 22, 2021 at 14:27
-
You have edited your question with your new image but now my answer is referencing
PetrovskySS.gdb
nottest.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– Hornbydd2021年06月22日 15:21:09 +00:00Commented 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– Vadim2021年06月23日 06:36:15 +00:00Commented Jun 23, 2021 at 6:36
Explore related questions
See similar questions with these tags.
<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).