frmap1

Add an Index to a feature Class

Discussion created by frmap1 on Apr 21, 2013
Latest reply on Apr 23, 2013 by mbaird-esristaff
Hi,
I want to add an index to a feature class. To do so, I use the very short and complete example given there:
http://resources.esri.com/help/9.3/arcgisengine/java/doc/b22267cb-642c-11dc-9ca3-0b35f906bb2e.htm

When I use this code, I obtain an exception due to a Castproblem:
It cannot cast from a IFeatureClass to a ISchemaLock:
Exception in thread "main" java.lang.ClassCastException: com.esri.arcgis.geodatabase.IFeatureClassProxy cannot be cast to com.esri.arcgis.geodatabase.ISchemaLock

Are you able to run this code sucessfully ?

You have to change the 2 first inputs in the code (the path and name of your shapefile)


import java.io.IOException;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.Fields;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IField;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.geodatabase.IFieldsEdit;
import com.esri.arcgis.geodatabase.IIndex;
import com.esri.arcgis.geodatabase.IIndexEdit;
import com.esri.arcgis.geodatabase.ISchemaLock;
import com.esri.arcgis.geodatabase.IWorkspace;
import com.esri.arcgis.geodatabase.IWorkspaceFactory;
import com.esri.arcgis.geodatabase.Index;
import com.esri.arcgis.geodatabase.esriSchemaLock;


public class classmain
{
 public static void main(String[] args)
    {
            
            try
            {
            // >>> Enter here the path and the name of your shapefile.
            String folderPath = "D:\\ShapeFolder\\test";
            String shapefileName = "myShape_022";

            // Step 1: Initialize the Java Component Object Model (COM) Interop.
            EngineInitializer.initializeEngine();

            // Step 2: Initialize an ArcGIS license.
            AoInitialize aoInit = new AoInitialize();
            aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
 
            // Load the  ShapeFile into "featureClass"
            IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspace workspace = workspaceFactory.openFromFile(folderPath, 0);
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
            IFeatureClass featureClass = featureWorkspace.openFeatureClass(shapefileName);;

            // Find the field "FID" in featureClass that will be used as an index
            int fieldIndex = featureClass.findField("FID");
   
            // Get the specified field from the feature class.
            IFields featureClassFields = featureClass.getFields();
            IField field = featureClassFields.getField(fieldIndex);
            
            // Create a new fields collection and add the specified field to it.
            IFields fields = new Fields();
            IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
            fieldsEdit.setFieldCount(1);
            fieldsEdit.setFieldByRef(0, field);
            
            //Create a new index and cast to the IIndexEdit interface.
            IIndex index = new Index();
            IIndexEdit indexEdit = (IIndexEdit)index;
            
            // Set the index's properties, including the fields it will have associated with it.
            indexEdit.setFieldsByRef(fields);
            indexEdit.setIsAscending(false);
            indexEdit.setIsUnique(false);
            indexEdit.setName("IndexFID");

            //Attempt to acquire an exclusive schema lock on the feature class.
            ISchemaLock schemaLock = (ISchemaLock)featureClass;
            schemaLock.changeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
            featureClass.addIndex(index);
            schemaLock.changeSchemaLock(esriSchemaLock.esriSharedSchemaLock);

            //  Disconnect licences 
            aoInit.shutdown();
        }
        catch (IOException ex)
        {
            System.out.println(ex.getMessage());
            System.out.println("App failed.");
        }
    } //End of method main.
} //End of class:EngineHelloWorld.

Outcomes