Select to view content in your preferred language

How to properly duplicate a map in ArcGIS using arcpy?

473
3
Jump to solution
01-28-2025 11:32 AM
Sfortunatamente
Occasional Contributor

I have a map with more than 60 layers and I want to duplicate the map many times using arcpy. I use aprx.createMap() to create an empty map and .addLayer to add layers in the loop from the "template" map. The thing is that the order of layers in the resulting map is different than in the template one. I monitor the process with prints, the iteration of adding the layers looks fine but the output is different.

How to do it properly so that order of layers is identical?

 

Thanks, B

0 Kudos
1 Solution

Accepted Solutions
Marshal
Frequent Contributor

You can use ArcGISProject object method .copyItem() to make an exact copy of the map within the same project.

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/arcgisproject-class.htm

It would look something like below.

 

import arcpy

aprx = arcpy.mp.ArcGISProject('CURRENT')

map_obj = aprx.listMaps('TEMPLATE_MAP')[0]
map_copy = aprx.copyItem(map_obj, 'NEW_MAP')

# Further operation on the map_copy object here

aprx.save()

 

 

 

View solution in original post

3 Replies
Marshal
Frequent Contributor

You can use ArcGISProject object method .copyItem() to make an exact copy of the map within the same project.

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/arcgisproject-class.htm

It would look something like below.

 

import arcpy

aprx = arcpy.mp.ArcGISProject('CURRENT')

map_obj = aprx.listMaps('TEMPLATE_MAP')[0]
map_copy = aprx.copyItem(map_obj, 'NEW_MAP')

# Further operation on the map_copy object here

aprx.save()

 

 

 

VittorioGalletto
New Contributor

this code sometimes work, others don't understand why, not:

SystemError: <built-in method copyItem of MappingArcGISProjectObject object at 0x00000287B656FF30> returned a result with an exception set
0 Kudos
Sfortunatamente
Occasional Contributor

Works like magic! Thanks! 

0 Kudos