// Passing zero values for all three double parameters recalculates the spatial index with
// acceptable (but not necessarily optimal) values.public void RebuildSpatialIndex(IFeatureClass featureClass, Double gridOneSize, Double gridTwoSize, Double gridThreeSize){ // Get an enumerator for indexes based on the shape field. IIndexes indexes = featureClass.Indexes; String shapeFieldName = featureClass.ShapeFieldName; IEnumIndex enumIndex = indexes.FindIndexesByFieldName(shapeFieldName); enumIndex.Reset(); // Get the index based on the shape field (should only be one) and delete it. IIndex index = enumIndex.Next(); if (index != null) { featureClass.DeleteIndex(index); } // Clone the shape field from the feature class. int shapeFieldIndex = featureClass.FindField(shapeFieldName); IFields fields = featureClass.Fields; IField sourceField = fields.get_Field(shapeFieldIndex); IClone sourceFieldClone = (IClone)sourceField; IClone targetFieldClone = sourceFieldClone.Clone(); IField targetField = (IField)targetFieldClone; // Open the geometry definition from the cloned field and modify it. IGeometryDef geometryDef = targetField.GeometryDef; IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; geometryDefEdit.GridCount_2 = 3; geometryDefEdit.set_GridSize(0, gridOneSize); geometryDefEdit.set_GridSize(1, gridTwoSize); geometryDefEdit.set_GridSize(2, gridThreeSize); // Create a spatial index and set the required attributes. IIndex newIndex = new IndexClass(); IIndexEdit newIndexEdit = (IIndexEdit)newIndex; newIndexEdit.Name_2 = String.Concat(shapeFieldName, "_Index"); newIndexEdit.IsAscending_2 = true; newIndexEdit.IsUnique_2 =false; // Create a fields collection and assign it to the new index. IFields newIndexFields = new FieldsClass(); IFieldsEdit newIndexFieldsEdit = (IFieldsEdit)newIndexFields; newIndexFieldsEdit.AddField(targetField); newIndexEdit.Fields_2 = newIndexFields; // Add the spatial index back into the feature class. featureClass.AddIndex(newIndex);}
[VB.NET]
' Passing zero values for all three double parameters recalculates the spatial index with' acceptable (but not necessarily optimal) values.Public Sub RebuildSpatialIndex(ByVal featureClass As IFeatureClass, ByVal gridOneSize As Double, ByVal gridTwoSize AsDouble, ByVal gridThreeSize As Double) ' Get an enumerator for indexes based on the shape field. Dim indexes As IIndexes = featureClass.Indexes Dim shapeFieldName As String = featureClass.ShapeFieldName Dim enumIndex As IEnumIndex = indexes.FindIndexesByFieldName(shapeFieldName) enumIndex.Reset() ' Get the index based on the shape field (should only be one) and delete it. Dim index As IIndex = enumIndex.Next() If Not index Is Nothing Then featureClass.DeleteIndex(index) End If ' Clone the shape field from the feature class. Dim shapeFieldIndex As Integer = featureClass.FindField(shapeFieldName) Dimfields As IFields = featureClass.Fields Dim sourceField As IField = fields.Field(shapeFieldIndex) Dim sourceFieldClone AsIClone = CType(sourceField, IClone) Dim targetFieldClone As IClone = sourceFieldClone.Clone() Dim targetField As IField =CType(targetFieldClone, IField) ' Open the geometry definition from the cloned field and modify it. Dim geometryDef AsIGeometryDef = targetField.GeometryDef Dim geometryDefEdit As IGeometryDefEdit = CType(geometryDef, IGeometryDefEdit) geometryDefEdit.GridCount_2 = 3 geometryDefEdit.GridSize_2(0) = gridOneSize geometryDefEdit.GridSize_2(1) = gridTwoSize geometryDefEdit.GridSize_2(2) = gridThreeSize ' Create a spatial index and set the required attributes. Dim newIndex As IIndex = New IndexClass() Dim newIndexEdit As IIndexEdit = CType(newIndex, IIndexEdit) newIndexEdit.Name_2 =String.Concat(shapeFieldName, "_Index") newIndexEdit.IsAscending_2 = True newIndexEdit.IsUnique_2 = False ' Create a fields collection and assign it to the new index. Dim newIndexFields As IFields = New FieldsClass() Dim newIndexFieldsEdit AsIFieldsEdit = CType(newIndexFields, IFieldsEdit) newIndexFieldsEdit.AddField(targetField) newIndexEdit.Fields_2 = newIndexFields ' Add the spatial index back into the feature class. featureClass.AddIndex(newIndex) End Sub
|
Rebuild Spatial Index
最新推荐文章于 2024-02-04 05:27:36 发布