Select to view content in your preferred language

Delete layers saved in memory database from Active map on Pro closing

1053
10
07-27-2023 01:41 AM
ShaneTsh
New Contributor II

 

Hi,

I have created a group layer containing multiple layers inside it in the memory database. But what I need is to delete group layer as well as sub layers inside it when application is closed. Because if these layers are not deleted and since memory database will be deleted on close after opening the project again these layers are shown in red exclamation mark.

I thought of deleting the layers in the UninitializeAsync method of the dockpane by fetching the group layer from the active map.

var groupLayers = MapView.Active?.Map?.Layers?.OfType<GroupLayer>();

But it looks like Active map becomes null when it reaches to the UninitializeAsync  of the dock pane.

Using other approach I implemented this when the project is opened again using MapViewInitializedEvent or DrawCompleteEvent and not on close.

void OnMapViewInitialized(MapViewEventArgs args)
{
     var mapView = args?.MapView;
     if (mapView is null)
          return;
 
     QueuedTask.Run(() =>
     {
          var groupLayers = mapView.Map.Layers?.OfType<GroupLayer>().ToList();
          foreach (var groupLayer in groupLayers.Where(groupLayer => groupLayer.Name.StartsWith("MyLayer")))
               mapView.Map.RemoveLayer(groupLayer);
     });
}
 

Not sure if any other better way like deleting on close only which I cant achieve right now.

@Wolf @GKmieliauskas @CharlesMacleod 

Tags (1)
0 Kudos
10 Replies
CharlesMacleod
Esri Regular Contributor

Maps are persisted in the project. Use the project instead of the view.

take a look at: https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets-Content#get-all-mapprojectitems-for-a-projec...

and https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets-Content#get-a-specific-mapprojectitem 

Alternatively, u could delete the layers on the open of the project when the first view containing the map in question is opened.

 

ShaneTsh
New Contributor II

@CharlesMacleod Ya I am deleting the group layer as well as sub layers when the project is reopened again. But now I am getting another issue which is a message box with error "Failed to open the table" on project reopening. And more interestingly sometimes it comes sometimes it won't.

ShaneTsh_0-1690886101648.png

 

And I am pretty sure this is related to my layers in the memory database because whatever number of layers are present in the group layer that many times this pop is coming. But why is this so? Because memory database will get deleted when ArcGIS Pro is started again (since it will be there for that instance of Pro). And I thought that the tables will also be gone. And on project open I am also deleting the layers as well. But it seems Pro is still trying to open that table. But from where? Any idea what it can be?

@Wolf @GKmieliauskas 

0 Kudos
CharlesMacleod
Esri Regular Contributor

first, identify the source. that is trial and error. i suspect that something on the UI, like a dockpane, was showing information about the layer/table when the project was closed and is attempting to "re" initialize on the open.

0 Kudos
ShaneTsh
New Contributor II

@CharlesMacleod There is no such code at least in my dock pane initialization. The only thing related to layers is my Map initialization code to delete those layers.

MapViewInitializedEvent.Subscribe(OnMapViewInitialized);

void OnMapViewInitialized(MapViewEventArgs args)
{
var map = args?.MapView?.Map;
if (map is null)
return;
 
try
{
QueuedTask.Run(() =>
{
var groupLayers = map.Layers?.OfType<GroupLayer>().ToList();
if (groupLayers is null || groupLayers.Count == 0)
return;
 
foreach (var groupLayer in groupLayers.Where(groupLayer => groupLayer.Name.StartsWith("My Group layer")))
{
var subLayers = groupLayer.Layers.ToList();
if (subLayers?.Count > 0)
map.RemoveLayers(subLayers);
 
map.RemoveLayer(groupLayer);
}
});
}
catch (Exception ex)
{
m_Logger?.LogError(ex, "An error occurred on Map initialization.");
}
}
0 Kudos
CharlesMacleod
Esri Regular Contributor

i was more thinking one of Pro's dockpanes. One that might hv a context related to one of your layers...but it's a guess.

0 Kudos
ShaneTsh
New Contributor II

@CharlesMacleod No I think deleting the in-memory layers now on Map Initialization is causing the error. Because the above logic will run when Map is initialized. But while Pro is in Initialization stage I think it will try to fetch the details of those in-memory layers and since in-memory DB is deleted, it will not find the data for those layers and then it is throwing an error "Failed to Open the table". Therefore, I don't think deleting those layers in Initialization is a good option. But I don't know what will be the other best option since On closing Map View is null.

And also Project.Current (your other option) is also null if I will try to fetch the layers using project and not the view in the Uninitialization event of dock pane.

@Wolf  @GKmieliauskas @UmaHarano 

0 Kudos
CharlesMacleod
Esri Regular Contributor

>Maps are persisted in the project. Use the project instead of the view.

take a look at: https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets-Content#get-all-mapprojectitems-for-a-projec...

and https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets-Content#get-a-specific-mapprojectitem 

<

so:

internal class Module1 : Module {
 protected override bool Initialize() {
  ArcGIS.Desktop.Core.Events.ProjectOpenedEvent.Subscribe((args) => {
   QueuedTask.Run(() => {
     var map_item = Project.Current.GetItems<MapProjectItem>().First();
     var map = map_item.GetMap();
     var grp_layer = map.GetLayersAsFlattenedList()
        .OfType<GroupLayer>().First();
     map.RemoveLayer(grp_layer);
   });
 });

 

be sure to set your autoLoad to true in the config.daml

<modules>
  <insertModule id="..." className="..." autoLoad="true" caption="...">

 

0 Kudos
ShaneTsh
New Contributor II

@CharlesMacleod This is working fine mostly. But what I am seeing now is that after opening the map now that map tab is not on focus, therefore, contents pane will be empty. Now I have to manually click on the Map tab to keep it in focus. Is there a way to keep the tab of the Map on focus if it is not?

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I just worked on an example (slated for 3.2 release) which is using a Memory geodatabase to store log information.  My add-in creates a memory geodatabase and then creates a 'log' table (to track changes) in that memory geodatabase (under certain conditions).  The 'log' table was then added to the map as a 'standalone' table (so that i could use a TableControl to display its content).  If the project was saved at any time the project file on reopening showed the 'log' standalone table entry in the table of content with an error explanation mark.  To solve my problem my add-in listened to the usual events (i.e., ActiveMapViewChangedEvent) and re-created the memory geodatabase and table.   This fixed the problem for me.  Needless to say, if one opens such a project file without my add-in installed the orphaned entry would stay in the table of content.

0 Kudos