# How do I construct a line from points that are out of order?

I'm using ArcGIS Pro and would prefer solutions using ArcPy, ArcMap, or ArcGIS Pro, but an open source alternative may work too.

I'm trying to construct a line from a series of points using the Points to Line tool. This works well if the points are all in some sort of order as given by the attributes table or their ObjectID; but if points are added out of order, the line is then constructed out of order as well, as seen in the image below after using Points to Line. The intended image looks like a smooth 'S' curve. (This is a simple example based off of more complex multiple points/lines.)

Sorting by latitude or longitude doesn't always work either, since the line meanders back at times.

How do I construct a line from points based off of something like nearest point?

• Is there an ascending field to put the points into order? The points to line tool resources.arcgis.com/en/help/main/10.2/index.html#/… can sort the points but there needs to be something to sort by. Otherwise you could give desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/… a go and see if it helps, I haven't used this tool so cannot testify to its efficacy. Dec 3, 2019 at 0:31
• I'll take a look at Sort (management), thanks!
– JMNC
Dec 3, 2019 at 0:40
• Sort (management) didn't work in my case, but it gave interesting results--basically zigzagged the line as if I'd sorted strictly by longitude or latitude. I may end up having to sort by longitude or latitude and then manually correct at places where the line doubles back.
– JMNC
Dec 3, 2019 at 0:57
• You have to define the order. ArcGIS can only guess the order OR use an order that you define. If there is not already an attribute that defines the order, then you'll need to create one, and populate it yourself, either manually, or based on some criteria that can be calculated. Eg, you could write a script that would calculate the next nearest point, however, in my experience, this is not always the next intended point; you also need to calculate which is the first point to start at, which is not something that can be reliably done automatically. Then use points-to-line tool. Dec 3, 2019 at 2:32
• Do you have other features that overlap or are nearby that have attributes that are in order? If so I would consider Spatial Join. Dec 18, 2019 at 4:30

Minimum spanning tree handles this very well, unless there are sharp turns < 90 degrees:

Output:

So:

• triangulate points
• extract edges
• build undirected graph
• compute it's MST

Networkx module does it with ease.

See script here.