Thursday 19 January 2017

arcgis 10.0 - Programmatically calculating the length of all polyline features in a featureclass


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

Answer



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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...