Select to view content in your preferred language

Creating Dynamic Field Maps for Tools in Model Builder

230
2
3 weeks ago
LindsayRaabe_FPCWA
MVP Regular Contributor
2 2 230

I've been working on a problem lately, and discovered that there was no out-of-the-box way to achieve this. In short, I have 2 feature classes going into a Spatial Join, but the fields in those feature classes could change each time the model is run. The issue here is that the Field Map in the Spatial Join tool is static, and can only be updated by editing the model, running up to the Spatial Join, opening the Spatial Join parameters and resetting the Field Map to include additional fields, or remove those no longer present in the input datasets. 

This is obviously not ideal  - Model Builder should be able to deal with this. Enter python script tools! 

With a bit of help from a certain AI that everyone seems to know of, I quickly had a python script that when loaded into a python script tool, would take the Target feature class and Join feature class (or as many input datasets as required), iterates through the fields in each and adds them to a Field Mappings object. This Field Mappings object is then passed as an output of the script tool and as an input Field Mappings parameter to the Spatial Join. This then dynamically updates the Field Map of the Spatial Join every time the model is run. 

Configuring the parameters of the python script to accept the multiple input datasets and output Field Mappings object, this tool was ready to be added to my model. In short, I now have a reusable tool that when added to any model, will generate a new Field Map for any GP tool that requires it (e.g. Export Features)

 

import arcpy

# Get semicolon-delimited input feature layers
input_features = arcpy.GetParameterAsText(0)

# Split into a list of layers
layers = input_features.split(";")

# Create FieldMappings object
field_mappings = arcpy.FieldMappings()

# Add fields from all input layers
for layer in layers:
    fields = arcpy.ListFields(layer)
    for field in fields:
        if field.type not in ['Geometry', 'OID']:  # Skip geometry and object ID fields
            field_map = arcpy.FieldMap()
            field_map.addInputField(layer, field.name)
            field_mappings.addFieldMap(field_map)

# Set output parameter (derived)
arcpy.SetParameter(1, field_mappings)

LindsayRaabe_FPCWA_0-1754965746729.png

LindsayRaabe_FPCWA_1-1754965792181.png

This approach could be used to work with any tool that requires a Field Map (e.g. Export Features).

Next time you have a problem with tools in Model Builder not quite doing what you need, fire up ChatGPT, Copilot or another AI helper and ask it how to make your process happen. Check the code it provides thoroughly because they do make mistakes, but it's a great way to get into the coding space when you just don't quite know where to start. 

***Update*** - since originally posting this, I have simplified it as I had made things more complex that necessary. Also another risk with the AI helpers. They don't always point out that there may be easier ways of achieving a problem as they tend to focus on the problem through the narrow lens of the prompts you entered. Always ask if a solution can be streamlined or done with fewer steps. 

2 Comments
AlfredBaldenweck
MVP Regular Contributor

Cool work, but couldn't you have just made the spatial join's field map a parameter of the model?

LindsayRaabe_FPCWA
MVP Regular Contributor

@AlfredBaldenweck Yes! You're right! It could be made even simpler and more versatile. I'll update the original post to reflect this. This is in fact what I had hoped to achieve in the first instance. Thanks for the prod. 

Contributors
About the Author
After leaving TAFE as a Mining & Engineering Surveyor, I got my first real taste of GIS at the Forest Products Commission. After a year, an economic downturn resulted in me getting a job doing FIFO at a BHP mine site as a Mining Surveyor. 3.5 years of that was enough and a spot opened up at the FPC again and I've been there ever since (10+ years now). I enjoy my work managing and developing FPC's GIS, and being a part of a great team where we create tools and workflows that make the lives of our office based and field staff easier. Using ArcGIS Online, ArcGIS Pro, Field Maps, Survey123, GeoMaster and ArcMap, we aim to improve data flows to and from the field by improving the way data is collected, displayed, used and shared within the organisation and with our peers in government and private enterprise.