# Join two lines in one line

I have an array of Lines and I am using it to draw vectors in my map. I want to replace two superposed Lines (or have short distance between them) with one Line. Here is a picture that helps you to understand this problem:

The Input Lines :

The Output

PS: A Line is an ArrayList of points.

-
What software are you using? Some of it (such as much GIS software) has this capability built in, but if you are on a non-GIS platform, you might need a detailed algorithm. – whuber Dec 14 '12 at 17:31

Possibly take a look at the topojson project, which includes a simplification option for geometry.

If you wanted to do this yourself one algoritm would be as follows:

1. Break each line into segments (pairs of points)
2. Order these segments in a clockwise direction (sort vertices by x ascending, then y descending)
3. Calculate whether the segments are the same within some threshold
4. If the segments are the same, determine how you will calculate the true value (average, priorities a over b etc).
5. Replace updated segments in original shapes

Note this is a very simple algorithm - it doesn't take into account that you may wish to "snap" lines based on slope - to do that you'd have to calculate the line functions, compare them (the standard two-point function of a line is (x2 - x1)(y - y1) = (y2 - y1)(x - x1) for two points x1,y1 and x2,y2 - see Wikipedia for a more detailed explanation), and then decide on your rules for snapping them to the original line.

The author of the topojson projet, Mike Bostock, has also written a good article on line simplification which would certainly be worth a read.

-
Program options are using PostGIS ST_SnaptoGrid and topology tools, FME networktopology + transformer or MRFtools, GRASS v.net. – simplexio May 21 '13 at 12:43

Edit the vertices and merge it.

But it all depends on what you are doing and what you want out of it.

Topology and logical consistencies, come to mind as well.

-