Select to view content in your preferred language

Apply symbology to featureclass added via button addin

1149
4
Jump to solution
11-16-2020 01:13 PM
DerekSalinas
Occasional Contributor

Good Afternoon Everyone,

So I have a button created and it is adding a feature class to my map via a FileGeodatabaseConnectionPath. As seen below.

 

            // Add this in your OnClick code-behind for the button
            if (MapView.Active?.Map == null) return;
            ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
            {
                // using the community sample dataset
                using (Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"U:\environmental\envgist.gdb"))))
                {
                    using (FeatureClass addFeatureClass = geodatabase.OpenDataset<FeatureClass>("counties"))
                    {
                        LayerFactory.Instance.CreateFeatureLayer(addFeatureClass, MapView.Active.Map, 0, "Counties");
                    }
                }
            });

 


What information do I need to add to apply symbology from a lyr/lyrx file to this feature class? The layer files are in a folder stored on the same location as the gdb. 

Any help in the right direction is much appreciated.

Thank You,

 

Derek Salinas

0 Kudos
1 Solution

Accepted Solutions
DerekSalinas
Occasional Contributor

Good Afternoon Gin!

Thanks so much for getting back to be so quick. That example worked out wonderfully! I did have to make a few edits to the code, as seen below. 

if (MapView.Active?.Map == null) return;
  QueuedTask.Run(() =>
  {            
  // add lyr/lyrx to map
  var featureLayer = (FeatureLayer)LayerFactory.Instance.CreateLayer(new 
                      Uri(@"layerpath"), MapView.Active.Map, 0, "TOC Name");

  CIMDataConnection currentDataConnection = featureLayer.GetDataConnection();
  CIMStandardDataConnection updatedDataConnection = null;
  WorkspaceFactory wf = WorkspaceFactory.FileGDB;

// FileGeodatabaseConnectionPath does not contain a constructor that takes two arguments

     var dbGdbConnection = new FileGeodatabaseConnectionPath(new Uri(@"gdb 
     Path"));

// provide a replace data connection method

     updatedDataConnection = new CIMStandardDataConnection()
     {
       WorkspaceConnectionString = new 
       Geodatabase(dbGdbConnection).GetConnectionString(),
       WorkspaceFactory = wf,
       Dataset = "FeatureClassName",
       DatasetType = esriDatasetType.esriDTFeatureClass
      };

       featureLayer.SetDataConnection(updatedDataConnection);
       featureLayer.ClearDisplayCache();
  });

 

The main correction was that FileGeodatabaseConnectionPath does not need to have a Uri type to be determined as it does not allow for more than one argument, which is the actual connection path. Maybe something different should be used there instead, but this seems to work all the same.

Once again, thank you for your help. It is very much appreciated.

-Derek Salinas

View solution in original post

0 Kudos
4 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

You can use geoprocessing function ApplySymbologyFromLayer_management. More info about some details and code you can found here:

https://community.esri.com/t5/arcgis-pro-sdk-questions/applysymbologyfromlayer-geoprocessing-tool/m-... 

https://community.esri.com/t5/arcgis-pro-sdk-questions/building-proper-syntax-for-importing-lyr-symb... 

Another way is to create layer from lyrx file and change data connection (CIMDataConnection) to you featureclass source. You can find in Esri Pro samples.

0 Kudos
DerekSalinas
Occasional Contributor

Good Evening Gin,

So in reply to your suggestion:

"Another way is to create layer from lyrx file and change data connection (CIMDataConnection) to you featureclass source. You can find in Esri Pro samples."

Would that just mean I change my existing code above to add the lyr or lyrx file to the map via the button instead of a feature class? 

0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi Derek,

If your lyr/ lyrx file contains path to your file gdb feature class then you need just to add lyr/lyrx file to map, otherwise  you need to change your existing code like this:

await QueuedTask.Run(() => {

// add lyr/lyrx to map
var featureLayer = (FeatureLayer)LayerFactory.Instance.CreateLayer(new Uri(lyrFilePath), MapView.Active.Map, 0, "Counties");

CIMDataConnection currentDataConnection = featureLayer.GetDataConnection();

CIMStandardDataConnection updatedDataConnection = null;
WorkspaceFactory wf = WorkspaceFactory.FileGDB;

var dbGdbConnection = new FileGeodatabaseConnectionPath(new Uri(@"U:\environmental\envgist.gdb"), UriKind.Absolute));
// provide a replace data connection method
updatedDataConnection = new CIMStandardDataConnection()
{
WorkspaceConnectionString = new Geodatabase(dbGdbConnection).GetConnectionString(),
WorkspaceFactory = wf,
Dataset = "counties",
DatasetType = esriDatasetType.esriDTFeatureClass
};

featureLayer.SetDataConnection(updatedDataConnection);

featureLayer.ClearDisplayCache();
});

0 Kudos
DerekSalinas
Occasional Contributor

Good Afternoon Gin!

Thanks so much for getting back to be so quick. That example worked out wonderfully! I did have to make a few edits to the code, as seen below. 

if (MapView.Active?.Map == null) return;
  QueuedTask.Run(() =>
  {            
  // add lyr/lyrx to map
  var featureLayer = (FeatureLayer)LayerFactory.Instance.CreateLayer(new 
                      Uri(@"layerpath"), MapView.Active.Map, 0, "TOC Name");

  CIMDataConnection currentDataConnection = featureLayer.GetDataConnection();
  CIMStandardDataConnection updatedDataConnection = null;
  WorkspaceFactory wf = WorkspaceFactory.FileGDB;

// FileGeodatabaseConnectionPath does not contain a constructor that takes two arguments

     var dbGdbConnection = new FileGeodatabaseConnectionPath(new Uri(@"gdb 
     Path"));

// provide a replace data connection method

     updatedDataConnection = new CIMStandardDataConnection()
     {
       WorkspaceConnectionString = new 
       Geodatabase(dbGdbConnection).GetConnectionString(),
       WorkspaceFactory = wf,
       Dataset = "FeatureClassName",
       DatasetType = esriDatasetType.esriDTFeatureClass
      };

       featureLayer.SetDataConnection(updatedDataConnection);
       featureLayer.ClearDisplayCache();
  });

 

The main correction was that FileGeodatabaseConnectionPath does not need to have a Uri type to be determined as it does not allow for more than one argument, which is the actual connection path. Maybe something different should be used there instead, but this seems to work all the same.

Once again, thank you for your help. It is very much appreciated.

-Derek Salinas

0 Kudos