How to test if a Layer exists in a map in Model Builder

1180
3
12-16-2020 06:05 PM
LeandraGordon
Occasional Contributor II

I am attempting to build a model to test whether a layer exists in the current map, and if true, run the command ApplySymbologyFromLayer on that particular layer as follows:

aaaCapture.PNG

The 'If Data Exists' doesn't seem to be the right tool to test whether a layer exists and the tool runs with an error:

Failed to execute. Parameters are not valid.
ERROR 000735: Input Values: Value is required
Failed to execute (13UGBLYR).


What would be the correct tool to use to check whether a layer exists in the current map?

Tags (2)
0 Kudos
3 Replies
AndrewStickney1
New Contributor III

Hi Leandra,

Answer Summary

You can either do this by developing a custom script tool that checks the layer for you and then returns an output that you can evaluate....or by using the Calculate Value tool with a custom Python function and then check the output value of that tool with the If Value Is tool to create the split branch you had in your model above:

 

AndrewStickney1_0-1608227335385.png

Configuring the Calculate Values tool

Here's how to configure the Calculate Values tool.

Expression Field: 

 

CheckLayer("%Test Layer%")

 

Note that the "Test layer" is referring to my layer variable in my model that I am checking for existence. You would need to change in your model to:

 

CheckLayer("%UGB_Layer%")

 

Code Block field:

 

def CheckLayer(layer):
	try:
		import arcpy
		# current ArcGIS Pro project
		aprx = arcpy.mp.ArcGISProject("CURRENT")
		# current  open map
		map = aprx.activeMap 
		
		#check to see if the layer is listed in map
		if layer in map.listLayers(layer):
			return "PRESENT"
		else:
			return "NOTPRESENT"
	except:
		return "NOTPRESENT"

 

Make sure to set the Data type to String

 

Configuring the If Value Is tool

Set the output of the Calculate Value to be the input Value to check.

Keep the default value for the Value Test "Is equal to at least one value"

Set the Comparison Type to "Case Insensitive string"

Values - you only need to type in PRESENT. This is important because you only want to test for the "True" branch condition. Any other value (i.e. NOTPRESENT) would trigger the false branch.

AndrewStickney1_1-1608228435465.png

 

This should do what you need.

LeandraGordon
Occasional Contributor II

Thanks Andrew, I'll give that a go. I'm trying to get around a Python bug with Apply Symbology so it would either be run the model in the Python Script or run some Python in the Model 😄

0 Kudos
LeandraGordon
Occasional Contributor II

In the end I used the iterate Layers and set the wildcard to the layer name itself.

LeandraGordon_0-1624608753806.png

 

Slightly clumsy but it will suffice until the Apply Symbology from Layer bug is fixed in Python Toolboxes (and Jupyter Python as well!)

<rant>As an aside it seems that anything I try and do in Python to manipulate layers (moving, showing/hiding popups, changing symbology) just doesn't work in scripts so I'm going to try to do all that in model building becuase at least it seems to do 'what it says on the box'</rant>

0 Kudos