Look for a definition
Not in every case it is clear to which next line the end of one line should be connected. So the first step is to find a defnition what means "vertical line" in your case and based on what criteria to select the lines than should be connected.
As of now, you do it manually, thus you have an implicit understanding. Make it explicit. 2nd step than is how to transform this definition in a machine-readable workflow (algorithm).
An algorithmic approach (workflow)
I propose one such algorithmic approach. Maybe someone else can help implementing it with Python.
One such definition could be based on 2 criteria, each with a variable value that increases with each iteration:
- 1: connect each end point of a line to the nearest start point of another line. Set a maximum distance: points further away will not be considered.
- 2: exclude connections with a certain deviation in x-direction: say, created lines should be vertical (azimuth=0 degrees) +/- a tolerance of e.g. 5 degrees.
It is an iterative approach. Start with low values for maximum distance (criteria 1) and devation (criteria 2), than increase the values for the remaining lines. Stop iteration when no more points, fulfilling both criteria with the increased values, are left.
Or: Insted of using azimuth, with a somehow similar approach you could define as a condition that values for x-coordinates for start- and connected end-point should not deviate for more than a certain amount (like 1 meter or so, depending on your data). So the line should not shit too much to the left or right.
Depending on how the result than looks, - you might want to add an additional criteria for the rest of the lines.
Probably even better and maybe to realize in just one run (without iteration), you could first define a number of nearest points as "candidates" to which the line could be connected. Let's say: for each end-point, get the five or ten nearest start-points and than keep the one where the azimuth of the connecting line has the smallest value.
Pre-conditions for the data to use
Of course, as a prepartion you should make sure that all the lines you already have run in the same direction (northto south or inverse). If you have lines N to S, than you could exclude all end-points with a higher y-value than the start-point as you want the line to follow one direction and not to go back and forth.
Screenshot of your upper right section: considering just connecting to the nearest start-point of the next line, you would get the blue lines. However, you want the black lines. Difference between blue and black lines, as can be clearly seen, is their azimuth, so this should be the key to select those lines you're interested in: