Thursday 30 November 2017

arcobjects - "Item not found in this collection" when trying to set the Shape on IFeatureBuffer


I'm working on code to open a 3rd party data file and convert it into a collection of features in a custom layer based on FeatureLayer. I have had no issue importing the data itself (nodal data for each 'cell' as a feature, such as elevation).


Because the data files are on average rather large (10k+ cells), I'm using a cursor implementation with an IFeatureBuffer that is filled with each cell one at a time. However, when I try to set the geometry of the cell via IFeatureBuffer like so,


IGeometry cellGeometry = getCellGeometry(cell);



featureBuffer.Shape = cellGeometry;


I subsequently get the error "Item not found in this collection" called on the second line. My first thought was that I was creating the cell geometry incorrectly in some way, so I tested it by creating a simple point as the geometry. This failed with the same error. I next thought it might be an issue with ZAware. I tried setting it to both ((IZAware)cellGeometry).ZAware = true; and ((IZAware)cellGeometry).ZAware = false; with the same issue both times.


I'm thinking it might have to do with how the fields in the FeatureClass were setup, but I'm quite stuck at the moment.


UPDATE:


I've tried updating how the fields are created to fix any issues that may exist therein, and the issue remains. The fields are generated like so,


    {
if (this.ImportedField == null)
{
return null;
}


IObjectClassDescription fcDesc;
if (this.DatasetType == esriDatasetType.esriDTTable)
fcDesc = new ObjectClassDescriptionClass();
else
fcDesc = new FeatureClassDescriptionClass();

IFields fields = fcDesc.RequiredFields;
IFieldsEdit fieldsEdit = (IFieldsEdit)fields;


//Setup shape field
IField shapeField = fields.Field[fields.FindField("Shape")];
IFieldEdit shapeFieldEdit = (IFieldEdit)shapeField;
shapeFieldEdit.Name_2 = "SHAPE";
shapeFieldEdit.AliasName_2 = "SHAPE";
shapeFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
shapeFieldEdit.Length_2 = 0;
shapeFieldEdit.Precision_2 = 0;
shapeFieldEdit.Scale_2 = 0;
shapeFieldEdit.IsNullable_2 = true;

shapeFieldEdit.Required_2 = true;
shapeFieldEdit.Editable_2 = true;
shapeFieldEdit.Domain_2 = null;
shapeFieldEdit.DomainFixed_2 = true;
shapeFieldEdit.GeometryDef_2 = BuildGeometryDefinition(BuildGenericSpatialReference(true), esriGeometryType.esriGeometryPolygon, true, false);

var fieldNames = this.ImportedField.CoordinateData.ScalarData.Select(nodeData => nodeData.Name).ToList();
fieldNames.AddRange(this.ImportedField.CellData.ScalarData.Select(cellData => cellData.Name));

//Add fields from ImportedField

foreach (var fieldName in fieldNames)
{
IFieldEdit fieldEdit = new FieldClass();
fieldEdit.Length_2 = 1;
fieldEdit.Name_2 = fieldName;
fieldEdit.Type_2 = esriFieldType.esriFieldTypeSingle;
fieldsEdit.AddField(fieldEdit);
}

return fields as IFields;

}

Answer



Finally found the solution. For whatever reason, the Shape field must be set before any of the other fields.


This does not work:


IGeometry cellGeometry = getCellGeometry(cell);
featureBuffer.Shape = cellGeometry;

But for some reason this does:


featureBuffer.Shape = cellGeometry;
IGeometry cellGeometry = getCellGeometry(cell);

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...