3

Using ArcPy for ArcGIS Pro 3.1, is it possible to close a particular map view if it is open but not any other open map views in an ArcGIS Pro project?

Currently my tool uses "aprx.closeViews("MAPS")" but of course that closes all the open map views.

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 have not found a method in any other class e.g. "closeView("MyMapName").

I have written a Python toolbox tool and so far I have not not had any issues with users having all the maps closed when they run it. I am not asking how to remove a map completely from a project, but rather how to CLOSE a single map view if the user has it open.

0

2 Answers 2

2

This is as close as I have come to a workaround.

def close_map(close_map_names:list[str]):
    p = arcpy.mp.ArcGISProject('current')
    # get a list of all maps, except close_map_names
    open_maps = [m.name for m in p.listMaps() if m.name not in close_map_names]
    # close all the open maps
    p.closeViews("MAPS")
    # Reopen all the maps except the closed
    for m_name in open_maps:
       p.listMaps(m_name)[0].openView()
    return

The problem is that the listMaps returns all maps, even if they are initially closed, so you have to pass in a list of all the maps to keep closed, not just the one you want to close.

close_map(['Map', 'Map1'])
1
  • The reason I did not choose this as the answer is that I'm already doing this in a very similar manner. I really was hoping there was a method I hadn't found yet that I could use to close a single map without affecting the others in any way. Thanks for the reply! Commented Aug 8, 2023 at 17:52
1

Six years ago, at about ArcGIS Pro 2.0, I had a similar requirement, and got two enhancements underway by submitting ArcGIS Ideas. These can be seen at Saving active view into ArcGIS Pro project using ArcPy and Removing map from ArcGIS Pro project using ArcPy. Both appear to have been implemented but neither does exactly what you are after but they seem to have opened the door for you or others to submit some ArcGIS Pro Ideas for exactly what you need.

While your idea of an aprx.closeView("MyMapName") would be ideal, something else that I looked for without success was an aprx.listViews["MAP"] so that, as a workaround, I could close them all using closeViews("MAPS"), and then iterate the list of map views to re-open all but the one I wanted to close using mapx.openView().

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.