# Get points, along a polyline, at defined distance

I need to store into an array the points, along a polyline, at each defined distance.

Example: A polyline length is 50m, I set the interval to 5m. I have to store in the array all the points along the polyline each 5 meter. (points on 0m-5m-10m-15m....).

I'm using C#. Any function to get these points?

• C# does not have such a function. Are you programming against some GIS? – Olivier Jacot-Descombes Jun 9 '14 at 16:36
• Welcome to GSE. Please edit your question to include the GIS software are you using and the coordinate system in which the coordinate data is stored. If the data is in a projected coordinate system, it's a trivial function to use basic trigonometry to compute an angle and create as many vertices as you want (no GIS needed). – Vince Jun 9 '14 at 16:37

A possible algorithm in pseudo code (as I don't know how your polyline is implemented):

``````var pointList = new List<Point>();
double polyLineLength = polyline.GetTotalLength();
double pointDist = 5.0;
int numDist = (int)(polyLineLength / pointDist);

double pointPosition = 0.0;
double prevSegmentsLength = 0.0;
double segmentsLength = 0.0;
int currentSegment = 0;
Segment segment = polyline[0];

for (int i = 0; i <= numDist; i++)
{
while (pointPosition > segmentsLength) {
prevSegmentsLength = segmentsLength;
segment = polyline.Segment[currentSegment];
segmentsLength += segment.Length;
currentSegment++;
}
var point = Interpolate(segment, pointPosition - prevSegmentsLength);
pointPosition += pointDist;
}
``````

`pointPosition`           is the distance from the beginning of the polyline to the current point.
`prevSegmentsLength` is the sum of the lengths of the preceeding polyline segements.
`segmentsLength`         is the sum of the lengths of the polyline segements including the length of the current segment.

The idea is to walk along the polyline for each point until you find the segment on which the point lies. Then interpolate the point from the two end points of the segment.

The difficulty is that several points can lie on one segment, but is is also possible that several segements lie between two points.

Interpolation:

If the segment is defined as (x0, y0) - (x1, y1) you can interpolate like this:

``````dx = x1 - x0
dy = y1 - y0
l = Sqrt(dx^2 + dy^2)
x = x0 + dx/l * t
y = y0 + dy/l * t
``````

where `t` is the distance of the point along the segment from (x0, y0).

Assuming you are using ArcGIS, convert your line features to Linear Referenced Routes using the Create Route tool and setting the measures to use length in meters. All of the measure interpolation between nodes is done for you by an event table without any code just by providing a unique RouteID for the line you want and a measure value in meters in the records of a separate event table.

Create a unique ID for each set of lines you want to function as a single route (or just calc the ObjectID into a long field and use it for each individual line). Then create an event table with the Route (or Routes) IDs and a record for each successive 5 meter interval for as long as you want. When you make the event table into a point layer with the Display Route Layer tool you can access the advanced functions to get an Error Code. Any distance within the actual line will have a "NO ERROR" code, while those that fall off the line will have a "MEASURE NOT FOUND" code.

It is easy to create such an event table in Excel starting with just your RouteID set and adding a record for a measure at 0 and 5 and then duplicating and extending the series. Import it to a dbf or GDB table and you are done and make it a Route Event Layer. The Copy Features tool converts the events to actual points independent of the line if you need that, otherwise the events remain fluid with the line geometry and remeasuring of each line. Event tables can also tell you the normal or tangent angle of each point where it falls along the line for offset operations and you can optionally offset your points or line segment events from the line with an Offset field. Dynamic Segmentation is used to aptly describe this fluid and elastic way of creating point position and line segment portions along any line with M values.

For ArcObjects in C# use the ICurve.QueryPointAndDistance Method to do the same thing the event table is doing in code on a line with or without measures.