1

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);
artwork21
35.2k8 gold badges69 silver badges135 bronze badges
asked Mar 12, 2013 at 12:17
6
  • On your .openFeatureClass("test.shp"), are you defined full path name to .shp e.g. .openFeatureClass("C:\\temp\test.shp")? Commented Mar 12, 2013 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. Commented Mar 12, 2013 at 12:42
  • Do you know what line the error occurs on? Commented Mar 12, 2013 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(); Commented Mar 12, 2013 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? Commented Mar 12, 2013 at 13:37

1 Answer 1

0

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); 
answered Mar 15, 2013 at 12:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.