Remove and edit Layers from Mapframe

2804
4
Jump to solution
03-27-2023 06:12 AM
ammsgis
Emerging Contributor

 

 

aprx = arcpy._mp.ArcGISProject("CURRENT")
content = aprx.activeMap
lyr = content.removeLayer("lakes")
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 2003, in removeLayer
    return convertArcObjectToPythonObject(self._arc_object.removeLayer(*gp_fixargs((remove_layer,), True)))
ValueError: lakes

 

 

 

I have been trying to remove layers from the map frame through python code, but the above errors have returned.

My idea is put this code on Calculate Value and use that in one Model Builder I wanted to create. May this possible?

Thank you in advance.
Best Regards.

 

 

0 Kudos
1 Solution

Accepted Solutions
Mahdi_Ch
Regular Contributor

 

UPDATED: I think the issue is mp.removeLayer() only takes a reference to a Layer object, not layer name . Also I think you don't need to set content.removeLayer('lakes') to be equal to a variable again. See if that's the case. Also, to make sure that the layer exists, add some check point:

 

import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
mp= aprx.activeMap

# to make sure you are in the right map
print(mp.name) 

# all layer in the active map 
ly_ls = [i for i in mp.listLayers()]

# print all layer names and their index in the active map 
for c, n in enumerate(ly_ls):
    print(f'{c} - {n.name}')
# the index of the layer in the list. I put 1 as an exmple
layer2remove = ly_ls[1]

# if else helps catching the error if you pass the layer manually. here should't help much but included it if you want to copy for other places.
if layer2remove in ly_ls:
    mp.removeLayer(layer2remove)
else:
    print('Layer not in the map!')
    

 

 

 

View solution in original post

4 Replies
Mahdi_Ch
Regular Contributor

 

UPDATED: I think the issue is mp.removeLayer() only takes a reference to a Layer object, not layer name . Also I think you don't need to set content.removeLayer('lakes') to be equal to a variable again. See if that's the case. Also, to make sure that the layer exists, add some check point:

 

import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
mp= aprx.activeMap

# to make sure you are in the right map
print(mp.name) 

# all layer in the active map 
ly_ls = [i for i in mp.listLayers()]

# print all layer names and their index in the active map 
for c, n in enumerate(ly_ls):
    print(f'{c} - {n.name}')
# the index of the layer in the list. I put 1 as an exmple
layer2remove = ly_ls[1]

# if else helps catching the error if you pass the layer manually. here should't help much but included it if you want to copy for other places.
if layer2remove in ly_ls:
    mp.removeLayer(layer2remove)
else:
    print('Layer not in the map!')
    

 

 

 

ammsgis
Emerging Contributor

Ohh man!! It worked ! Thank you so much @Mahdi_Ch!! That was what I wanted!
But I have some questions,
1- Can you explain me why i can't managed to use the layers's name to remove them? 

2- In this code snippet below , what is c and n ?

ly_ls = [i for i in mp.listLayers()]
for c, n in enumerate(ly_ls):
    print(f'{c} - {n.name}')

 

Thank you again for all help !

 

0 Kudos
Mahdi_Ch
Regular Contributor

Great! Happy that it helped. 

Yeah, at first, I was not sure what the issue was, then after writing the code I realized it, but forgot to edit my initial text. My apologies for the confusion.  

1) Basically the issue is mp.removeLayer() only takes a reference to a Layer object, not layer name. That's the way it was defined in Arcpy and I believe that was the root of the issue here.

2) As you probably know,, "for loop" goes into the list (or other iterable objects) and takes the elements out one by one and returns them to you. Sometimes the order of the elements in that list matters to you. In that case you can set a counter manually to know about the position of an item in the list. Alternatively, Python has a built-in function called enumerate that helps with that.
Enumerate takes the list, loops over it and returns two items at a time, the first item is the index of the element in that list (starting from 0) and the second one is the actual element. Also c, n can be called  whatever you like. 

Hope it make it more clear for you.

P.S. I will edit/update my initial response to be more clear just in case other people land here with the same issue later. 

TinaSlunt
New Contributor

Thank you so much for the above code.  Is there a way to delete multiple at a time?

0 Kudos