Selection set to Feature Class

5489
11
04-18-2010 06:12 PM
JeffGiesler
Occasional Contributor
Hello  Everyone,
I have a quick question.  I am trying to get a set of selected values from a map layer and get them into a feature class.  I have the selected Item in IFeatureSeletion.SelectionSet and I would like to get it into IfeatureClass.  Can this be done simply thriough a cast or do I need to export it.  I hope someone can help.
Cheers,
Jeff
0 Kudos
11 Replies
DevdattaTengshe
New Contributor II
You can use the IExportOperation:ExportFeatureClass method for this.

Here is some sample VBA code:

Sub ExportFc()
    Dim pDoc As IMxDocument
    Dim pFLayer As IFeatureLayer
    Dim pFc As IFeatureClass
    Dim pInFeatureClassName As IFeatureClassName
    Dim pDataset As IDataset
    Dim pInDsName As IDatasetName
    Dim pFSel As IFeatureSelection
    Dim pSelSet As ISelectionSet
    Dim pFeatureClassName As IFeatureClassName
    Dim pOutDatasetName As IDatasetName
    Dim pWorkspaceName As IWorkspaceName
    Dim pExportOp As IExportOperation
   
    'Get the first layer in the map
    Set pDoc = ThisDocument
    Set pFLayer = pDoc.FocusMap.Layer(0)
    Set pFc = pFLayer.FeatureClass
   
    'Get the Featureclassname from the featureclass
    Set pDataset = pFc
    Set pInFeatureClassName = pDataset.FullName
    Set pInDsName = pInFeatureClassName
   
    'Get the selected features
    Set pFSel = pFLayer
    Set pSelSet = pFSel.SelectionSet
   
    'Define the output featureclass
    'give it the name of the input feature class + exp
    Set pFeatureClassName = New FeatureClassName
    Set pOutDatasetName = pFeatureClassName
    pOutDatasetName.Name = pFc.AliasName & "exp"
    Set pWorkspaceName = New WorkspaceName
    pWorkspaceName.PathName = "c:\temp"
    pWorkspaceName.WorkspaceFactoryProgID = "esriCore.shapefileworkspacefactory.1"
    Set pOutDatasetName.WorkspaceName = pWorkspaceName
    'Give the output shapefile the same props as the input dataset
    pFeatureClassName.FeatureType = pFc.FeatureType
    pFeatureClassName.ShapeType = pFc.ShapeType
    pFeatureClassName.ShapeFieldName = pFc.ShapeFieldName
   
    'Export selected features
    Set pExportOp = New ExportOperation
    pExportOp.ExportFeatureClass pInDsName, Nothing, pSelSet, Nothing, pOutDatasetName, 0
End Sub
0 Kudos
CK
by
New Contributor
Hello!
Is it also possible to export the features into a feature dataset? I wrote the following code but it only works when the features are exported to the database and not into a feature dataset:

    Dim pMxDocument     As IMxDocument
    Dim pMap            As IMap
    Dim pFeatureLayer   As IFeatureLayer
    Dim pDisplayTbl     As IDisplayTable
    Dim pFeatureClass   As IFeatureClass
    Dim pDataset        As IDataset
    Dim pInDSname       As IDatasetName
    Dim pathExport      As String
    Dim pWorkspaceFact  As IWorkspaceFactory
    Dim pWorkspace      As IWorkspace
    Dim pWorkspaceName  As IWorkspaceName
    Dim pOutDSname      As IDatasetName
    Dim pExportOp       As IExportOperation
   
    Set pMxDocument = ThisDocument
    Set pMap = pMxDocument.FocusMap
    Set pFeatureLayer = pMap.Layer(0)
    Set pDisplayTbl = pFeatureLayer
    Set pFeatureClass = pDisplayTbl.DisplayTable
    Set pDataset = pFeatureClass
    Set pInDSname = pDataset.FullName

    pathExport = "E:\Test\Join\database.mdb"
    Set pWorkspaceFact = New AccessWorkspaceFactory
    Set pWorkspace = pWorkspaceFact.OpenFromFile(pathExport, 0)
    Set pDataset = pWorkspace
   
    Set pWorkspaceName = pDataset.FullName
    Set pOutDSname = New FeatureClassName
    pOutDSname.Name = "Export"
    Set pOutDSname.WorkspaceName = pWorkspaceName

    Set pExportOp = New ExportOperation
    pExportOp.ExportFeatureClass pInDSname, Nothing, Nothing, Nothing, pOutDSname, 0


