Ordering a set of lines so that they follow one from the other
Line# Lat. Lon.
1a 1573313.042320 6180142.720910
..
..
1z 1569171.442602 6184932.867930
3a 1569171.764930 6184934.045650
..
..
3z 1570412.815667 6190358.086690
5a 1570605.667770 6190253.392920
..
..
5z 1570373.562963 6190464.146120
4a 1573503.842910 6189595.286870
..
..
4z 1570690.065390 6190218.190575
Each pair of lines above (a..z) represents the first and last coordinate pair of a number of points which together define a line. The lines are not listed in sequence because I don't know what the correct sequence is just by looking at the coordinates (unless I look at the lines in a map).
Hence my question: how can I find programmatically (in Python) what the correct sequence is, so I can join the lines into one long line, keeping in mind the following problem: - a 'z' point (the last point in a line) may well be the first 开发者_如何学运维point if the line is described as proceeding in the opposite direction to other lines. e.g. one line may go from left to right, another from right to left (or top to bottom and viceversa).
Thank you in advance...
Create a dictionary whose keys are (lat, long)
tuples for either end point, and whose values are arrays of all of the lines that have such an end point. Search the dictionary for a tuple whose value has only one line. That is an end point. Use the dictionary to traverse the path.
This approach will run into issues if the line segments don't connect, or if there is no end, or if there are multiple intersections somewhere. It is up to you to decide how much validation to do for those cases, and how you want to handle those potential problems.
精彩评论