A line-simplifying and line-rectifying tool

32
Simplifying & Rectifying a SHP Linestring Layer While I was interested in my ideas about 3D data in QGIS I met the contour lines.... by Giuliano Curti (giulianc51 at gmail dot com) Gennaio 2014 v. 2014-02-01

Transcript of A line-simplifying and line-rectifying tool

Simplifying & Rectifyinga SHP Linestring Layer

While I was interested in my ideas about 3D data in QGIS I met the contour lines....

by Giuliano Curti (giulianc51 at gmail dot com)Gennaio 2014

v. 2014-02-01

my initial intention was to clean the lines (obtained from DEM) from the flurry of points;

when I planned this script I did not know that the job were already possible in QGIS with the help of fTools;

I knew even less about the Ramer-Douglas-Peucker algorithm used by fTools.

At the discovery of my error, reported to me by a friend of the Italian GFOSS mailing list, I wanted to compare my game

procedure with that of fTools, finding that my script behaved well all things considered: in one test case my script was

cleaning the lines from the original 46k vertices at only 10711, compared to 10716 of QGIS / fTools.

5 out of 11k are almost a trivial problem, even though I was intrigued so I wanted to investigate;with the help of Spatialite I have identified 5 points

that appeared in fTools and not in my result;some points were caused by different threshold

used, but other do not and I discovered something interesting:

a small flaw in the RDP algorithm !look at the next slide:

This is an original line (look at how many red crosses indicating the number of vertices

this is the same line cleaned with fTools (note the central point emphasized)

this point is not necessary for the definition of the line that is in that stretch rectilinear and uniquely

defined by the initial vertex and the final one;

this is the result of my script (as you can see, the only preserved vertices are those

essential to the definition of the line)

one more example: the original line

The line cleaned with fTools: still a point unnecessary

This the line cleaned with my script: the point is eliminated.

If I may hazard a guess, is the central point(*) to be critical: if this is far from the line joining the start point - end point, as evident in the first case, it is still preserved, although a subsequent analysis

could be eliminated.

I do not have enough mathematical skills to do an analysis of the problem and probably I can not even speak of error, because the

final result is correct, in fact the RDP algorithm provides a line similar to the original one, certainly simplified;

perhaps what we can say is that this solution is not optimal, since in some cases, such as those seen above, leaves vertices

unnecessary to define the line.

(*) more precisely, the center point of each portion of the line.

One more note:analyzing the problem it happens that the vertex 2 has the greater distance from the base line 0-4, therefore

becomes one of the cornerstones of the line

If the distance was slightly shorter it would have been overlooked and therefore eliminated

After this necessary clarification, I continue with the presentation of my

script

With the advice that my script is purely experimental and need for accommodation.

Furthermore, it has purposes different from the RDP algorithm, because:

● The intention is conservative: intends to build the same original line, using only the vertices strictly necessary

● The algorithm is different, based on a locally defined approach

DisclaimerThis script is not intended to be an improvement of the RDP algorithm or

any other similar algorithm; it is an independent, simple and naive approach to the problem of discarding unnecessary points from a linestring.

This is a DTM 20m ~9km x 9km of my town, near Milan, Italy

Extracting the contour lines(Raster → Estrazione → Curve di

livello) (sorry, I've an italian menu :-)

We have >46000 points!

Many points are redundant:- or too near

- or, more frequently in this case, aligned;

thus I've written an (experimental) python script that should (it's far from

certain :-):a) simplify eliminating the points too near

b) rectify eliminating aligned points

below threshold value decided by the user

This is a result of my scriptthe original ones (red wider)

and the news simplified (yellow finer)

These are the points after simplification procedure (<11000)

This is an enlargement where we can see that the only retained points are the fundamental ones :-)

the original ones (green circles) and the news (stars)

… more enlarged …...

Author's approach to the problem

Simplify code

Rectify code

Flow chart of the algorithm

Passo 1

Passo 2

Passo 3

Passo.....

Probably there are other, and even better, similar tools, but I've made

this one and offer it to the community if it can be useful for

someone(remember this is an experimental tool !)

thank You :-)

Giuliano Curti, Melegnano (Mi), 2014-02-01

With apologies for my bad english :-)