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've come across this issue a few times and have not yet found an agreed upon solution on any forum.

Simply put, I have a featureclass containing many polyline features. I would like to calculate the lengths of each feature and write that value to a new field, which I have already added to the featureclass.

I know that I can export the featureclass to a geodatabase and have the length calculated automatically, but that doesn't make much sense in terms of programmatic workflow. It seems like I might be missing something simple, but there are obviously others out there with the same unanswered question.

EDIT: I should have been more clear. I would like to carry this out using ArcObjects. The field calculator method that keeps coming up does not appear to work for this purpose.

EDIT 2: I've sorted out one method for doing this using ArcObjects. Solution/Code is below. Thanks for all the help and suggestions.

The following code assumes you are operating on IFeatureClass and have already added a field called "Length" to the existing FeatureClass (which contains a series of polyline Features). I have tested it on a shapefile containing about 300 polyline Features.

            Dim nLengthFieldIndex As Integer = pFCXS.FindField("Length")
            Debug.Assert(nLengthFieldIndex > -1)
            Dim dLength As Double
            If TypeOf pFC Is IFeatureClass Then
                Dim pFeatureCursor As IFeatureCursor
                pFeatureCursor = pFC.Search(Nothing, False)
                If TypeOf pFeatureCursor Is IFeatureCursor Then
                    Dim pFeature As IFeature = pFeatureCursor.NextFeature()
                    While pFeature IsNot Nothing
                        Dim pLine As IPolyline
                        pLine = pFeature.Shape
                        If TypeOf pLine Is IPolyline Then
                            dLength = pLine.Length
                            pFeature.Value(nLengthFieldIndex) = dLength
                            pFeature.Store()
                            pFeature = pFeatureCursor.NextFeature
                        End If
                    End While
                End If
                pFeatureCursor.Flush()
            End If
share|improve this question
1  
This can easily be done with arcobjects however the problem you will have is that these length values will be static. If you modify the feature you will need to recalculate. Not so in a geodatabase. You can even get stats like sum average ect on the length field. –  Jakub Jul 22 '11 at 2:18
    
I think the question should also be what is the best method to calculate distances of polylines. Is there a best method, in terms of what projection to store the data and and what projection to calculate the data? <a href="gis.stackexchange.com/questions/10257/… asked question</a> My data is stored in NAD83 and I am working on a python script to loop through each record, projecting the data to their respective state plane then calculating the footage (often there are crossing multiple state planes and for this it is coded). Any th –  Justin Jul 22 '11 at 5:51

5 Answers 5

You can use Field Calculator for the same.
See the code snippet below.
Dim dblLength as double
Dim pCurve as ICurve
Set pCurve = [shape]
dblLength = pCurve.Length

Refer the online link for more information.

Cheers!

share|improve this answer

You can also use the calculate geometry tool in tables.

share|improve this answer

Get a reference to the geometry from each individual feature, cast to ICurve, and use the Length property to get the length in the linear unit of the feature's projected coordinate system. Project to a different coordinate system first if the feature class isn't in a suitable spatial reference already. In C#:

IFeature feat = ...;
ICurve curve = feat.Shape as ICurve;
double length = curve.Length;
share|improve this answer
up vote 0 down vote accepted

I've sorted it out using ArcObjects (VB). Feel free to critique/comment/improve.

The following code assumes you are operating on IFeatureClass and have already added a field called "Length" to the existing FeatureClass (which contains a series of polyline Features). I have tested it on a shapefile containing about 300 polyline Features.

                Dim nLengthFieldIndex As Integer = pFCXS.FindField("Length")
                Debug.Assert(nLengthFieldIndex > -1)
                Dim dLength As Double
                If TypeOf pFC Is IFeatureClass Then
                    Dim pFeatureCursor As IFeatureCursor
                    pFeatureCursor = pFC.Search(Nothing, False)
                    If TypeOf pFeatureCursor Is IFeatureCursor Then
                        Dim pFeature As IFeature = pFeatureCursor.NextFeature()
                        While pFeature IsNot Nothing
                            Dim pLine As IPolyline
                            pLine = pFeature.Shape
                            If TypeOf pLine Is IPolyline Then
                                dLength = pLine.Length
                                pFeature.Value(nLengthFieldIndex) = dLength
                                pFeature.Store()
                                pFeature = pFeatureCursor.NextFeature
                            End If
                        End While
                    End If
                    pFeatureCursor.Flush()
                End If
share|improve this answer

You can take a look at ET Tools easycalculate. there is a new version for 10 and there are many functions that can be used in toolbox. It is free.

easy calculate 10

share|improve this answer

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.