How do I check if a layer already exists on the Map in ArcGIS Pro using ArcPy?

4444
7
04-24-2019 04:31 AM
by Anonymous User
Not applicable

I have a process that adds a layer from a lyrx file to a group layer on the map. I need to add something into my script to check to see if it already exists so it doesn't add it again but I'm struggling to find anything online about this for ArcGIS Pro. Here's what I currently have (sorry I'm new at this):

LyrDir = "C:\\mydir\\"
off_pk = "Offline group layer\\"
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("Map")[0]
offGrp = aprxMap.listLayers("Offline group layer")[0]

my_lyr = arcpy.mp.LayerFile(LyrDir + "myfile.lyrx")
 aprxMap.addLayerToGroup(offGrp,my_lyr,[2])
 offlyr = aprxMap.listLayers(off_pk + "my layer")
 for layer in offlyr:
    layer.visible = False

I was hoping it would be as simple as typing something like:

if layer.exists(mylayer) == False:
   aprxMap.addLayerToGroup(offGrp,my_lyr,[2])
   offlyr = aprxMap.listLayers(off_pk + "my layer")
   for layer in offlyr:
      layer.visible = False

 

If anyone can help, thanks in advance.

7 Replies
DanPatterson_Retired
MVP Emeritus

Exists—ArcPy Functions | ArcGIS Desktop 

arcpy.Exists(…) I would think would do it, but you will need to provide the path

by Anonymous User
Not applicable

Thanks for the help Dan. That gets me part of the way there. It returns 'True' if I check a layer exists outside of a group layer but when I try to find my layer 'my group\my layer' it always returns 'False' even though it exists. I'm assuming it requires a slightly different process if something is nested. Any ideas?

Thanks.

0 Kudos
DanPatterson_Retired
MVP Emeritus

sounds like you have to check to see if the layer is a group layer or check each layers 'longName' property to get the full path to it

longName
(Read Only)

A layer's full path including group layer folder structure.

Layer—ArcPy | ArcGIS Desktop 

by Anonymous User
Not applicable

Thanks that's helpful. Unfortunately I' struggling with implementing it. I can't find out to to specify a particular group. I have added the following:

for lyr in aprxMap.listLayers("my layer"):
 if lyr.longName:
 print(lyr.longName)

I've put print in the third line to see what response I get and it is as expected. The issue is that I have two group layers which have the layer in. I want to specify the offline layer as I'm essentially copying the data from online group layer to the offline one.

Thanks.

0 Kudos
DanPatterson_Retired
MVP Emeritus

sounds convoluted, so you seem to be wanting to parse the path as well to find the source group layer, so you can copy (or whatever) to a destination group layer.  So the layer name will remain the same, but the long name will differ

0 Kudos
by Anonymous User
Not applicable

Apologies, I'm possibly not explaining this very well.

I have an online version of the feature layer that comes from ArcGIS Online and sits in the group layer 'Online group layer'. I then have an offline version of this layer (a file gdb) which sits locally and exists as a layer in the group layer 'Offline group layer'.

The process previously copied the data from the online layer into the file gdb and worked without anything too complex having to happen however, since version 2.2 of ArcGIS Pro and later, because of a bug which is now known to Esri, the layer based on the gdb is dropped from the map when it's overwritten.

What I've had to do is add it back in using a lyrx file after the gdb has been updated. In some situations there is a possibility that the offline version of the layer is not dropped from the map and to ensure it isn't added again I want to do a check to tell it to only add the layer to the 'Offline group layer' on the map from the lyrx file if it doesn't already exist in the offline group layer.

All names are static so the map contents will always look roughly like this:

v Map

 v Offline group layer

   >  my layer

 v Online group layer

   > my layer

So the 'my layer' in 'offline group layer' comes from a gdb file/is inserted from a lyrx file and the 'my layer' in 'Online group layer' comes from ArcGIS Online

Thanks

0 Kudos
LeandraGordon
Occasional Contributor II

I think Dans answer checks for the Feature Class in the geodatabase. To check for the layer in the map use a for loop to iterate through the layers e.g.

currentProject = arcpy.mp.ArcGISProject("CURRENT")
currentMap = currentProject.listMaps()[0] #I think this assumes there is only one map
layers = currentMap.listLayers()

for layer in layers:

    if layer.name == "Property_LYR":
         #do something with the layer
         arcpy.management.ApplySymbologyFromLayer(PROPERTY_LYR, PROPERTY_LYRX)
   else:
        #do something else