shp转featureclass

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值