select_by_survey_result_%Survey_Number%See the examples of inline variable substitution.
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?
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 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.
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?
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>
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>
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!