Select to view content in your preferred language

How to switch the active map in ArcGIS Pro project

1150
3
Jump to solution
01-05-2023 08:34 AM
BarakGarty
Occasional Contributor

I have 2 Maps (tabs) in an active (loaded) project in the AcrGIS Pro SW.

Map1 is the active map. I am running a script via the python console  that intersect 2 layers (lyr1 and lyr2) that are in Map2 and output a new layer (lyr3);

The output of that intersect operation is added to the active Map (Map1) but I want it to be added to where the 2 layers that are being intersect are (i.e. Map2);

 

- I know that I can manually switch the active map by selecting it with the mouse prior to running my script. Don't want to do it this way....

- Is there a python method to force that switch? I tried using openView() but this is not the solution I am looking for as this opens another view of Map2 that already exists. 

* this same question was asked in this forum last May but was not answered.

Thanks in advance.

0 Kudos
2 Solutions

Accepted Solutions
DanielMiranda2
Regular Contributor

When I ran into this issue, I set the environmental variable addOutputsToMap to false. This of course only works if you are running your code in Pro, which it sounds like you are.

arc.env.addOutputstoMap = False

 

That stops layers from being automatically added to maps. Then, if you store the result of your intersect in a variable, you can use addDataFromPath on the specific map you want to add the layer to.

 

Relevant links:

env—ArcGIS Pro | Documentation (search addOutputstoMap

Map—ArcGIS Pro | Documentation (search addDataFromPath)

View solution in original post

BarakGarty
Occasional Contributor

Excellent!

I used your solution and found it to work for my needs in this case.

Thank you.

View solution in original post

3 Replies
DanielMiranda2
Regular Contributor

When I ran into this issue, I set the environmental variable addOutputsToMap to false. This of course only works if you are running your code in Pro, which it sounds like you are.

arc.env.addOutputstoMap = False

 

That stops layers from being automatically added to maps. Then, if you store the result of your intersect in a variable, you can use addDataFromPath on the specific map you want to add the layer to.

 

Relevant links:

env—ArcGIS Pro | Documentation (search addOutputstoMap

Map—ArcGIS Pro | Documentation (search addDataFromPath)

BarakGarty
Occasional Contributor

Excellent!

I used your solution and found it to work for my needs in this case.

Thank you.

DougGreen
Frequent Contributor

It sounds like you want to specifically check if a map is open as a View and then work with it if it is open and if it is not open, you want to open it and then specifically add layers to a certain map correct?

There's a post here where it indicates that there's not a way to tell if a map is open in a view and an ESRI employee suggests that it would be good to submit an idea that exposes that property of a map.

However, despite knowing whether or not the view is open, explicitly getting a reference to each map using the aprx object that you want to interact with is totally possible and might meet your need. 

aprx = arcpy.mp.ArcGISProject("CURRENT")
map1 = aprx.listMaps("my first map")
if map1 == None:
    print("Could not find map: my first map")
    # end or raise error here

map2 = aprx.listMaps("my second map")
if map2 == None:
    print("Could not find map: my second map")
    # end or raise error here

 

Each map object could then be used to add layers as you wish.