2

I want to add an index to a feature class. To do so, I used the very short and complete example given there: http://resources.esri.com/help/9.3/arcgisengine/java/doc/b22267cb-642c-11dc-9ca3-0b35f906bb2e.htm

My modified code is below. When I use that code, I obtain an exception due to a Cast problem; 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 without the error ?

If you want to run this code, you have to change the 2 first string 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.");
    }
} 
} 
  • try to give specific method. people don't like to see huge code segment. – Emi Apr 23 '13 at 7:10
2

Ihad the same problem and I resolved it by casting featureClass from IFeatureClass to FeatureClass:

IFeatureClass featureClass = featureWorkspace.openFeatureClass(shapefileName);
FeatureClass featureClass2= new FeatureClass(featureClass);
.....
//Attempt to acquire an exclusive schema lock on the feature class.
  ISchemaLock schemaLock = (ISchemaLock)featureClass2;
.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.