Add a Raster field to a feature class [ArcObjects 10]

405
2
Jump to solution
07-26-2012 06:08 AM
AkhilParujanwala
New Contributor III
I am trying to create a feature class using C# and ArcObjects and it needs to have a raster field. However I am getting an error when I try to create the feature class.

Here is a snippet of the code I am using.

public IFields CreateFields(ISpatialReference spatialReference)         {             IFields fields = new FieldsClass();             IFieldsEdit fieldsEdit = (IFieldsEdit)fields;             fieldsEdit.FieldCount_2 = 3;                          //Create the Object ID field.             IField fieldUserDefined = new Field();             IFieldEdit fieldEdit = (IFieldEdit)fieldUserDefined;             fieldEdit.Name_2 = "OBJECTID";             fieldEdit.AliasName_2 = "OBJECT ID";             fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;             fieldsEdit.set_Field(0, fieldUserDefined);              // Create the Shape field.             fieldUserDefined = new Field();             fieldEdit = (IFieldEdit)fieldUserDefined;             // Set up the geometry definition for the Shape field.             IGeometryDef geometryDef = new GeometryDefClass();             IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;             geometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint;             // By setting the grid size to 0, you're allowing ArcGIS to determine the appropriate grid sizes for the feature class.              // If in a personal geodatabase, the grid size will be 1000. If in a file or ArcSDE geodatabase, the grid size             // will be based on the initial loading or inserting of features.             geometryDefEdit.GridCount_2 = 1;             geometryDefEdit.set_GridSize(0, 0);             geometryDefEdit.HasM_2 = false;             geometryDefEdit.HasZ_2 = false;             //Assign the spatial reference that was passed in, possibly from             //IGeodatabase.SpatialReference for the containing feature dataset.             if (spatialReference != null)             {                 geometryDefEdit.SpatialReference_2 = spatialReference;             }              // Set standard field properties.             fieldEdit.Name_2 = "SHAPE";             fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;             fieldEdit.GeometryDef_2 = geometryDef;             fieldEdit.IsNullable_2 = true;             fieldEdit.Required_2 = true;             fieldsEdit.set_Field(1, fieldUserDefined);              fieldUserDefined = new Field();             fieldEdit = (IFieldEdit)fieldUserDefined;             fieldEdit.Name_2 = "PHOTO";             fieldEdit.AliasName_2 = "Attach a Photo";             fieldEdit.Editable_2 = true;             fieldEdit.IsNullable_2 = false;             fieldEdit.Type_2 = esriFieldType.esriFieldTypeRaster;             fieldsEdit.set_Field(2, fieldUserDefined);


After the fields have been defined, the following method is called to create the feature class.

public IFeatureClass CreateFeatureDatasetFeatureClass(IFeatureDataset featureDataset, String featureClassName, IFields fieldsCollection, String shapeFieldName)         {             IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass();             IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc;              // Use IFieldChecker to create a validated fields collection.             IFieldChecker fieldChecker = new FieldCheckerClass();             IEnumFieldError enumFieldError = null;             IFields validatedFields = null;             fieldChecker.ValidateWorkspace = featureDataset.Workspace;             fieldChecker.Validate(fieldsCollection, out enumFieldError, out validatedFields);              // The enumFieldError enumerator can be inspected at this point to determine              // which fields were modified during validation.             IFeatureClass featureClass = featureDataset.CreateFeatureClass(featureClassName,                 validatedFields, ocDesc.InstanceCLSID, ocDesc.ClassExtensionCLSID,                 esriFeatureType.esriFTSimple, fcDesc.ShapeFieldName, "");             return featureClass;         }


On the last line of code I  get an error: Value does not fall within the expected range.
If I change the code to create the field,
from :
fieldEdit.Type_2 = esriFieldType.esriFieldTypeRaster;
to:
fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;

It works. But I obviously don't want a string field, I would like to have a raster field.

Can someone please help me out, that would be greatly appreciated.

Thanks!
0 Kudos
1 Solution

Accepted Solutions
FengZhang2
Occasional Contributor
RasterDef is required for a raster field. Please look into 'Remarks' section of the following help file.
-- IFieldEdit2 Interface --
http://help.arcgis.com/en/sdk/10.0/arcobjects_cpp/componenthelp/index.html#//000s00000348000000

// Create a RasterDef. IRasterDef rasterDef = new RasterDefClass(); rasterDef.Description = "Raster Field"; rasterDef.IsManaged = true; rasterDef.SpatialReference = spatialRef;  // Create a raster field. IField field = new FieldClass(); IFieldEdit2 fieldEdit2 = (IFieldEdit2)field; fieldEdit2.Name_2 = "Raster"; fieldEdit2.Type_2 = esriFieldType.esriFieldTypeRaster; fieldEdit2.RasterDef = rasterDef;  // Add the field to the feature class. featureClass.AddField(field);

View solution in original post

0 Kudos
2 Replies
FengZhang2
Occasional Contributor
RasterDef is required for a raster field. Please look into 'Remarks' section of the following help file.
-- IFieldEdit2 Interface --
http://help.arcgis.com/en/sdk/10.0/arcobjects_cpp/componenthelp/index.html#//000s00000348000000

// Create a RasterDef. IRasterDef rasterDef = new RasterDefClass(); rasterDef.Description = "Raster Field"; rasterDef.IsManaged = true; rasterDef.SpatialReference = spatialRef;  // Create a raster field. IField field = new FieldClass(); IFieldEdit2 fieldEdit2 = (IFieldEdit2)field; fieldEdit2.Name_2 = "Raster"; fieldEdit2.Type_2 = esriFieldType.esriFieldTypeRaster; fieldEdit2.RasterDef = rasterDef;  // Add the field to the feature class. featureClass.AddField(field);
0 Kudos
AkhilParujanwala
New Contributor III
Perfect thank you!
I didn't notice the IFieldEdit2 interface, and I did have a funny feeling I needed a raster definition, I just didn't know how.

Thanks again!
0 Kudos