How to access more than one map in an aprx using arcpy.

1237
8
Jump to solution
09-11-2022 01:11 PM
markmorrison1
New Contributor II

I have an aprx with two maps. They do not contain the same data. I am trying to write a script that allows me to perform a search cursor on data in one map and then use the results to perform analysis (select layer by attribute) and zoom on the second map. I have not been able to find a method to "switch" the active map in the documentation. Has anyone had luck working with multiple maps in using arcpy?

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor

Here is a snippet of code that works in the application using the current keyword.  I'm using a point FC in the first map and using the linkID to zoom to another layer in the second map using the ID.

 

p = arcpy.mp.ArcGISProject('current')
m1 = p.listMaps('Map')[0]
m2 = p.listMaps('Map1')[0]

#M1/layer1 - point FC assuming 1 feature is selected
lyr1 = m1.listLayers('PointFC')[0]
cursor = arcpy.SearchCursor(lyr1)
for row in cursor:
linkID = row.getValue("LinkID")
print(linkID)

#M2/layer2 - zoom to polygon feature
lyr2 = m2.listLayers('PolygonFC')[0]
lyr2.definitionQuery = "LinkID = linkID"
view = m2.openView()
view.camera.setExtent(view.getLayerExtent(lyr2, True))

View solution in original post

0 Kudos
8 Replies
DanPatterson
MVP Esteemed Contributor

have you tried listMaps and listLayers ?  use [1] to get the second map if there are only 2

Map—ArcGIS Pro | Documentation

since you have a limited environment, then you don't need to rely on using "Current"


... sort of retired...
markmorrison1
New Contributor II

Thank you for your prompt reply. However listMaps is a read only object. It does not change the active map. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

yes that is correct, but it provides a list of the maps and you can get the second one's name by taking a slice from the list.  Since you have two maps, listMaps()[1] will give you the map's name as a start


... sort of retired...
0 Kudos
JeffBarrette
Esri Regular Contributor

I'd like to learn more about your workflow and why you need to activate a map.  Are you working with maps in map views or maps in map frames (on a layout)?  At 3.0, we have new functions that allow you to call Map.OpenView() or Layout.OpenView() - this command will open and activate the current view.  In terms of referencing multiple maps and passing variable information from one map to another map for the purposes of cursors, changing extents, etc, that all works. 

Jeff - Layout and arcpy.mp teams.

0 Kudos
markmorrison1
New Contributor II

Hi Jeff, I am trying to do something a little unconventional. I have one map with point features that represent electrical equipment. I have another map that contains a diagram with no spatial reference (floating) that represents the same electrical equipment but the locations and dimensions are not necessarily correct. It is used to show connectivity. I made a point feature class for each map that contains a linkID field that is the common element between maps. I want to have the user select a feature from map1, and when they run the script, it will use the linkID to select the sister feature on map2 and zoom to it. The problem is switching to map2 using arcpy. I can't seem to find the correct method. Any help or insight would be appreciated. Thank you.

0 Kudos
JeffBarrette
Esri Regular Contributor

Here is a snippet of code that works in the application using the current keyword.  I'm using a point FC in the first map and using the linkID to zoom to another layer in the second map using the ID.

 

p = arcpy.mp.ArcGISProject('current')
m1 = p.listMaps('Map')[0]
m2 = p.listMaps('Map1')[0]

#M1/layer1 - point FC assuming 1 feature is selected
lyr1 = m1.listLayers('PointFC')[0]
cursor = arcpy.SearchCursor(lyr1)
for row in cursor:
linkID = row.getValue("LinkID")
print(linkID)

#M2/layer2 - zoom to polygon feature
lyr2 = m2.listLayers('PolygonFC')[0]
lyr2.definitionQuery = "LinkID = linkID"
view = m2.openView()
view.camera.setExtent(view.getLayerExtent(lyr2, True))

0 Kudos
markmorrison1
New Contributor II

Thank you Jeff. With a few small tweaks, I got the code to work. For some reason if you have the map open already, it opens another copy and then won't zoom to the layer extent. Also the definition query needs to be "LinkID = " linkID with the variable outside of the quotes.

 

 

0 Kudos
JeffBarrette
Esri Regular Contributor

Interesting, the variable inside the quotes works for me.  And correct, if the map is already open and not active, zooming won't.  We can't identify specific map view by name because a map could have 3 map views, each with different extents.  That is why we developed OpenView().  You can either change the default camera extent before opening a view OR use the navigation functions after opening a view.

 

Jeff - Layout and arcpy.mp teams

0 Kudos