Iteration and Inline Variables

409
10
04-05-2012 07:51 PM
NoahHuntington
Occasional Contributor
I am needing help determining syntax of inline variable to use string from field name instead of 1..2...3..

Similar to:

[ATTACH=CONFIG]13287[/ATTACH]

Where n in Test%n% becomes selected field name during each selection.

Thanks in advance.
0 Kudos
10 Replies
curtvprice
MVP Esteemed Contributor
I am needing help determining syntax of inline variable to use string from field name instead of 1..2...3..


Your question is unclear to me. Have you tried looking at %Value%, which has the value for each iteration?
0 Kudos
NoahHuntington
Occasional Contributor
Your question is unclear to me. Have you tried looking at %Value%, which has the value for each iteration?


Thanks for the response.  To clarify, I am trying to iterate through feature class using OID, but would like to return Name field in output.  sp. Clip%Name%
is this possible?
0 Kudos
curtvprice
MVP Esteemed Contributor
Thanks for the response.  To clarify, I am trying to iterate through feature class using OID, but would like to return Name field in output.  sp. Clip%Name%
is this possible?


In ModelBuilder 10.x you can access this value from the feature layer output for the iterator using the Get Field Value tool. The output of that tool will be the value (say, %Name%) you can use in the output file name.

Another approach I've used is include the OID and name field in the iterator - then you can parse the value out of the iterator label element using the Calculate Value tool. For example on an iteration the iterator label element may be "1_Foo", you can then use Calculate Value to get the value using this expression:

r"%Value%".split("_")[1]

the above expression returns "Foo" if Value is "1_Foo".
0 Kudos
WadeGivens
New Contributor II
In ModelBuilder 10.x you can access this value from the feature layer output for the iterator using the Get Field Value tool. The output of that tool will be the value (say, %Name%) you can use in the output file name.

Another approach I've used is include the OID and name field in the iterator - then you can parse the value out of the iterator label element using the Calculate Value tool. For example on an iteration the iterator label element may be "1_Foo", you can then use Calculate Value to get the value using this expression:

r"%Value%".split("_")[1]

the above expression returns "Foo" if Value is "1_Foo".


Curtis:

Can you point me to some documentation, or examples of the expression syntax similar to that above.  I'm wanting to do something similar but strip off the last 4 characters of %Value%.  So if %Value% = raster.tif, I want to use %Value% plus an expression to get raster.shp

Thanks!
Wade
0 Kudos
curtvprice
MVP Esteemed Contributor
Can you point me to some documentation, or examples of the expression syntax similar to that above.  I'm wanting to do something similar but strip off the last 4 characters of %Value%.  So if %Value% = raster.tif, I want to use %Value% plus an expression to get raster.shp


You need to get familiar with the basic Python syntax for handling strings. I'd start with the Python tutorial. (All the basic Python string handling is early in the tutorial.)  Esri has some nice python training on the training.esri.com, free and not-free.

And, for use in ModelBuilder, the Calculate Value tool reference.

There is a great series on the ArcGIS blog on taking full advantage of ModelBuilder's capabilities here:
ArcGIS Blog: If you are stuck at "if" (3 parts)

Now, to your issue:

The safest way to strip off a file extension is to use the Python os module's os.path.splitext:

>>> import os
>>> os.path.splitext("e:\\work\\image.tif")[0]
'e:\\work\\image'


Using Calculate Value - you need a code block to use the os module:

Expression (mind the use of "r", quotes and the "%" variable delimeter)
f(r"%path_element%")

Code Block:
def f(path):
  import os
  return os.path.splitext(path)[0]
0 Kudos
WadeGivens
New Contributor II
Thanks for the references!  Will start digging into them.

In the interest of making it a bit more universal for future use, would the following remove the last 4 characters from a string stored in %Value% ?

Using Calculate Field

Expression:

f(r"%Value%")


Code Block:

def f(Value):
  import os
  return Value[-4]


Data type set as String
0 Kudos
curtvprice
MVP Esteemed Contributor
In the interest of making it a bit more universal for future use, would the following remove the last 4 characters from a string stored in %Value% ?


Almost got it -- refer to the examples of slicing strings in that Python tutorial I referenced.


(BTW Calculate Value is the tool you want, not Calculate Field, which calculates fields in tables, not variables in ModelBuilder.)

If you're just doing string manipulation you don't need the os module, so you can just snip off the last four characters of a path in the expression itself using built-in Python string slicing syntax and leave the code block empty:

r"%Value%"[:-4]


One of the really nice things about the python command line (including the one in ArcMap) is you can test these things easily before you put them in your model:

>>> "file.tif"[4]
'.'
>>> "file.tif"[:-4]
'file'
>>> "file.tif"[-4:]
'.tif'
>>>
0 Kudos
WadeGivens
New Contributor II
You were right, meant Calculate values :)

The reference for the string slicing is a good nugget to have.  I Can never keep straight of when to use the colon.

I'm attaching a screenshot of the part of the model where I'm trying to implement the Calculate Value.  I specified a .tif image and parse the the path to get the file name (%Value%) to carry off for further processing.  In the Calculate Base Fiel Name I put the following code in the Expression line only (expression.png):

r"%Value%"[:-8]


[ATTACH=CONFIG]13606[/ATTACH]

The output value for basename is in the screenshot (model_snippet.png).  I'm expecting the value to be "dinsdale_b1"

Should the line of code be encapsulated with f() ?

[ATTACH=CONFIG]13605[/ATTACH]
0 Kudos
curtvprice
MVP Esteemed Contributor
Should the line of code be encapsulated with f() ?

No, the f() is how you call a function in the code block (if you need one).

You need to run the tool to get the result, and you need to run them in order (for example, Value need to be set before you run your Calculate Value tool that uses it).
0 Kudos