QGIS – Simplifying Multiple Lines to Create Central Axis

generalizationNetworkqgissimplifyskeleton

I have a layer with railway lines, downloaded from OpenStreetMap. Every single track is represented separately, so at trainstations, you have several parallel lines, as well as sidelines that lead to depots. Sometimes, lines are broken (it's OSM!). I want to reduce all these lines to one single, simple line.

An idea is to create buffers around the railway lines and to dissolve them to a single polygon (and maybe apply a negative buffer to reduce its width and generalize the geometry)?

Railway lines, Buffer and vertices of the buffer

If I could create a medial axis from this polygon,, it would represent quite well the simplified railway line I want to create.

However, how to create such a medial axis in QGIS?

There is a solution in PostGIS: https://postgis.net/docs/ST_ApproximateMedialAxis.html

But unfortunately, I have no experience with postgis, so I'm looking for a solution inside QGIS. A possible solution seems to be the HCMGIS-plugin that has a function "Create Skeleton/Medial Axis". The result represents quite well the railway-track (see screenshot, red line), but with one major problem: it creates a skeleton with small side-lines connecting to the vertices of the simplified buffer.

Original railway line, simplified buffer and skeleton in red

Second problem: algorith takes quite long for a larger selection – I was able to test it only with a small selection as the calculation for the whole railway-network did not stop even after half an hour. I'm not sure if it works well for the whole network (see screenshot 3 with the red part representing the selection used for testing).
enter image description here

Any idea for alternative solutions?


@Cyril's suggestion of using the midline of the skeltonize method would be perfect for me, but unfortunately, the resulting layer consists of only one object – so the short lines are not separated from the long one.

Maybe there is also a completely different method for solving my problem?

Update: see PostGIS-syntax: export query-result to QGIS / Shapefile

Best Answer

Take the result of the Skeletonize method and remove the short line segments.

  1. Since the output is a single feature, first split the segments into separate features. The tool Multipart to single parts should achieve this. If not, you may need to resort to Explode lines, which will split the line apart at the vertices.
  2. Select the short lines with the Select by Expression tool, and the expression $length < L, where L is an appropriate length value.
  3. Delete the selected lines.
  4. If you used Explode lines in step one, you now have midlines made up of many separate line segments. Merge them into individual lines using the dissolve tool, followed by multipart to single parts again.