public static void Execute(IFeatureClass featureClass, String indexName, String nameOfField) { // Ensure the feature class contains the specified field. int fieldIndex = featureClass.FindField(nameOfField); if (fieldIndex == -1) { throw new ArgumentException("The specified field does not exist in the feature class."); } // Get the specified field from the feature class. IFields featureClassFields = featureClass.Fields; IField field = featureClassFields.get_Field(fieldIndex); // Create a fields collection and add the specified field to it. IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; fieldsEdit.FieldCount_2 = 1; fieldsEdit.set_Field(0, field); // Create an index and cast to the IIndexEdit interface. IIndex index = new IndexClass(); IIndexEdit indexEdit = (IIndexEdit)index; // Set the index's properties, including the associated fields. indexEdit.Fields_2 = fields; indexEdit.IsAscending_2 = false; indexEdit.IsUnique_2 = false; indexEdit.Name_2 = indexName; // Add the index to the feature class. featureClass.AddIndex(index); }
public static void createSpatialIndex(IFeatureClass fc, double gridOneSize = 0.0, double gridTwoSize = 0.0, double gridThreeSize = 0.0) { String shapeFieldName = fc.ShapeFieldName; // Clone the shape field from the feature class. int shapeFieldIndex = fc.FindField(shapeFieldName); IFields fields = fc.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; if (gridTwoSize > 0.0) { geometryDefEdit.GridCount_2 = 1; geometryDefEdit.set_GridSize(0, gridOneSize); } if (gridTwoSize > 0.0) { geometryDefEdit.GridCount_2 = 2; geometryDefEdit.set_GridSize(1, gridTwoSize); } if (gridThreeSize > 0.0) { geometryDefEdit.GridCount_2 = 3; 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 = shapeFieldName + "_Indx"; // 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. fc.AddIndex(newIndex); }
//--------------------------------------------------------------------- // CHANGED: CR13 (Export features performance) // Add a new attribute index to a feature class. /// <summary> /// Adds an attribute index to a field in a feature class. /// </summary> /// <param name="featureClass">The feature class.</param> /// <param name="indexName">Name of the index.</param> /// <param name="fieldName">Name of the field.</param> public void AddFieldIndex(IFeatureClass featureClass, String indexName, String fieldName) { // Ensure the feature class contains the specified field. int fieldIndex = featureClass.FindField(fieldName); if (fieldIndex == -1) return; // Get the specified field from the feature class. IFields featureClassFields = featureClass.Fields; IField field = featureClassFields.get_Field(fieldIndex); // Create a fields collection and add the specified field to it. IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; fieldsEdit.FieldCount_2 = 1; fieldsEdit.set_Field(0, field); //Create an index and cast to the IIndexEdit interface. IIndex index = new IndexClass(); IIndexEdit indexEdit = (IIndexEdit)index; // Set the index's properties, including the associated fields. indexEdit.Fields_2 = fields; indexEdit.IsAscending_2 = false; indexEdit.IsUnique_2 = false; indexEdit.Name_2 = indexName; // Attempt to acquire an exclusive schema lock on the feature class. ISchemaLock schemaLock = (ISchemaLock)featureClass; try { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); featureClass.AddIndex(index); } catch (COMException comExc) { // Handle this in a way appropriate to your application. Console.WriteLine("A COM Exception was thrown: {0}", comExc.Message); } finally { schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
/// <summary> /// 重建要素类空间索引 /// </summary> /// <param name="pFeatureClass"></param> /// <param name="gridOneSize"></param> /// <param name="gridTwoSize"></param> /// <param name="gridThreeSize"></param> /// <returns></returns> public bool RebuildSpatialIndex(IFeatureClass pFeatureClass, Double gridOneSize = 0, Double gridTwoSize = 0, Double gridThreeSize = 0) { ISchemaLock pSchemaLock = pFeatureClass as ISchemaLock; try { pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); // Get an enumerator for indexes based on the shape field. IIndexes indexes = pFeatureClass.Indexes; String shapeFieldName = pFeatureClass.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) { pFeatureClass.DeleteIndex(index); } // Clone the shape field from the feature class. int shapeFieldIndex = pFeatureClass.FindField(shapeFieldName); IFields fields = pFeatureClass.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 Index(); 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 Fields(); IFieldsEdit newIndexFieldsEdit = (IFieldsEdit)newIndexFields; newIndexFieldsEdit.AddField(targetField); newIndexEdit.Fields_2 = newIndexFields; // Add the spatial index back into the feature class. pFeatureClass.AddIndex(newIndex); return(true); } catch { return(false); } finally { pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
/// <summary> /// 初始化数据 /// </summary> public void InitData(object progress1) { ProgressBar progress = (ProgressBar)progress1; //耗时巨大的代码 // MainForm.baseData.InitData(progressBar1); ////////////////////////////////////////////////////////////////////////// //建立分区索引 IEnumIndex pEnumIndex = zoneLCA_FC.Indexes.FindIndexesByFieldName(zidField); IIndex pTemIndex = pEnumIndex.Next(); if (pTemIndex == null) { IIndex pIndex = new IndexClass(); IIndexEdit pIndexEdit = pIndex as IIndexEdit; IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; int zidindex = zoneLCA_FC.Fields.FindField(zidField); IField pField = zoneLCA_FC.Fields.Field[zidindex]; pFieldsEdit.FieldCount_2 = 1; pFieldsEdit.set_Field(0, pField); pIndexEdit.Fields_2 = pFields; pIndexEdit.Name_2 = zidField; pIndexEdit.IsAscending_2 = true; zoneLCA_FC.AddIndex(pIndex); } //建立分类索引 IEnumIndex pEnumIndex1 = zoneLCA_FC.Indexes.FindIndexesByFieldName(codeField); IIndex pTemIndex1 = pEnumIndex1.Next(); if (pTemIndex == null) { IIndex pIndex = new IndexClass(); IIndexEdit pIndexEdit = pIndex as IIndexEdit; IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; int codeindex = zoneLCA_FC.Fields.FindField(codeField); IField pField = zoneLCA_FC.Fields.Field[codeindex]; pFieldsEdit.FieldCount_2 = 1; pFieldsEdit.set_Field(0, pField); pIndexEdit.Fields_2 = pFields; pIndexEdit.Name_2 = codeField; pIndexEdit.IsAscending_2 = true; zoneLCA_FC.AddIndex(pIndex); } areaIndex = zoneLCA_FC.FindField("Shape_Area"); perimeterIndex = zoneLCA_FC.FindField("Shape_Length"); if (zone_FC != null) { areaIndex_zone = zone_FC.FindField("Shape_Area"); perimeterIndex_zone = zone_FC.FindField("Shape_Length"); } codeIndex = zoneLCA_FC.FindField(codeField); Utilities.Common.SetProgress(progress); IQueryDef pQueryDef; IRow pRow; ICursor pCursor; IWorkspace pWorkspace; IFeatureWorkspace pFeatureWorkspace; IDataset pDataset; pDataset = zoneLCA_FC as IDataset; pWorkspace = pDataset.Workspace; pFeatureWorkspace = pDataset.Workspace as IFeatureWorkspace; pQueryDef = pFeatureWorkspace.CreateQueryDef(); ////////////////////////////////////////////////////////////////////////// //添加分区唯一值 pQueryDef.SubFields = "DISTINCT(" + zidField + ")"; pQueryDef.Tables = pDataset.Name; pCursor = pQueryDef.Evaluate(); pRow = pCursor.NextRow(); while (pRow != null) { object obj = pRow.get_Value(0); zoneValue.Add(obj.ToString()); pRow = pCursor.NextRow(); if (progress.Value < progress.Maximum) { progress.Value++; } } ////////////////////////////////////////////////////////////////////////// //添加 分类唯一值 pQueryDef.SubFields = "DISTINCT(" + codeField + ")"; pQueryDef.Tables = pDataset.Name; pCursor = pQueryDef.Evaluate(); pRow = pCursor.NextRow(); while (pRow != null) { object obj = pRow.get_Value(0); ccValue.Add(obj.ToString()); pRow = pCursor.NextRow(); if (progress.Value < progress.Maximum) { progress.Value++; } } if (zone_FC != null) { string objectFiled = ""; objectFiled = zone_FC.Fields.Field[0].Name; pQueryDef.SubFields = (zone_FC as IDataset).Name + "." + objectFiled; //pQueryDef.SubFields = pDataset.Name + "." + zidField + "," + (zone_FC as IDataset).Name + "." + zidField + "," + (zone_FC as IDataset).Name + ".OBJECTID"; pQueryDef.Tables = (zone_FC as IDataset).Name; for (int i = 0; i < zoneValue.Count; i++) { pQueryDef.WhereClause = zidField + " = " + "'" + zoneValue[i] + "'"; pCursor = pQueryDef.Evaluate(); pRow = pCursor.NextRow(); while (pRow != null) { object obj1 = pRow.get_Value(0); // zoneValue.Add(obj.ToString()); zoneObjectID.Add((int)obj1); pRow = pCursor.NextRow(); if (progress.Value < progress.Maximum) { progress.Value++; } } } } string objectFiled1 = ""; objectFiled1 = zoneLCA_FC.Fields.Field[0].Name; ///////////////////////// //1 pQueryDef.SubFields = objectFiled1 + ",Shape_Area"; pQueryDef.Tables = pDataset.Name; for (int i = 0; i < zoneValue.Count; i++) { double area = 0.0; pQueryDef.WhereClause = zidField + "=\'" + zoneValue[i] + "\'"; pCursor = pQueryDef.Evaluate(); pRow = pCursor.NextRow(); List <int> pids = new List <int>(); while (pRow != null) { area += (double)pRow.get_Value(1); pids.Add((int)pRow.get_Value(0)); pRow = pCursor.NextRow(); // progress.Invoke((MethodInvoker)delegate() // { // if (progress.Value < progress.Maximum) // { // progress.Value++; // } // }); if (progress.Value < progress.Maximum) { progress.Value++; } } zoneArea.Add(area); patchIDs.Add(pids); } for (int i = 0; i < patchIDs.Count; i++) { patchIDArray.Add(GetZonePids(i)); // progress.Invoke((MethodInvoker)delegate() // { // if (progress.Value < progress.Maximum) // { // progress.Value++; // } // }); if (progress.Value < progress.Maximum) { progress.Value++; } } }