I don't know what to change... 😞
0 Kudos
ShaunConway
Occasional Contributor II
Did you ever resolve your issue? I am also trying to save to a feature dataset and having trouble figuring it out.

Thanks,
Shaun
0 Kudos
JeffGiesler
Occasional Contributor
Shaun,
Did you try to create the Feature Class in a feature dataset then use that FC as the output in the IExportOperation.exportfeatureclass.  The link below shows you how to create the feature class in a feature dataset.
Cheers,
Jeff
http://edndoc.esri.com/arcobjects/9.2/NET/0b614f08-461a-4d63-bbcd-1c633a7886ba.htm
0 Kudos
DerekLoi
New Contributor III
I will chime in on this since i've been struggling to figure this out too...
here's what i had to do.... (ArcEngine application)

1) select the features in the layer that you want in your map control
        /// <summary>
        /// selects a feature from a specific featureclass
        /// </summary>
        /// <param name="myMap">map control</param>
        /// <param name="player">layer to search on in the map control</param>
        /// <param name="searchString">search criteria</param>
        public void selectFeature(AxMapControl myMap, ILayer player, string searchString)
        {
            pFeatureLayer = (IFeatureLayer)player;
            if (pFeatureLayer != null)
            {
                //select the feature
                IQueryFilter queryFilter = new QueryFilterClass();
                queryFilter.WhereClause = searchString;
                IFeatureSelection featureSelection = (IFeatureSelection)pFeatureLayer;
                myMap.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, pFeatureLayer, null);
                featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
            }
            return;
        }


2) then call the code to run the "Export"
        /// <summary>
        /// Exports the selected features of a layer in the Map Control to a Shapefile
        /// </summary>
        /// <param name="pLayer">mapcontrol layer</param>
        /// <param name="inputPath">input shapefile full path</param>
        /// <param name="outputPath">output shapefile full path</param>
        /// <param name="qry">query to filter out features</param>
        public void ExportMapLyrToShapefile(ILayer pInLayer, string inputName, string outputPath, string qry)
        {
            // Set the GeoFeatureLayer to the layer in the map so that we can use it as the input feature later
            IGeoFeatureLayer pInGFLayer = (IGeoFeatureLayer)pInLayer;
            IFeatureLayer pInFeatureLayer = (IFeatureLayer)pInGFLayer;
            IFeatureClass inputFeatureClass = pInFeatureLayer.FeatureClass;
            IDataset inputDataset = (IDataset)inputFeatureClass;
            IDatasetName inputDatasetName = (IDatasetName)inputDataset.FullName;

            // Get the layer's selection set (the selected/highlighted features of the layer in the mapcontrol)
            IFeatureSelection featureSelection = (IFeatureSelection)pInFeatureLayer;
            ISelectionSet selectionSet = featureSelection.SelectionSet;

            // Create a feature class name for the output shapefile and open the output's workspace
            IWorkspaceFactory shpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            IWorkspace shpWorkspace = shpWorkspaceFactory.OpenFromFile(outputPath, 0);
            IDataset shpWorkspaceDataset = (IDataset)shpWorkspace;
            IWorkspaceName workspaceName = (IWorkspaceName)shpWorkspaceDataset.FullName;
            IFeatureClassName shpFeatureClassName = new FeatureClassNameClass();
            IDatasetName shpDatasetName = (IDatasetName)shpFeatureClassName;
            shpDatasetName.WorkspaceName = workspaceName;
            shpDatasetName.Name = qry.Replace(" = ", "");

            // Use the IFieldChecker interface to make sure all of the field names are valid for the output shapefile.
            IFieldChecker fieldChecker = new FieldCheckerClass();
            IFields shapefileFields = null;
            IEnumFieldError enumFieldError = null;
            fieldChecker.InputWorkspace = inputDataset.Workspace;
            fieldChecker.ValidateWorkspace = shpWorkspace;
            fieldChecker.Validate(inputFeatureClass.Fields, out enumFieldError, out shapefileFields);
            
            // We also need to retrieve the GeometryDef from the input feature class.
            int shapeFieldPosition = inputFeatureClass.FindField(inputFeatureClass.ShapeFieldName);
            IFields inputFields = inputFeatureClass.Fields;
            IField shapeField = inputFields.get_Field(shapeFieldPosition);
            IGeometryDef geometryDef = shapeField.GeometryDef;

            //// Create a query filter to select specific features in the input featureclass
            //IQueryFilter queryFilter = new QueryFilterClass();
            //queryFilter.WhereClause = qry;

            // Now we create a feature data converter to "Export" the selected features to a shapefile
            IFeatureDataConverter2 featureDataConverter2 = new FeatureDataConverterClass();
            IEnumInvalidObject enumInvalidObject = featureDataConverter2.ConvertFeatureClass(inputDatasetName, null, selectionSet, null, shpFeatureClassName, geometryDef, shapefileFields, "", 1000, 0);
        }


