A new ArcPy method to close a single map view

607
5
08-08-2023 12:05 PM
Status: Open
Labels (1)
eclecticlearner
New Contributor III

I build automation tools for my team.  Most of them are written in python and some are written using ArcPy.  I have a tool that does many things, but one of them is to create a map called "MyMap" if it doesn't exist  or to close "MyMap" if it exist and is open, add some data to "MyMap" and open/reopen it for the user. 

Currently my tool uses "aprx.closeViews("MAPS")" but of course that closes all the open map views.  I want to be able to close a single specific map with my tool.

For context: The closeViews() method is part of the ArcGISProject class and the openView() method is part of the Map class. The closeViews() closes all open views as specified e.g. closeViews("MAPS"). The openView() opens a specific map openView("MyMapName").

I am proposing a new ArcPy method such as "closeView("MyMapName").  

Of course there are work arounds, but a simple call to close a single map by name would be much simpler.

5 Comments
JeffBarrette

@eclecticlearner its possible that a map view or layout view could be opened multiple times.  For example, you could have 3  map view tabs (e.g., Map, Map, Map). 

Do you want to close a specific, singular map view  OR would you be ok with closing all of them (that have the same label).  The former is currently not possible, the later might be.

At 3.2, there is a new number/sequence where the above example would show as Map (1/3), Map (2/3) and Map (3/3).  We would need to investigate if its reasonable to close a view based on an auto generated suffix.  I think that may introduce some challenges.

 

Jeff - Layout and arcpy.mp teams

eclecticlearner

@JeffBarrette I think it is probably reasonable to close all open instances of a map view with the same label.  Having the same map open multiple times has never occurred to me, and to be honest I don't think I even knew it was possible to have multiple instances open of the same map.  That being said, it does raise a little bit of "concern" to close all the maps... I wonder if my tool should come with a warning label :-).  For example, if I have multiple instances of the same map view open (MyMap, MyMap, MyMap), I guess it stands to reason, I can manipulate data in each of those independent of the other instances? If that is the case, then which map instance gets saved when they all close and the project is saved?  

My tool closes all open maps, does some analysis on the back end, adds a data layer to the map "MyMap", adds symbology to the data layer, and opens/reopens the map "MyMap".  

I think for my purposes, closing all instances of "MyMap" is ok, and the burden will be on the user to save the other instances as a new map or risk losing any changes/differences between the instances.  

Thanks for your feedback/input.  I hope this idea moves along!

AlfredBaldenweck

Maybe, in cases where the same map is open multiple times, be able to specify which view?

Like, if layer names are duplicated, they're Layer:1 and Layer:2 behind the scenes. 

Maybe have closeview("MapView") close all map views of that name, but closeview("MapView:1") only close the first one?

JeffBarrette

@eclecticlearner multiple map views that point to the same MAP all display the same data.  The only differences is that they can each have their own camera information (i.e., they can display different scales or have different rotations, etc independent of one another). So even if you edit data in one map view, the other map views will automatically update.  And it is NOT possible for map views pointing to the same MAP to have different symbology or collections of layers (that is all managed on the MAP).

The issue here is that map views don't have names that differentiate one map view from the other so we can't find a specific map view and close only it. If you are ok with the idea that calling something like MAP.closeView() will close all map views that reference the same MAP, that could be possible.  But if you require being able to close a specific view, we may not do that due to current limitations and the intent of arcpy.mp.

Arcpy.mp is intended for map and project automation, not application customization (that is what the .Net SDK is intended for).  And arcpy.mp can also be run out of process (outside the application).  Scripts run out of process can NOT manipulate views.  This is documented in many locations in our help.

Our original design was simple in intent.  You do something to a map or layout and you want to see the changes in the application.  Because we have no way of knowing if the mapview or layoutview is open or focused, we simply provided Map/Layout.openView().  This would create a duplicate view if it happened to be opened already.  That is why we also provided Project.closeViews().  This allows you to close all views before opening the one view you want to see updates.

Jeff

eclecticlearner

@JeffBarrette That's all great information and provides me with good insight! With that information I can pretty confidently say for my purposes calling something like "MAP.closeView()" closing all map views that reference the same MAP will be exactly what I need.  

Thanks again for your feedback.