1.Copying and pasting geodatabase datasets
// Create workspace name objects.
IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass();
IWorkspaceName targetWorkspaceName = new WorkspaceNameClass();
IName targetName = (IName)targetWorkspaceName;
// Set the workspace name properties.
sourceWorkspaceName.PathName = @"C:\arcgis\ArcTutor\BuildingaGeodatabase\Montgomery.gdb";
sourceWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory";
targetWorkspaceName.PathName = @"PartialMontgomery.gdb";
targetWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory";
// Create a name object for the source feature class.
IFeatureClassName featureClassName = new FeatureClassNameClass();
// Set the featureClassName properties.
IDatasetName sourceDatasetName = (IDatasetName)featureClassName;
sourceDatasetName.WorkspaceName = sourceWorkspaceName;
sourceDatasetName.Name = "Blocks";
IName sourceName = (IName)sourceDatasetName;
// Create an enumerator for source datasets.
IEnumName sourceEnumName = new NamesEnumeratorClass();
IEnumNameEdit sourceEnumNameEdit = (IEnumNameEdit)sourceEnumName;
// Add the name object for the source class to the enumerator.
sourceEnumNameEdit.Add(sourceName);
// Create a GeoDBDataTransfer object and a null name mapping enumerator.
IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass();
IEnumNameMapping enumNameMapping = null;
// Use the data transfer object to create a name mapping enumerator.
Boolean conflictsFound = geoDBDataTransfer.GenerateNameMapping(sourceEnumName, targetName, out enumNameMapping);
enumNameMapping.Reset();
// Check for conflicts.
if (conflictsFound)
{
// Iterate through each name mapping.
INameMapping nameMapping = null;
while ((nameMapping = enumNameMapping.Next()) != null)
{
// Resolve the mapping's conflict (if there is one).
if (nameMapping.NameConflicts)
{
nameMapping.TargetName = nameMapping.GetSuggestedName(targetName);
}
// See if the mapping's children have conflicts.
IEnumNameMapping childEnumNameMapping = nameMapping.Children;
if (childEnumNameMapping != null)
{
childEnumNameMapping.Reset();
// Iterate through each child mapping.
INameMapping childNameMapping = null;
while ((childNameMapping = childEnumNameMapping.Next()) != null)
{
if (childNameMapping.NameConflicts)
{
childNameMapping.TargetName = childNameMapping.GetSuggestedName(targetName);
}
}
}
}
}
// Start the transfer.
geoDBDataTransfer.Transfer(enumNameMapping, targetName);
2.Converting simple data
public void ConvertShapefileToFeatureClass()
{
// Create a name object for the source (shapefile) workspace and open it.
IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass
{
WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory",
PathName = @"C:\Data\Shapefiles"
};
IName sourceWorkspaceIName = (IName)sourceWorkspaceName;
IWorkspace sourceWorkspace = (IWorkspace)sourceWorkspaceIName.Open();
// Create a name object for the target (file GDB) workspace and open it.
IWorkspaceName targetWorkspaceName = new WorkspaceNameClass
{
WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory",
PathName = @"C:\Data\Public.gdb"
};
IName targetWorkspaceIName = (IName)targetWorkspaceName;
IWorkspace targetWorkspace = (IWorkspace)targetWorkspaceIName.Open();
// Create a name object for the source dataset.
IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;
sourceDatasetName.Name = "Can_Mjr_Cities";
sourceDatasetName.WorkspaceName = sourceWorkspaceName;
// Create a name object for the target dataset.
IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName;
targetDatasetName.Name = "Cities";
targetDatasetName.WorkspaceName = targetWorkspaceName;
// Open source feature class to get field definitions.
IName sourceName = (IName)sourceFeatureClassName;
IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();
// Create the objects and references necessary for field validation.
IFieldChecker fieldChecker = new FieldCheckerClass();
IFields sourceFields = sourceFeatureClass.Fields;
IFields targetFields = null;
IEnumFieldError enumFieldError = null;
// Set the required properties for the IFieldChecker interface.
fieldChecker.InputWorkspace = sourceWorkspace;
fieldChecker.ValidateWorkspace = targetWorkspace;
// Validate the fields and check for errors.
fieldChecker.Validate(sourceFields, out enumFieldError, out targetFields);
if (enumFieldError != null)
{
// Handle the errors in a way appropriate to your application.
Console.WriteLine("Errors were encountered during field validation.");
}
// Find the shape field.
String shapeFieldName = sourceFeatureClass.ShapeFieldName;
int shapeFieldIndex = sourceFeatureClass.FindField(shapeFieldName);
IField shapeField = sourceFields.get_Field(shapeFieldIndex);
// Get the geometry definition from the shape field and clone it.
IGeometryDef geometryDef = shapeField.GeometryDef;
IClone geometryDefClone = (IClone)geometryDef;
IClone targetGeometryDefClone = geometryDefClone.Clone();
IGeometryDef targetGeometryDef = (IGeometryDef)targetGeometryDefClone;
// Cast the IGeometryDef to the IGeometryDefEdit interface.
IGeometryDefEdit targetGeometryDefEdit = (IGeometryDefEdit)targetGeometryDef;
// Set the IGeometryDefEdit properties.
targetGeometryDefEdit.GridCount_2 = 1;
targetGeometryDefEdit.set_GridSize(0, 0.75);
// Create a query filter to only select cities with a province (PROV) value of 'NS.'
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = "PROV = 'NS'";
queryFilter.SubFields = "Shape, NAME, TERM, Pop1996";
// Create the converter and run the conversion.
IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();
IEnumInvalidObject enumInvalidObject = featureDataConverter.ConvertFeatureClass
(sourceFeatureClassName, queryFilter, null, targetFeatureClassName,
targetGeometryDef, targetFields, "", 1000, 0);
// Check for errors.
IInvalidObjectInfo invalidObjectInfo = null;
enumInvalidObject.Reset();
while ((invalidObjectInfo = enumInvalidObject.Next()) != null)
{
// Handle the errors in a way appropriate to the application.
Console.WriteLine("Errors occurred for the following feature: {0}",
invalidObjectInfo.InvalidObjectID);
}
}
3.How to export a dataset to XML
本主题介绍如何使用IGdbXmlExport接口输出数据库的Schema。这个例子创建了一个方法,通过接受数据库名称作为参数为geodatabase创建一个可扩展标记语言(XML)的文件。
private void ExportDatasetToXML(String fileGdbPath, String outputXmlFile)
{
// Open the source geodatabase and create a name object for it.
Type factoryType = Type.GetTypeFromProgID(
"esriDataSourcesGDB.FileGDBWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
(factoryType);
IWorkspace workspace = workspaceFactory.OpenFromFile(fileGdbPath, 0);
IDataset workspaceDataset = (IDataset)workspace;
IName workspaceName = workspaceDataset.FullName;
// Retrieve the first feature dataset from the workspace.
IEnumDatasetName enumDatasetName = workspace.get_DatasetNames
(esriDatasetType.esriDTFeatureDataset);
enumDatasetName.Reset();
IName featureDatasetName = (IName)enumDatasetName.Next();
if (featureDatasetName == null)
{
throw new Exception(
"No feature datasets exist in the specified geodatabase.");
}
// Create a new names enumerator and add the feature dataset name.
IEnumNameEdit enumNameEdit = new NamesEnumeratorClass();
enumNameEdit.Add(featureDatasetName);
IEnumName enumName = (IEnumName)enumNameEdit;
// Create a GeoDBDataTransfer object and create a name mapping.
IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass();
IEnumNameMapping enumNameMapping = null;
geoDBDataTransfer.GenerateNameMapping(enumName, workspaceName, out
enumNameMapping);
// Create an exporter and export the dataset with binary geometry, not compressed,
// and including metadata.
IGdbXmlExport gdbXmlExport = new GdbExporterClass();
gdbXmlExport.ExportDatasets(enumNameMapping, outputXmlFile, true, false, true);
}
4.How to import a dataset from XML
private void ImportXmlWorkspaceDocument(String fileGdbPath, String workspaceDocPath)
{
// Open the target file geodatabase and create a name object for it.
Type factoryType = Type.GetTypeFromProgID(
"esriDataSourcesGDB.FileGDBWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
(factoryType);
IWorkspace workspace = workspaceFactory.OpenFromFile(fileGdbPath, 0);
IDataset workspaceDataset = (IDataset)workspace;
IName workspaceName = workspaceDataset.FullName;
// Create a GdbImporter and use it to generate name mappings.
IGdbXmlImport gdbXmlImport = new GdbImporterClass();
IEnumNameMapping enumNameMapping = null;
Boolean conflictsFound = gdbXmlImport.GenerateNameMapping(workspaceDocPath,
workspace, out enumNameMapping);
// Check for conflicts.
if (conflictsFound)
{
// Iterate through each name mapping.
INameMapping nameMapping = null;
enumNameMapping.Reset();
while ((nameMapping = enumNameMapping.Next()) != null)
{
// Resolve the mapping's conflict (if there is one).
if (nameMapping.NameConflicts)
{
nameMapping.TargetName = nameMapping.GetSuggestedName(workspaceName);
}
// See if the mapping's children have conflicts.
IEnumNameMapping childEnumNameMapping = nameMapping.Children;
if (childEnumNameMapping != null)
{
childEnumNameMapping.Reset();
// Iterate through each child mapping.
INameMapping childNameMapping = null;
while ((childNameMapping = childEnumNameMapping.Next()) != null)
{
if (childNameMapping.NameConflicts)
{
childNameMapping.TargetName =
childNameMapping.GetSuggestedName(workspaceName);
}
}
}
}
}
// Import the workspace document, including both schema and data.
gdbXmlImport.ImportWorkspace(workspaceDocPath, enumNameMapping, workspace, false)
;
}