*note that i didn't use the IQueryFilter.... I kept getting errors when going that route...
Now, the only issue i'm running into is that I have a table joined to the map layer and those joined fields are not being "exported"!  So far no luck on this particular issue.

If anyone has any info on how I can include my joined table's fields in the "Export"... please help!
Derek
0 Kudos
JeffreyHamblin
New Contributor III
Now, the only issue i'm running into is that I have a table joined to the map layer and those joined fields are not being "exported"!  So far no luck on this particular issue.

If anyone has any info on how I can include my joined table's fields in the "Export"... please help!
Derek



Take a look at using IDisplayTable for your source.
0 Kudos
DerekLoi
New Contributor III
Take a look at using IDisplayTable for your source.


Hi Jeff,
I tried using an IDisplayTable as you suggested but it still exports the selected features without the joined fields... is there something i'm doing wrong?

i selected the features in the layer (0) in the map control
then I cast the map layer to an IDisplayTable
    -> IDisplayTable myDispTable = (IDisplayTable)axMapControl1.get_Layer(0);
and then used the IDisplayTable "myDispTable" as the input...
0 Kudos
JeffreyHamblin
New Contributor III
I suspect it is because you are using the IFeatureDataConverter2.

I just did a quick test with IExportOperation instead, in an Add-In, and it does export joined fields.

Here's some quick and dirty code that I tested it with:
public void PerformTest5()
{
    // Get the selection in TOC.
    object item = ArcMap.Document.SelectedItem;

    // Make sure it is a feature layer
    if (item is IFeatureLayer)
    {
        IFeatureLayer featureLayer = item as IFeatureLayer;

        IDisplayTable displayTable = featureLayer as IDisplayTable;
        ISelectionSet selectionSet = displayTable.DisplaySelectionSet;
        IDataset dataset = displayTable.DisplayTable as IDataset;
        IDatasetName datasetName = dataset.FullName as IDatasetName;

        IWorkspaceFactory outShapeWSF = new ShapefileWorkspaceFactoryClass();
        IDataset outShapeWS = outShapeWSF.OpenFromFile("C:\\GIS", 0) as IDataset;
        IWorkspaceName outShapeWSName = outShapeWS.FullName as IWorkspaceName;
        IDatasetName outShapeName = new FeatureClassNameClass();
        outShapeName.Name = "Export_test1";
        outShapeName.WorkspaceName = outShapeWSName;

        IExportOperation exportOperation = new ExportOperationClass();
        exportOperation.ExportFeatureClass(datasetName, null, selectionSet, null,
                outShapeName as IFeatureClassName, 0);

    }
    else
    {
        MessageBox.Show("Please select a feature layer.");
    }


}
NigelDsouza
Occasional Contributor

This works well when I export to a file geodatabase.

What part of code do I have to modify if I wish to export it to a featuredataset within a file geodatabase?

0 Kudos