ArcGis featureLayer中添加字段

featureLayer中添加字段

这是我2010年11月份写的东东,现在拿出来给大家分享一下,这几个方法都是抽象出来的,可以拿过来直接使用。

说明一下:这些都是.net平台,VS2008+ArcGis9.3调试通过的

//检查fields是否有效


public IFields ValidateFieldsForWorkspace(IFields fields, IWorkspace workspace)
{
    // Create a new field checker.
    IFieldChecker fieldChecker = new FieldCheckerClass();
    fieldChecker.ValidateWorkspace = workspace;

    // Validate the fields.
    IEnumFieldError enumFieldError = null;
    IFields validatedFields = null;
    fieldChecker.Validate(fields, out enumFieldError, out validatedFields);

    // Display the field errors.
    IFieldError fieldError = null;
    enumFieldError.Reset();
    while ((fieldError = enumFieldError.Next()) != null)
    {
        IField errorField = fields.get_Field(fieldError.FieldIndex);
        Console.WriteLine("Field '{0}': Error '{1}'", errorField.Name,
            fieldError.FieldError);
    }

    // Return the validated fields.
    return validatedFields;
}





//为要素类创建fields



public IFields CreateFieldsCollectionForFeatureClass(ISpatialReference
    spatialReference)
{
    // Use the feature class description to return the required fields in a fields collection.
    IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass();
    IObjectClassDescription cDesc = (IObjectClassDescription)fcDesc;

    // Create the fields using the required fields method.
    IFields fields = ocDesc.RequiredFields;

    // Locate the shape field with the name from the feature class description.
    int shapeFieldIndex = fields.FindField(fcDesc.ShapeFieldName);
    IField shapeField = fields.get_Field(shapeFieldIndex);

    // Modify the GeometryDef object before using the fields collection to create a new feature class.
    IGeometryDef geometryDef = shapeField.GeometryDef;
    IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;

    // Alter the feature class geometry type to lines (default is polygons).
    geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
    geometryDefEdit.HasM_2 = true;
    geometryDefEdit.GridCount_2 = 1;

    // Set the first grid size to zero and allow ArcGIS to determine a valid grid size.
    geometryDefEdit.set_GridSize(0, 0);
    geometryDefEdit.SpatialReference_2 = spatialReference;

    // Because the fields collection already exists, the AddField method on the IFieldsEdit interface
    // will be used to add a field that is not required to the fields collection.
    IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
    IField incomeField = new FieldClass();
    IFieldEdit incomeFieldEdit = (IFieldEdit)incomeField;

    // Create a user-defined double field.
    incomeFieldEdit.AliasName_2 = "Average income for 1999-2000";
    incomeFieldEdit.Editable_2 = true;
    incomeFieldEdit.IsNullable_2 = false;
    incomeFieldEdit.Name_2 = "average_income";
    incomeFieldEdit.Precision_2 = 2;
    incomeFieldEdit.Scale_2 = 5;
    incomeFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
    fieldsEdit.AddField(incomeField);

    return fields;
}





//为table创建fields



public IFields CreateFieldsCollectionForTable()
{
    // Create a new fields collection.
    IFields fields = new FieldsClass();

    // Cast to IFieldsEdit to modify the properties of the fields collection.
    IFieldsEdit fieldsEdit = (IFieldsEdit)fields;

    // Set the number of fields the collection will contain.
    fieldsEdit.FieldCount_2 = 2;

    // Create the ObjectID field.
    IField idField = new FieldClass();

    // Cast to IFieldEdit to modify the properties of the new field.
    IFieldEdit idFieldEdit = (IFieldEdit)oidField;
    oidFieldEdit.Name_2 = "ObjectID";
    oidFieldEdit.AliasName_2 = "FID";
    oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;

    // Add the new field to the fields collection.
    fieldsEdit.set_Field(0, oidField);

    // Create the text field.
    IField textField = new FieldClass();
    IFieldEdit textFieldEdit = (IFieldEdit)textField;
    textFieldEdit.Length_2 = 30;
        // Only string fields require that you set the length.
    textFieldEdit.Name_2 = "Owner";
    textFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;

    // Add the new field to the fields collection.
    fieldsEdit.set_Field(1, textField);

    return fields;
}






//向要素类添加fields字段


public void AddFieldToFeatureClass(IFeatureClass featureClass, IField field)
{
    ISchemaLock schemaLock = (ISchemaLock)featureClass;

    try
    {
        // A try block is necessary, as an exclusive lock may not be available.
        schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);

        // Add the field.
        featureClass.AddField(field);
    }
    catch (Exception exc)
    {
        // Handle this in a way appropriate to your application.
        Console.WriteLine(exc.Message);
    }
    finally
    {
        // Set the lock to shared, whether or not an error occurred.
        schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
    }
}

你可能感兴趣的:(exception,object,properties,null,Class,interface)