How to convert to other formats (shapefiles)


SummaryThis article shows how to use the IFeatureDataConverter interface to load simple feature data into a geodatabase. The first example sets up the basic framework to convert a shapefile into a file geodatabase feature class. Converting into a personal geodatabase or enterprise geodatabase only requires changing the WorkspaceFactory. Code fragments explaining how to use a spatial index grid size other than the default and how to modify the output spatial reference (project) are also included.

Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime

Additional Requirements
  • The IFeatureDataConverter interface only loads simple feature data (point, line, polygons).
  • If working in ArcSDE, an ArcEditor or greater license will be needed for ArcGIS Desktop, and the Geodatabase Update extension will be required for ArcGIS Engine.

Converting to other formats (shapefiles)

The IFeatureDataConverter interface is designed to work with the ArcCatalog IGxDialog minibrowser and accepts IWorkspaceNames as input. As this example is not using an ArcCatalog IGxDialog minibrowser, two IWorkspace objects are required—one for the source data and one for the target.  
 

Connecing to a geodatabase

The inWorkspace is a file geodatabase feature class, and the outWorkspace, a shapefile. Simple features can be loaded from and to shapefiles, personal geodatabases, file geodatabases, and ArcSDE. ArcInfo coverages can be converted to any of the other listed workspaces but cannot be used as a target. For more information, see How to connect to a geodatabase.
 
  • You need to create the necessary IWorkspaceName objects as shown in the following code:
 

[C#]
              //Create inworkspace name.
IDataset inWorkspaceDataset = (IDataset)inWorkspaceFactory;
IWorkspaceName inWorkspaceName = (IWorkspaceName)inWorkspaceDataset.FullName;
//Create outworkspace name.
IDataset outWorkspaceDataset = (IDataset)outWorkspaceFactory;
IWorkspaceName outWorkspaceName = (IWorkspaceName)outWorkspaceDataset.FullName;
  • Using inWorkspaceName, create inDatasetName using the IDatasetName interface and assign the name of the input feature class, which in this case, is a file geodatabase feature class (ctgFeature_fdcon). See the following code:

[C#]
               // Set in dataset and feature class names.
IFeatureClassName inFeatureClassName = new FeatureClassNameClass();
IDatasetName inDatasetName = (IDatasetName)inFeatureClassName;
inDatasetName.WorkspaceName = inWorkspaceName;
inDatasetName.Name = "ctgFeature_fdcon";
  • Using outWorkspaceName, create outDatasetName and assign the name of the output feature class, which in this case, is a shapefile (ctgFeatureshp_out.shp). The name of the target feature class must not already exist in the outWorkspace. See the following code:

[C#]
               // Set out dataset and feature class names.
IFeatureClassName outFeatureClassName = new FeatureClassNameClass();
IDatasetName outDatasetName = (IDatasetName)outFeatureClassName;
outDatasetName.WorkspaceName = outWorkspaceName;
outDatasetName.Name = "ctgFeatureshp_out.shp";
  • Open the input feature class using the IName.Open method to get field definitions for validation. See the following code:

[C#]
                //Open input Featureclass to get field definitions.
IName inName = (IName)inFeatureClassName;
IFeatureClass inFeatureClass = (IFeatureClass)inName.Open();
  • Set up for field name validation. See the following code:
Setting both the IFieldChecker, InputWorkspace, and IFieldChecker.ValidateWorkspace parameters is required for correct field validation.

[C#]
                //Validate the field names.
IFieldChecker fieldChecker = new FieldCheckerClass();
IFields outFeatureClassFields;
IFields inFeatureClassFields = inFeatureClass.Fields;
IEnumFieldError enumFieldError;
fieldChecker.InputWorkspace = inWorkspaceFactory;
fieldChecker.ValidateWorkspace = outWorkspaceFactory;
  • Validate the fields. If an error is found, enumFieldErrorcan be traversed and an error returned. Refer to IFieldChecker.Validate and enumFieldError for details. See the following code:
 

[C#]
                // Validate the fields.
fieldChecker.Validate(inFeatureClassFields, out enumFieldError, out outFeatureClassFields);
  • Get the geometry definition from the shapefile of the outFeatureClass. See the following code:
 

[C#]
              // Set up the geometry definition.
IField geometryField;
geometryField = outFeatureClassFields.get_Field(outFeatureClassFields.FindField(inFeatureClass.ShapeFieldName));
// Get the geometry field's geometry definition.
IGeometryDef geometryDef = geometryField.GeometryDef;
  • Set up the IQueryFilter to convert all the features by leaving a blank WhereClause. A Where clause can be used to subset the input feature class. See the following code:
 

[C#]
                IQueryFilter qf = new QueryFilterClass();
qf.WhereClause = "";
  • Optionally, set up the IQueryFilter to convert a subset of features using a WhereClause. The SubFields method is used to export only the desired fields. This can be particularly useful where a field type is not supported by the target. For example, if the target is a shapefile, raster columns are not supported and can be removed by not including them in the SubField list. When converting a feature class, the list must always include the geometry column (shape in this example). See the following code:
 

[C#]
                IQueryFilter qf = new QueryFilterClass();
qf.WhereClause = "NAME = 'Los Angeles'";
qf.SubFields = "shape, TAG, TYPE, SUBTYPE";
  • Load the feature class. Though rarely needed, rejected features can be reported using enumErrors. See the following code:

 

[C#]
                IFeatureDataConverter fctofc = new FeatureDataConverterClass();
IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(inFeatureClassName, qf, null, outFeatureClassName, geometryDef, outFeatureClassFields, "", 1000, 0);