Select to view content in your preferred language

How to create a feature class?

298
8
Jump to solution
4 weeks ago
nadja
by
Occasional Contributor III

This might appear really basic, but I only manage to create a query feature class from my dockpane using this code:

await QueuedTask.Run(() => LayerFactory.Instance.CreateLayer<FeatureLayer>(flyrCreatnParam, MapView.Active.Map));

  that's nice, but query feature class don't show the area and length of a feature. however we need that, which is why we want to create a feature class instead. the data lies on a postgis/postgresql database. how can I create a feature class using the ArcGIS Pro .NET SDK?

 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
nadja
by
Occasional Contributor III

never mind, I found half of the solution:

using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri(sde_path))))
{

    //ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(featureClassDefinition.ToString(), "featureClassDefinition");
    using (FeatureClass fc = geodatabase.OpenDataset<FeatureClass>("parcs."+name))
    {
        // Create the feature layer creation parameters
        FeatureLayerCreationParams layerCreationParams = new FeatureLayerCreationParams(fc)
        {
            // Optionally set additional properties
            Name = name
        };


        // Create the feature layer and add it to the active map
        await QueuedTask.Run(() => LayerFactory.Instance.CreateLayer<FeatureLayer>(layerCreationParams, MapView.Active.Map));
    }
}

only thing is, it doesnt really behave like a Enterprise Geodatabase Feature Class. Neither the attribute Shape_Length nor Shape_Area are in the attribute table and there still is a strange query in the layer properties (SELECT attr1,attr2,attr3,...,attrn FROM schema.name). 

View solution in original post

0 Kudos
8 Replies
AbhijeetNandeshwar1
New Contributor III
0 Kudos
Aashis
by Esri Contributor
Esri Contributor

You can use FeatureClass featureClass = featureLayer.GetFeatureClass() to access the feature class from a feature layer. Please refer to the API for more info.

0 Kudos
nadja
by
Occasional Contributor III

Thank you @Aashis  and @AbhijeetNandeshwar1 for your answers.

@Aashis I tried that, but no matter what I do, it won't add the data as FeatureClass to my Active Map, it always adds a Query Feature Class to my map or throws an error that that method is not implemented even if I'm using the code from https://github.com/Esri/arcgis-pro-sdk/wiki/ProSnippets-Geodatabase or the API. 

LayerFactory.Instance.CreateLayer<FeatureLayer>(flyrCreatnParam, MapView.Active.Map)

 how do I add the created feature class to my active map?

0 Kudos
Aashis
by Esri Contributor
Esri Contributor

Could you post your complete code snippet and provide additional details about the error? I have difficulty understanding "it won't add the data as FeatureClass to my Active Map; it always adds a Query Feature Class to my map." and the error you got.

0 Kudos
nadja
by
Occasional Contributor III

@Aashis of course, sorry. I hope I got all relevant snippets.

private async Task AddVectorToMapMethod()
{
    await QueuedTask.Run(async () =>
    {
        try
        {
            // Use the Dispatcher to access UI elements from the UI thread
            TreeViewItem selectedNode = null;
            vector_gdbTreeView.Dispatcher.Invoke(() =>
            {
                selectedNode = vector_gdbTreeView.SelectedItem as TreeViewItem;
            });
            if (selectedNode != null)
            {
                string data_path = null;
                string name = null;
                selectedNode.Dispatcher.Invoke(() =>
                {
                    data_path = selectedNode.Tag.ToString();
                    string[] parts = data_path.Split('.');
                    name = parts[1] + "." + parts[2];
                });
                string layer_path = sde_path + "\\" + data_path;
               
                // C:\Users\xyzx\AppData\Roaming\Esri\ArcGISPro\Favorites\db_connection.sde\yyy.schema.name
                if (selectedNode != null & !layer_path.EndsWith(".sde"))
                {
                    // creates the feature layer parameters
                    var flyrCreatnParam = new FeatureLayerCreationParams(new Uri(layer_path))
                    {
                        Name = name
                    };

                    // Access the active map
                    if (MapView.Active != null)
                    {
                     using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri(sde_path))))
{

        // Create the FeatureLayer
    FeatureLayer featureLayer = LayerFactory.Instance.CreateLayer<FeatureLayer>(flyrCreatnParam, MapView.Active.Map);

    // Access the FeatureClass from the created FeatureLayer
    FeatureClass featureClass = featureLayer.GetTable() as FeatureClass;

    string featureClassName = featureClass.GetDefinition().GetName();
    FeatureClassDefinition featureClassDefinition = geodatabase.GetDefinition<FeatureClassDefinition>(featureClassName);

    using (FeatureClass fc = geodatabase.OpenDataset<FeatureClass>(featureClassName))
    {
        // Create the feature layer creation parameters
        FeatureLayerCreationParams layerCreationParams = new FeatureLayerCreationParams(featureClass)
        {
            // Optionally set additional properties
            Name = name,
            IsVisible = true
        };


        // Create the feature layer and add it to the active map
        LayerFactory.Instance.CreateLayer<FeatureLayer>(layerCreationParams, MapView.Active.Map);
        // FeatureLayer fl = LayerFactory.Instance.CreateLayer<FeatureLayer>(layerCreationParams, MapView.Active.Map);
        
    }
   
}  
                    else
                    {
                       
                        // do sth else
                        
                    }
                }
                else
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("No item selected.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }


        }
        catch (Exception ex)
        {
            // Handle exceptions
            ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(ex.Message, "Error: Adding vectors to map not possible");
        }
    });

}         
0 Kudos
Aashis
by Esri Contributor
Esri Contributor

Line # 52:  FeatureLayerCreationParams layerCreationParams = new FeatureLayerCreationParams(featureClass). Are you trying to refer to the feature class inside from a geodatabase? Then it should be  FeatureLayerCreationParams layerCreationParams = new FeatureLayerCreationParams(fc)

0 Kudos
nadja
by
Occasional Contributor III

Not sure what you mean by "Are you trying to refer to the feature class inside from a geodatabase". I try this: I have my PostGIS/postgresql database with vectors. I can list these vectors in my dockpane and select one, e.g. schema.name. The code snippet is me trying to add that dataset schema.name to the active map as feature class like we had in our old database (ArcSDE) 

nadja_swiss_parks_0-1720627670359.png

or which is default from a file geodatabase 

nadja_swiss_parks_1-1720627712419.png

(not as query feature class

nadja_swiss_parks_2-1720627767504.png

).

0 Kudos
nadja
by
Occasional Contributor III

never mind, I found half of the solution:

using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri(sde_path))))
{

    //ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(featureClassDefinition.ToString(), "featureClassDefinition");
    using (FeatureClass fc = geodatabase.OpenDataset<FeatureClass>("parcs."+name))
    {
        // Create the feature layer creation parameters
        FeatureLayerCreationParams layerCreationParams = new FeatureLayerCreationParams(fc)
        {
            // Optionally set additional properties
            Name = name
        };


        // Create the feature layer and add it to the active map
        await QueuedTask.Run(() => LayerFactory.Instance.CreateLayer<FeatureLayer>(layerCreationParams, MapView.Active.Map));
    }
}

only thing is, it doesnt really behave like a Enterprise Geodatabase Feature Class. Neither the attribute Shape_Length nor Shape_Area are in the attribute table and there still is a strange query in the layer properties (SELECT attr1,attr2,attr3,...,attrn FROM schema.name). 

0 Kudos