1. Simplifying & Rectifying
a SHP Linestring Layer
by Giuliano Curti (giulianc51 at gmail dot com)
Gennaio 2014
While I was interested in my ideas about 3D data
in QGIS I met the contour lines....
v. 2014-02-01
2. 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.
3. 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:
4. This is an original line (look at how many
red crosses indicating the number of vertices
5. this is the same line cleaned with
fTools (note the central point emphasized)
6. 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;
7. this is the result of my script (as you
can see, the only preserved vertices are those
essential to the definition of the line)
10. This the line cleaned with my script:
the point is eliminated.
11. 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.
12. 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
13. If the distance was slightly shorter it would have
been overlooked and therefore eliminated
14. After this necessary clarification, I
continue with the presentation of my
script
15. 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
Disclaimer
This 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.
16. This is a DTM 20m ~9km x 9km of
my town, near Milan, Italy
17. Extracting the contour lines
(Raster → Estrazione → Curve di
livello) (sorry, I've an italian menu :-)
19. 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
20. This is a result of my script
the original ones (red wider)
and the news simplified (yellow finer)
21. These are the points after
simplification procedure (<11000)
22. 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)
32. 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 :-)