[GIS] Simplify the straight sections of (multi)Linestring in PostgreSQL/PostGIS leaving the curved sections untouched

linestringpostgispostgresqlsimplify

I have (multi)linestrings containing upwards of 5000 points and I want to perform two simplifications possibly using Ramer Douglas Peucker or alternative methods in PostGIS. The (multi)linestrings consist of straight lines, 90 degree curves, 180 degree curves and loops (circles).

I understand that ST_simplify may remove the loops and possibly some of the bends depending on the tolerance set. I need to have a zoomed out version which can probably be done satisfactorily done with ST_simplify. However, on the zoomed in version, it is important to keep the loops, and curve points.

I would like to remove all the points from the straight segments (within a tolerance) which constitute 90% of the (multi)linestring and keep the points on the curve section.

How can I achieve this in PostGIS?

Best Answer

Use ST_SetEffectiveArea in conjunction with ST_FilterByM on the result of ST_LineMerge, e.g.:

SELECT  ST_AsText(
            ST_FilterByM(
                ST_SetEffectiveArea(
                    ST_LineMerge('MULTILINESTRING((0 0, 0 1), (0 1, 0 2), (0 2, 0 3), (0 3, 0 4), (0 4, 1 4), (1 4, 2 4), (2 4, 3 4))'::GEOMETRY)
                ),
                0.0001
            )
        )
;

SELECT  ST_AsText(
            ST_FilterByM(
                ST_SetEffectiveArea(
                    ST_Buffer('POINT(0 0)'::GEOMETRY, 0.1)
                ),
                0.0001
            )
        )
;
Related Question