ModelBuilder use of Variable in Output Filename possible?

18275
9
11-22-2012 10:58 AM
TysonSchoepflin
New Contributor II
Hello,

I've setup a model with a variable named Survey_Number, it is set as a parameter.  I use this variable in a Select Layer by Attribute function.  I then use Make Feature Layer and check it to Add to Display.  I have a number of surveys I want query and I was hoping I could use the variable in Output Layer name of the Make Feature Layer function.  Something like this:

Output Layer
select_by_survey_result_Survey_Number

Can you use variables to create dynamic layer names or output file names in this manner?  Or do I need to just set the Output Layer as a parameter as well and manually change the name?
Tags (2)
0 Kudos
9 Replies
DarrenWiens2
MVP Honored Contributor
If you've got a variable called Survey_Number, you can use it in your output filename by naming it:
select_by_survey_result_%Survey_Number%
 
See the examples of inline variable substitution.
0 Kudos
TysonSchoepflin
New Contributor II
If you've got a variable called Survey_Number, you can use it in your output filename by naming it:
select_by_survey_result_%Survey_Number%
 
See the examples of inline variable substitution.


This doesn't work though. I've tried it a few times.  I get the warning message select_by_survey_result_%Survey_Number% already exists.  Regardless of the variable number.  I also have geoprocessing options set to Overwrite the outputs of geoprocessing operations.

Also, it doesn't add the layer to the map.  If I manually enter select_by_survey_result_1 for the Output Layer it works and adds it to the map.  It appears the tool is treating %Survey_Number% as a string and not a variable.

The variable %Survey_Number% DOES work in the sql statement.  But it doesn't work in the Output Layer text field of the Make Feature Layer tool.  Is there some other method to add the variable to the text field?
0 Kudos
MarcoBoeringa
MVP Regular Contributor
This doesn't work though. I've tried it a few times.  I get the warning message select_by_survey_result_%Survey_Number% already exists.  Regardless of the variable number.  I also have geoprocessing options set to Overwrite the outputs of geoprocessing operations.

Also, it doesn't add the layer to the map.  If I manually enter select_by_survey_result_1 for the Output Layer it works and adds it to the map. It appears the tool is treating %Survey_Number% as a string and not a variable.

The variable %Survey_Number% DOES work in the sql statement.  But it doesn't work in the Output Layer text field of the Make Feature Layer tool.  Is there some other method to add the variable to the text field?


I think you've hit the culprit in the bold text. Yes, in many instances, the variable is treated / passed / must be a string, and you will need to account for this in your code to make it work properly. E.g. convert numbers to strings or the other way around before using them in some parts of your code, like for this case with a filename.

I have used string variables successfully in tools of multiple models.
0 Kudos
TysonSchoepflin
New Contributor II
I think you've hit the culprit in the bold text. Yes, in many instances, the variable is treated / passed / must be a string, and you will need to account for this in your code to make it work properly. E.g. convert numbers to strings or the other way around before using them in some parts of your code, like for this case with a filename.

I have used string variables successfully in tools of multiple models.


I tired adding ' and " but neither work. I have a feeling this is not possible to have a variable in the Output Layer text field.  From other threads on this topic people seem to be using Calculate Value or creating their own script for the modelbuilder.  At this point I'm just going to run the queries by hand as I don't have time to pursue writing my own script at this point in time.  It would be great if they would support variables in all text fields of the tools.

ArcGIS 10 Model Builder In-line variable substitution for input data path
http://gis.stackexchange.com/questions/14149/arcgis-10-model-builder-in-line-variable-substitution-f...

Integrating scripts within a model
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Integrating_scripts_within_a_model/002...
0 Kudos
LauraBecht
Occasional Contributor

I created a test model that selects from a layer in ArcMap and makes a feature layer.  I made the number variable as a multivalue string that I iterated over.  When I ran the model, it selected each - in this case building - number and showed it in the existing feature layer.  Only problem is it wouldn't create a new feature layer in the table of contents for some reason.  It had no problem using the variable in the new layer name, just wouldn't display the new layer.

0 Kudos
curtvprice
MVP Esteemed Contributor

It had no problem using the variable in the new layer name, just wouldn't display the new layer.

Have you set add to display on the output feature layer model element? If running as a tool, have you set the model element as an output parameter so it will be added to the map?

0 Kudos
LauraBecht
Occasional Contributor

Yes to both, and neither have made any difference. I have written a python script that does the same thing using arcpy.mapping and am going to test that today. I saw a comment elsewhere that you can use calculate value and arcpy.mapping to add a layer to the TOC, but I wasn’t able to get that to work. Either I couldn’t get the syntax quite right, or you just can’t do it using calculate value. This kinda became a puzzle to solve for me. I have spent way too much time playing with this lol.

I am currently using 10.3 if that makes any difference.

Laura Becht, CFE

GIS Analyst – Geoprocessing

Representing Rick Singh, CFA | Orange County Property Appraiser

200 S. Orange Ave | Suite 1700 | Orlando, FL 32801

407.836.5908 work | 407.836.5985 fax

lbecht@ocpafl.org<mailto:lbecht@ocpafl.org> | www.ocpafl.org<http://www.ocpafl.org>

<http://www.ocpafl.org/>

<https://www.facebook.com/ocpafl>[cid:twitter_6cdbc742-1f4d-4e8a-9574-da593d6220f1.png]<https://twitter.com/ricksinghocpa>[cid:youtube_8082825c-0b33-4ebb-80c3-d490b789ebca.png]<https://www.youtube.com/user/OCPAFL>[cid:instagram_eddac590-fdb8-4599-aa07-0677c65b9ad6.png]<https://instagram.com/ricksinghocpa>[cid:Flickr_9a626e46-c468-4d00-a657-dd7094e9b498.jpg]<https://www.flickr.com/photos/151885696@N03/albums>

0 Kudos
LauraBecht
Occasional Contributor

I ran my model with the add layer script and got the same error I was getting with calculate value

Executing (Add Layer): AddLayer Adara_World_Gateway_BLDG_NO_1000

Start Time: Wed Apr 17 09:35:52 2019

Running script AddLayer...

Failed script AddLayer...

Traceback (most recent call last):

File "D:\Scripts\Add_Layer.py", line 5, in

<http://www.ocpafl.org/>

<https://www.facebook.com/ocpafl>[cid:twitter_6cdbc742-1f4d-4e8a-9574-da593d6220f1.png]<https://twitter.com/ricksinghocpa>[cid:youtube_8082825c-0b33-4ebb-80c3-d490b789ebca.png]<https://www.youtube.com/user/OCPAFL>[cid:instagram_eddac590-fdb8-4599-aa07-0677c65b9ad6.png]<https://instagram.com/ricksinghocpa>[cid:Flickr_9a626e46-c468-4d00-a657-dd7094e9b498.jpg]<https://www.flickr.com/photos/151885696@N03/albums>

0 Kudos
CSR_GarinHirata
New Contributor II

Add Feature Layer to TOC using Calculate Value

Try using the Calculate Value tool and insert this expression below, but using your layer file name from the output of the Select Layer by Attribute tool. 

arcpy.mapping.AddLayer(arcpy.mapping.ListDataFrames(arcpy.mapping.MapDocument("CURRENT"))[0],arcpy.mapping.Layer("%NameFormat%_Layer" ),"TOP")

1. name variable

2. Make Feature Layer tool (maybe you don't need this one, but for my model i do)

3. Select Layer By Attribute tool 

4. Calculate Value tool.

Maybe you might get a bit closer to what you are trying to do....For myself i'm having issues with trying to do this in ArcPro since things have changed. 

Good Luck!

0 Kudos