Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm relatively new to programming and ArcObjects. I'm trying to simply calculate the length of a line (test.shp) using ArcObjects and Java. I've tried the code below with no luck. This is simple in ArcMap because one just needs to use 'calculate geometry' on the field within the attribute table. But I need code to do this.

Any suggestions would be greatly appreciated!

FeatureClass fc1 = null;        
ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory(); Workspace shapefileWorkspace = new Workspace(shapefileWorkspaceFactory.openFromFile("c:/PFRAS/TEMP", 0));              
fc1 = new FeatureClass(shapefileWorkspace.openFeatureClass("test.shp"));

Field fe = new Field();
fe.setName("Dist");
fe.setType(esriFieldType.esriFieldTypeDouble);
fe.setScale(2);
fe.setPrecision(16);
fc1.addField(fe);

IFeature feature = fc1.getFeature(0);
Line line = new Line();

//Assume that the underlying feature is a Line.
IGeometry geom = feature.getShape();
    if (geom instanceof Line)
    line = (Line)geom;              
    double length = line.getLength();

int fieldIndex = fc1.findField("Dist");

FeatureCursor fCursor = new FeatureCursor(fc1.IFeatureClass_update(null, false));
        Feature feature2 = (Feature) fCursor.nextFeature();
        while (feature2 != null) 
        {                 
         feature2.setValue(fieldIndex, length);
         fCursor.updateFeature(feature2);
         feature2 = (Feature) fCursor.nextFeature();  
        }
        fCursor.flush();
        Cleaner.release(fCursor);
share|improve this question
    
On your .openFeatureClass("test.shp"), are you defined full path name to .shp e.g. .openFeatureClass("C:\\temp\test.shp")? –  artwork21 Mar 12 '13 at 12:23
    
I assume what I have here does work because I replaced the "length" variable in the .setValue method with a double value (e.g. 5.0) and that worked. I believe the prooblem is where I attempt to calculate the line length and assign it to "length" variable. –  Andrew15 Mar 12 '13 at 12:42
    
Do you know what line the error occurs on? –  artwork21 Mar 12 '13 at 13:29
    
I know it is within the part of the code pasted below. I'm adding this in to ArcMap as an ESRI Add-In so I don't get error reports. IFeature feature = fc1.getFeature(0); Line line = new Line(); //Assume that the underlying feature is a Line. IGeometry geom = feature.getShape(); if (geom instanceof Line) line = (Line)geom; double length = line.getLength(); –  Andrew15 Mar 12 '13 at 13:31
    
Have you looked at ICurve interface help.arcgis.com/en/sdk/10.0/java_ao_adf/api/arcobjects/com/esri/…, getLength() method? –  artwork21 Mar 12 '13 at 13:37
show 1 more comment

1 Answer

up vote 0 down vote accepted

I did solve this problem after using the ICurve .getLength() method as suggested by artwork21 user. Here is the code.

        FeatureClass fc1 = null;        
        ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory();    
        Workspace shapefileWorkspace = new Workspace(shapefileWorkspaceFactory.openFromFile("c:/TEMP", 0));           
        fc1 = new FeatureClass(shapefileWorkspace.openFeatureClass("test.shp"));

        //Create cumulative distance field using Field class
        Field fe = new Field();
        fe.setName("Cum_Dist");
        fe.setType(esriFieldType.esriFieldTypeDouble);
        fe.setScale(2);
        fe.setPrecision(16);
        fc1.addField(fe);       

        //Create field index 
        int fieldIndex = fc1.findField("Cum_Dist");     

        //Get reference to the line feature and calculate length using ICurve interface
        IFeature feature = fc1.getFeature(0);
        ICurve curve = (ICurve)feature.getShape();
        double length = curve.getLength();

        //Use a FeatureCursor to addign the length value to the field       
        FeatureCursor fCursor = new FeatureCursor(fc1.IFeatureClass_update(null, false));
        Feature feature2 = (Feature) fCursor.nextFeature();
        while (feature2 != null) 
        {
            feature2.setValue(fieldIndex, length);
            fCursor.updateFeature(feature2);
            feature2 = (Feature) fCursor.nextFeature();
        }

        fCursor.flush();
        Cleaner.release(fCursor);   
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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