How to convert to other formats (shapefiles)


Summary
This 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.

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.  

Connecting 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.
[Java]
//Create inWorkspace name.
IDataset inWorkspaceDataset = new IDatasetProxy(inWorkspace);
IWorkspaceName inWorkspaceName = (IWorkspaceName)inWorkspaceDataset.getFullName();

//Create outWorkspace name.
IDataset outWorkspaceDataset = new IDatasetProxy(outWorkspace);
IWorkspaceName outWorkspaceName = (IWorkspaceName)outWorkspaceDataset.getFullName();
  • 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:
[Java]
// Set in dataset and feature class names.
IFeatureClassName inFeatureClassName = new FeatureClassName();
IDatasetName inDatasetName = (IDatasetName)inFeatureClassName;
inDatasetName.setWorkspaceNameByRef(inWorkspaceName);
inDatasetName.setName("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:
[Java]
// Set out dataset and feature class names. 
IFeatureClassName outFeatureClassName = new FeatureClassName();
IDatasetName outDatasetName = (IDatasetName)outFeatureClassName;
outDatasetName.setWorkspaceNameByRef(outWorkspaceName);
outDatasetName.setName("ctgFeatureshp_out.shp");
  • Open the input feature class using the IName.Open method to get field definitions for validation. See the following code:
[Java]
//Open input Featureclass to get field definitions.
IName inName = (IName)inFeatureClassName;
IFeatureClass inFeatureClass = new IFeatureClassProxy(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.
[Java]
//Validate the field names.
IFieldChecker fieldChecker = new FieldChecker();
IFields[] outFeatureClassFields = new IFields[1];
IFields inFeatureClassFields = inFeatureClass.getFields();
IEnumFieldError[] enumFieldError = new IEnumFieldError[1];
fieldChecker.setInputWorkspace(new IWorkspaceProxy(((IName)inWorkspaceName).open()));
fieldChecker.setValidateWorkspaceByRef(new IWorkspaceProxy(((IName)outWorkspaceName)
    .open()));
[Java]
// Validate the fields.
fieldChecker.validate(inFeatureClassFields, enumFieldError, outFeatureClassFields);
[Java]
// Set up the geometry definition.
IField geometryField = outFeatureClassFields[0].getField
    (outFeatureClassFields[0].findField(inFeatureClass.getShapeFieldName()));
// Get the geometry field's geometry definition.
IGeometryDef geometryDef = geometryField.getGeometryDef();
[Java]
IQueryFilter qf = new QueryFilter();

qf.setWhereClause("");
[Java]
IQueryFilter qf = new QueryFilter();
qf.setWhereClause("NAME = 'Los Angeles'");
qf.setSubFields("shape, TAG, TYPE, SUBTYPE");

 
[Java]
IFeatureDataConverter fctofc = new FeatureDataConverter();
IEnumInvalidObject enumErrors = fctofc.convertFeatureClass(inFeatureClassName, qf,
    null, outFeatureClassName, geometryDef, outFeatureClassFields[0], "", 1000, 0);






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.

Development licensingDeployment licensing
ArcGIS for Desktop BasicArcGIS for Desktop Basic
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine