This project is read-only.

Fastest way to calculate line feature length?

Jun 26, 2015 at 11:48 AM
Edited Jun 26, 2015 at 11:49 AM
Hi,
What would be the fastest way to calculate the length of a line shape if it is not already calculated and stored in the datatable? I have a case with 400 000 line shapes and want to calculate the length of each. It takes about three minutes which seems a bit long to me. If a shape length column exists it only takes 9 seconds, which is obviously the time for opening the shape file, iterate over the features and cast each length value to a double.

So for the case where no shape_len column exists I have tried two alternatives. One is to use
foreach (var f in fs.Features)
{
   var length = feature.ToShape().ToGeometry().Length,
....do something
}
The other way I've tried is to use a custom function that iterates over the coordinates and uses coordinate.Distance(otherCoordinate) to calculate the distance for each line segment and summarizes them.

The second method is sligthly faster, about ten percent. So the casting to ToShape() and ToGeometry() has some cost I guess.

Is there a faster way?