shp转featureclass

来源:互联网 发布:安卓版cf刷枪软件 编辑:程序博客网 时间:2024/06/05 08:40
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);    }}

0 0
原创粉丝点击