# Calculate distance of point along a line using NetTopologySuite

I am looking to calculate the distance along a line of a point using NetTopologySuite.

Indications are that the distanceOp may have something to do with it, given that I can a point on a line closest to another geometry. However how to determine the distance along the line of that point? I can certainly determine what that distance is myself through some code - but I would have through NTS would already have this covered off.

Anyone have any pointers?

Thanks,

Matt

-

LinearReferencing.LengthLocationMap will get you the distance of point along a line.

Computes the LinearLocation for a given length along a linear Geometry.Negative lengths are measured in reverse from end of the linear geometry. Out-of-range values are clamped.

``````using NetTopologySuite.LinearReferencing;

string wkt = "LINESTRING (0 0, 10 0, 10 10)";

ILineString linearGeometry = (ILineString)g1;
Coordinate pt = new Coordinate(10,5);
LinearLocation linearLocation = LocationIndexOfPoint.IndexOf(linearGeometry, pt);
double length = LengthLocationMap.GetLength(linearGeometry, linearLocation);
``````
-

I don't use the JTS/NTS APIs much myself but I would look at the `LengthIndexedLine` class:

Supports linear referencing along a linear `Geometry` using the length along the line as the index. Negative length values are taken as measured in the reverse direction from the end of the geometry. Out-of-range index values are handled by clamping them to the valid range of values. Non-simple lines (i.e. which loop back to cross or touch themselves) are supported.

Particularly, look at the `project` method:

Computes the index for the closest point on the line to the given point. If more than one point has the closest distance the first one along the line is returned. (The point does not necessarily have to lie precisely on the line.)

I am assuming that the value returned is the actual length along the line to that point. I could be off base though, as I haven't tried it.

-