Select to view content in your preferred language

How to refresh a layer after adding a new feature on ArcGIS Pro add-in

908
9
Jump to solution
04-08-2024 07:02 AM
DENGD
by
New Contributor III

I have an ArcGIS pro (3.2.2) add in. I need to refresh the layer or the map after added a new feature. I know that I can set to refresh it by intervals on the layer property.  But that will keeping refresh all layers on the map by the interval. The reason that I need to refresh the layer is because the layer has a field with a trigger to populate the data. The data source is 19c SDE database. I have to save the edit and refresh the layer to get the trigger on. I need that data from that field for some following process. I searched the ArcGIS Pro APIs but did not find a refresh working to set off the trigger (i.e. LayoutView.Active.Layout.RefreshMapSeries(); but LayoutView was null) Any suggestions? Thank you!!

0 Kudos
1 Solution

Accepted Solutions
RobertCao
Esri Contributor

Thanks for the clarification. You might want take take a look of Redraw Method. Not sure if this is exactly what you are looking for (refresh one particular layer) in this case, but hopefully this will help. 

 

Thanks,

Rob

View solution in original post

9 Replies
RobertCao
Esri Contributor

Hi @DENGD ,

 

There are a few ways of refreshing contents/data/connection in ArcGIS Pro. I think this document might cover what you are looking for. 

 

Thanks,

Rob

0 Kudos
DENGD
by
New Contributor III

Thank you for the document. I meant that how to do in C# code to refresh the layer after added a new feature.

0 Kudos
RobertCao
Esri Contributor

Thanks for the clarification. You might want take take a look of Redraw Method. Not sure if this is exactly what you are looking for (refresh one particular layer) in this case, but hopefully this will help. 

 

Thanks,

Rob

DENGD
by
New Contributor III

Thank you so much for the Redraw Method that works perfectly for me. @RobertCao 

anonymous_geographer
Occasional Contributor

Thanks, @RobertCao. This is an okay band-aid option for now, but I'd prefer to refresh a feature layer and its attributes individually instead of bogging down the map session while all feature layers get refreshed unnecessarily. Would you or anyone else on your side happen to know of a way to refresh individual feature layers?

I got some odd suggestions from ChatGPT on this. Most of the options are invalid (e.g. featureLayer.Refresh() is not possible, even if ChatGPT thinks so...what a tease...). I thought maybe featureLayer.ClearDisplayCache() might work since it discusses the refreshing of the display, but it did not work as I needed. I'll put a pin in this and see if you have any other suggestions.

0 Kudos
Gurunara
Occasional Contributor

layer.ClearDisplayCache

0 Kudos
DENGD
by
New Contributor III

layer.ClearDisplayCache is working fine. Thank you for that.

0 Kudos
anonymous_geographer
Occasional Contributor

Following up on my own comment:

featureLayer.SetDataConnection() will refresh the data connection, refresh the attributes, and refresh the renderer within Pro if already open. If I set the featureLayer's data connection to itself, it refreshes and updates. Here's my template example currently in use, if anybody needs a starting point on this.

My script is doing some QC checks, then outputting the error features into an error feature class within the Pro project's default file geodatabase. I then refresh the default geodatabase before isolating the specific feature layer that's open within Pro to have the data connection reset.

 

// For each item in the file geodatabase...
foreach (Item gdb in Project.Current.GetGeodatabases().GetItems())
{
    // If the project's database path matches the expected default database path...
    if (gdb.Path == Project.Current.DefaultGeodatabasePath)
    {
        // Refreshes the File GDB (it won't auto-refresh within open Pro project)
        gdb.Refresh();
    }
}

// Create a new map view of the active map in Pro.
MapView mapView = MapView.Active;

// Check if the map view is valid (i.e. a map is open)
if (mapView != null && mapView.Map != null)
{
    // For each layer in the map...
    foreach (Layer layer in mapView.Map.Layers)
    {
        // Check if the layer is a feature layer...
        if (layer is FeatureLayer featureLayer)
        {
            // If the feature layer's underlying feature class path matches the error feature class path...
            if (System.IO.Path.GetFullPath(featureLayer.GetFeatureClass().GetPath().AbsolutePath) == System.IO.Path.GetFullPath(Project.Current.DefaultGeodatabasePath + "\\" + fc_errors_name))
            {
                // Refresh the feature layer (attributes and renderer) by resetting the data connection to itself again.
                featureLayer.SetDataConnection((CIMStandardDataConnection)featureLayer.GetDataConnection());
                //mapView.Redraw(true); // Redraw disabled. It will update the attribute table, but will not update the feature layer's renderer (as of Pro 3.2.2)
            }
        }
    }
}

 

 

DENGD
by
New Contributor III

featureLayer.SetDataConnection()  did not work on my case where the data source is a feature map service. Thank you for the sharing.

0 Kudos