Select to view content in your preferred language

Merge Buffers After Creating/Calculating Field

1552
4
03-04-2019 08:03 AM
JaredKauffman1
Emerging Contributor

I am ultimately trying to merge my buffered layers together after I've created and calculated a field within each buffer.

My steps within my submodel are

1: Iterate Row Selection from the Bus Route table list, which produces a Value

2: Select by Attribute from the Bus Stop layer the Value that's currently being iterated

3: Buffer the Bus Stop selection, and dissolve into single feature

4: Add Field to the newly created Stop Buffer layer

5: Calculate as text the newly created field (expression is '%Value%')

6: Collect Values

My steps within my Main Model are

1: Run the submodel

2: Merge the collected values

Each time I run it, the field that was created in the buffer is not there. How do I include this newly created field in my Merge?

0 Kudos
4 Replies
curtvprice
MVP Esteemed Contributor

The issue here is that your field map for the merge is calculated at validate time, before any buffers or add fields happen in the submodel.

I can think of two things to try:

  1. Add  field to Dec18Stops (STOPVAL, Long [I think]) in the main model before you run the submodel, and calculate that field in the submodel at each iteration. Dec18Stops should also be set up as a parameter to the submodel do the validation will pass correctly. 
  2. Use a Calculate Value python function to create a field map in the main model, at runtime, passing it to the Merge. (This is a bit more complicated.)
JayJohnsonWashoeCounty
Frequent Contributor

Much like the initial poster, I had a model where a field kept disappearing.  Annoying and puzzling.  Your answer (field map for the merge is calculated at validate time) put me on the right track to fix this - thanks! #model#validate

Jay
Washoe County GIS
JaredKauffman1
Emerging Contributor

Thanks for the advice Curtis. Because I would like to make my process (which will include other submodels) as much of a one-button click as possible, I am more interested in the Calculate Value tool you mentioned. Unfortunately, I don't have any Python scripting experience. Is it possible to help explain what my script would look like? The field I'm adding to the buffer (in my submodel) is Text, because the express bus routes are denoted with an "X", which are also denoted the same way in the Dec2018 Excel file, as well as within the Dec2018Stops point feature class.

Thanks again!

0 Kudos
curtvprice
MVP Esteemed Contributor

I'm a little confused because I don't get why you are adding a field that is already there.

The python code you need to do to create a field map at runtime would use the FieldMappings object.

FieldMappings—Help | ArcGIS Desktop 

Here's a script for the Calculate Value tool. NOT tested.

# expression
fmap(r"%Output Values%")

# code block
import arcpy
def fmap(cvalues):
  cvals = cvalues.split(";")
  fms = arcpy.FieldMappings()
  for ds in cvals:
    fms.addTable(ds)
  return fms.exportToString() # Data type Field Map, connect to Merge tool‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Field Mappings variable as a parameter not working in 10.5.1