I want to retrieve a list of map objects from my current ArcGIS Pro 2.6 project.
The way I achieve that currently is so:
internal void GetMaps()
{
var mapPanes = ProApp.Panes.OfType<IMapPane>();
List<Map> uniqueMaps = new List<Map>();
foreach (var mapPane in mapPanes)
{
if (mapPane.MapView.Map != null)
{
uniqueMaps.Add(mapPane.MapView.Map);
}
}
Maps = uniqueMaps;
}
Executing the method in a saved project (aprx) where I have nine map panes returns an IEnumerable<IMapPane> with a count of nine. However, line seven in the snippet above was introduced because the foreach loop was failing on the add-method (line 9) in some cases.
Debugging and playing with the project before executing the GetMaps() methode showed that map panes, that would return null for their map property in an as-is-project suddenly return a map if the map was activated before executing the GetMap() method.
How can I get rid of this behaviour and/ or is there a possibility to by-pass the issue?
Bests Thomas
Solved! Go to Solution.
Thomas,
One more thing you can look into - If you only want to get a list of all the maps in the project, it is better to get them from the MapProjectItems in your current project. This way, even if a map is not opened in a Map Pane, you will get that map in your collection.
var projectMapItems = Project.Current.GetItems<MapProjectItem>();
if (projectMapItems == null) return null;
var projectMaps = await QueuedTask.Run(() =>
{
List<Map> maps = new List<Map>();
foreach (var item in projectMapItems)
{
maps.Add(item.GetMap());
}
return maps;
});
Something I just learned -
If you have all the map panes open then only the active one is likely to be completely populated. This could be something you are running into. With the code snippet above, you will always be able to retrieve the maps in your project.
Thanks
Uma
Hi Thomas
I am testing this issue - To confirm, are all the panes in your project "MapViews"? Do you have any Attribute table for a feature layer open, for example? These attribute tables impersonate the Map Pane, hence the question.
Thanks
Uma
Hi Uma,
I had to check myself, and simply created a project with two empty map panes. Using the method from the original posting it identifies "Map" correctly, but "Map1" (sitting hidden behind "Map") comes out without a map object.
I will try to add the project as a zip file...
Bests Thomas
Hi Uma, any status change on the issue described above?
Bests Tom
Hi Thomas,
I downloaded your aprx - Thanks for the example. I can see the problem when I run the code.
As soon as I activate Map1, the problem goes away. Also, I tried making a new project with 2 empty maps - similar to yours. But I think I am missing something, because when I make my project, I don't see the issue.
Is there anything specific with how you "hide" Map1 behind Map? When I un-dock my Map1, it comes to the front. I can't hide.
Thanks
Uma
Thomas,
One more thing you can look into - If you only want to get a list of all the maps in the project, it is better to get them from the MapProjectItems in your current project. This way, even if a map is not opened in a Map Pane, you will get that map in your collection.
var projectMapItems = Project.Current.GetItems<MapProjectItem>();
if (projectMapItems == null) return null;
var projectMaps = await QueuedTask.Run(() =>
{
List<Map> maps = new List<Map>();
foreach (var item in projectMapItems)
{
maps.Add(item.GetMap());
}
return maps;
});
Something I just learned -
If you have all the map panes open then only the active one is likely to be completely populated. This could be something you are running into. With the code snippet above, you will always be able to retrieve the maps in your project.
Thanks
Uma