Change active map with python in current project

1169
4
05-13-2022 01:38 PM
JohnFixCai
New Contributor II

ArcGIS Pro 2.9.2 on Dell laptop running Windows 10.

I have a project opened and I want to use Python to change the active map. 

For this example, I have two maps in my ArcGISProject. Currently "Map A" is my active map, but when I run some geoprocessing tools, via python window, the output will be added to "Map A", but I want the output to be added to "B Map". 

Can I switch my active map with Python? Or how do I direct the output to a specific map? The arcpy.env does not seem to cover "maps".

import arcpy

aprx = arcpy.mp.ArcGISProject('current')
map1 = aprx.listMaps("Map A")[0]
map2 = aprx.listMaps("B Map")[0]

Thanks for you help.

-John

4 Replies
DanPatterson
MVP Esteemed Contributor

Is the project open and you are using a notebook or the python window?

tool in arctoolbox

What circumstances would you need to add data to a map that isn't open instead of the one that is?


... sort of retired...
0 Kudos
JohnFixCai
New Contributor II
The project is open, and I am using the Python window.
Use case: In one map I generate a vector tile cache, in another map I process layers and tables for geocoding, routing and other purposes, and in a third map I add the vector tile package, routing network, and other layers and tables from "map2" and output a mobile map package.
Currently I have my scripts divided into 7 "steps" and I manually switch between windows and run the steps. Ideally, I'd like a single script to run the entire process.
I do understand the create vector tile cache and create mobile map package take the map name as an argument, so in theory I could have "map 2" as my active map. However, there is one function that is not exposed in python I must do manually in the vector tile cache "map" before running the create vector tile cache processing. And, I am still learning python to get the AOI layer at the bottom of the layer stack to create the mobile map package, or the extent of the mmpk is incorrect.
Thanks!
0 Kudos
cs-map
by
New Contributor

Did you ever figure out how to achieve the switch?

I also got stuck trying to do this with arcpy, currently looking into what SDK has to offer for this use case.

0 Kudos
JohnFixCai
New Contributor II

within the script I can target which map I add/remove/modify layers. This has help streamline the process. But I still have 3 scripts instead of 7. I still get tripped up in 2 places....

When I am processing layers and I want to add them to the map to output a mobile map package, I can specify the map to add the layers, and then I can turn them off as I don't want them visible, but usable by the app.

# --- gets the current ArcGIS Pro project file

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

# --- gets the target map

m = aprx.listMaps("MMPK Output*")[0]

# --- sets the path and layers to add

LayerPath = ("D:\\Mobile Map Package Creator\\map_data.gdb\\")

LayerList = ("COMPLEXES", "MILEMARKERS", "Zones", "ROAD_NAMES")

# --- loops thru the layers and adds them to the target map, and then turns the visibility off

for Layer in LayerList:
AddStatus = m.addDataFromPath(LayerPath + Layer)
LayerName = m.listLayers(Layer)[0]
VisibleState = LayerName.visible = 0 #turn off layer

 

What trips me up?

The extent needs to be entered as a property of the map to create a vector tile cache, I can't find a way to do that in python.

I can't seem to use the above python to add a Network Dataset to the map.

0 Kudos