Select to view content in your preferred language

How to reorder/add/remove layers APRX map programmatically?

03-04-2020 04:08 PM
Regular Contributor

Trying to take a subset of available layers in the map of an ArcGIS Pro project (aprx) and make sure they are in the right order. (I'm trying to dump the arranged layers into a PDF). 

Originally I started out just hiding (setting layer.visible = False) for any layers I don't want. But while there were no errors, it didn't yield the expected results. The resulting layer order in the aprx is not the same as in Pro.

So I thought about removing all layers and adding back in those that I need. While I can do this:

while map.listLayers():
    map.removeLayer(map.listLayers()[0])‍‍ get rid of all layers, I can't do something more refined like:

layerNames = ['layer 1', 'layer 2', 'layer 3' .... ]

for layerName in layerNames:

That blows up even though my layer names...

for layerName in layerNames:
----> 6     map.removeLayer(map.listLayers(layerName)[0])
IndexError: list index out of range‍‍‍‍‍‍‍‍

... are all included in the list that is ...

[ for l in map.listLayers()]

So I can't figure out what's going on. I've tried individual layers and have found a few that worked. But I can't tell what's different about them. Depending on what type of layer it is, it's also hard to compare values for all the attributes..

'brightness', 'connectionProperties', 'contrast', 
'dataSource', 'definitionQuery', 'extrusion', 
'getSelectionSet', 'is3DLayer', 'isBasemapLayer', 
'isBroken', 'isFeatureLayer', 'isGroupLayer', 
'isNetworkAnalystLayer', 'isNetworkDatasetLayer',
 'isRasterLayer', 'isWebLayer', 'listLabelClasses', 
'listLayers', 'longName', 'maxThreshold', 'minThreshold',
 'name', 'saveACopy', 'setSelectionSet', 'showLabels', 
'supports', 'symbology', 'transparency', 
'updateConnectionProperties', 'updateLayerFromJSON', 'visible'‍‍‍‍‍‍‍‍‍‍‍‍

So if I go the route of removing all layers...

map = aprx.listMaps()[0]
layers= [l for l in map.listLayers()]

while map.listLayers():

and then try to add one back...


I get this error:

C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\ in fn_(*a, **k)
    189                                 "Invalid value for %s: %r (choices are: %r)" %
    190                                 (key, val, list(cls.__args__[key].values())))
--> 191             return fn(*args, **kw)
    192         # Add the old argspec to __doc__
    193         doc_addendum = "%s%s" % (fn.__name__,

C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\ in addLayer(self, add_layer_or_layerfile, add_position)
   1136             * TOP: Places the layer or layers at the top of the TOC layer stack."""
-> 1137         return convertArcObjectToPythonObject(self._arc_object.addLayer(*gp_fixargs((add_layer_or_layerfile, add_position), True)))
   1139     @constants.maskargs

ValueError: <MappingLayerObject object at 0x0000012704F6B570>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Anyone have any idea what's going on here or what I should do instead?

0 Kudos
1 Reply
Occasional Contributor II

Have you tried moveLayer ( I'm trying to use the command in a Python toolbox at the moment, not sure if it's working but that seems to be the command to use

0 Kudos