Select to view content in your preferred language

makeFeatureLayer() is removing layer of same name and source from active map while trying to assemble new map

318
4
03-15-2025 09:22 AM
Glasnoct
Regular Contributor

I am making a new map via code that shares similarities with the map I currently have open. I'm trying to generate Layer objects without automatically adding them to the new map as I want to modify them before finally adding them to a group layer. When I create a new layer object that has the same data path and name as a layer on my original map, the layer on the original map disappears for some reason. Why is my original map being modified when I don't seem to be interacting with it during layer creation? I don't want to copy the original layer at all, I want to create a new layer that just happens to have the same source and name.

 

p = arcpy.mp.ArcGISProject('current')
mp = p.createMap('test map')
arcpy.env.addOutputsToMap = False
# checking that the original layer is still there. Throws no exception.
original_map = p.listMaps('original map')[0]
original_layer = original_map.listLayers('layer name')[0]
# ----------
new_layer = arcpy.management.MakeFeatureLayer('url_of_feature_service', 'layer name').getOutput(0)
# checking that the original layer is still there again. Throws exception; can't find layer anymore
original_map = p.listMaps('original map')[0]
original_layer = original_map.listLayers('layer name')[0]
# ----------
mp.addLayerToGroup(mp.listLayers('group layer')[0], new_layer)

 

 

as an addendum, I'm using MakeFeatureLayer() as it was the answer i found via Google on how to create a Layer object from a portal data source without having to simultaneously add it to the map. The documentation doesn't suggest this should actually work as the arg description says it requires a Layer or LayerFile, not a string. Maybe this is the source of the issue? Is it somehow using the provided URL to return the first layer it finds with that data source?

 

0 Kudos
4 Replies
AlfredBaldenweck
MVP Regular Contributor

What version are you in? I'm unable to replicate in 3.3

I did run into the issue on your final line since you didn't make a group layer yet, but other than that, my maps were identical (the basemap is kind of odd, though? not sure what that's about-- one has a hillshade and the other doesn't, even though it's the same basemap).

 

I also attempted to use mp.addDataFromPath() instead but that did not obey addOutputsToMap = False for some reason.

That's because addDataFromPath() is explicitly telling it to add the output to the map. That environment setting is for things like setting up a long chain of geoprocessing with several intermediate files where you only need the final one.

 

The other question I have is: if the maps are very similar, why not just cycle through the original map and copy the layers that you need directly?

move_lay = original_map.listLayers()[0]
mp.addLayer(move_lay)
Glasnoct
Regular Contributor

I'm on 3.2 currently. 

Copying would certainly be an option for the layers already present on the original map, though I'd rather generate a new layer object instead just for the consistency of generating layers independent of their existence on any existing map.

0 Kudos
RhettZufelt
MVP Notable Contributor

You say the map you currently have open, so I'm assuming you are running this from inside Pro?

If so, perhaps it's the geoprocessing options set in Options page:

RhettZufelt_0-1742241662748.png

You might try with variations of the above settings.  The setting I have here will overwrite the layer if one with the same name is in the active map. 

R_

Glasnoct
Regular Contributor

Playing with these options seems to have fixed it for me momentarily but it's still removing layers for some reason now and I haven't sussed out where some setting is getting reset that layer object creation is removing the existing layer again.  My other scripts *want* to overwrite an existing layer on the active map which I'm ok with, so I don't want to toggle it off. 

 

I guess what confuses me is why is the existing layer being touched at all? I'm not passing it as a reference when creating the new feature layer and I'm creating the new feature layer with a different name. The answers in this thread asking a similar question  say I can just pass the URL instead of a layer object so I'm lead to believe Pro is somehow using the URL to grab the first related layer on the active map and overwriting it prior to adding it to the new map (as the error returned from the code would suggest when I turn off "allow geoprocessing tools to overwrite."

Is there not a way to make a new layer object that references the same data source *without* having to add it to the map first or without touching any layers that share that source? Seems odd.

0 Kudos