AnsweredAssumed Answered

How to assign name to map layer in APRX when importing from MXD?

Question asked by agelfert on Feb 12, 2019
Latest reply on Dec 10, 2019 by agelfert

I am tasked with rebuilding a bunch of web maps for ArcGIS Server (Enterprise) using set of MXD's behind existing map services. One reason I am doing it through ArcGIS Pro is to play with the new scale based symbologies for Map Image Layers versus creating multiple layers. At the same time, I want to script out map service publishing for any simple maps using arcpy. Starting with a large number of MXD's, I imported them into a blank APRX.


Here is how I grab my list of MXD's:

mxdpath = r'U:\pydev\portal\republish_hou\mxdlist.txt'
with open(mxdpath) as f:
   mxdlist = [mxd.strip() for mxd in f.readlines()]

Then I start importing the MXD's one by one as maps into a new blank project file, which I created inside ArcGIS Pro.

import arcpy
aprx ='c:\temp\blank.aprx')

for mxd in mxdlist:
   #### To be able to see which maps are going in, I add the following print stmt
   print("Importing the following....{0}".format(mxd.split('\\')[-1]))

   #### The following was to keep an eye on total count of maps
   print("Currently {0} maps in Project file.".format(len(aprx.listMaps())))

   #### Here I'm attempting to name the maps with the name of the source MXD
   aprx.listMaps()[-1].name = mxd.split('\\')[-1][:-4]


This seemed to work fine for a small sample, 3-4 MXD's.  In my case, I then tried web map #1 with has 32 MXD files, and it looked like it worked.


But when it look at the names of the maps, I find that some are missing, and have been replaced with names such as "Layers", "Layers1, "Layers2". 

cnt = 0
while True:
   if aprx.listMaps()[cnt]:
      print(cnt," ",aprx.listMaps()[cnt].name)
      cnt += 1

In fact, there is a strange pattern of which names are missing:

>0 Counties
1 Layers
2 Cities
3 Buildings
4 Roads
5 Rivers
6 Playgrounds
7 Lakes
8 Hospitals
9 Churches
10 Hydrants
11 Layers1
12 Rubberduckies
13 Monuments
14 PoliceStations
15 SevenElevens
16 ParkingLots
17 States
18 ZipCodes
19 Districts
20 Divisions
21 Layers2
22 Parks
23 Schools
24 Colleges
25 Geocaches
26 Hotels
27 GasStations
28 SwimmingPools
29 Museums
30 Cemeteries
31 Flamingos

What's going on here? Each MXD has a name that's a valid string, that should be used for naming the map inside the APRX. Why does this fail for maps 1, 11, and 21? It looks like my reference to the last map in the list doesn't work. In fact, the maps called "Layers" contain the data from other "named" maps, e.g. 'States'. Then 'States' gets completely different input and not what should be coming from 'States.mxd'.


Now, let's try this. Let's not try to name the maps at all. Let them remain nameless. When script finishes,...

[ for m in aprx.listMaps()]


So looks like the ordering of the maps in that list goes like 1,10,11,12,13,....2,20,21,22....  I'm sorry maybe there is a good reason for that. But it makes no sense to me. If you have a working solution to offer on how I should name my maps during import, please share. Maybe I've missed something in the documentation.