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

  • If you're happy with the midline of the skeletonize method, just select the short lines with the Select by Expression tool, and the expression $length < L, where L is an appropriate length value. Then delete the selected lines. – csk Apr 21 '19 at 1:28
  • 1
    If the skeltonize tool suits you, blow up the line and then use the csk clause ... – Cyril Mikhalchenko Apr 21 '19 at 11:58
  • OK, this seems to provide a solution. However, I' not sure which QGIS-tools to use: blowing up? csk clause? – Babel Apr 21 '19 at 14:10
  • 2
    To do this, QGIS in the Analysis Tools window you have the following tool Vector Geometry> Blow Up Lines ... – Cyril Mikhalchenko Apr 21 '19 at 14:48

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.
  • The proposed solutions would work theoretically, I guess. Unfortunately, the problem is that the HCMGIS-plugin's function "Create Skeleton/Medial Axis" has not stopped calculating even after more than one and a half day (!) and I'm not sure how long it will still take. So it is not practicable in my case. Is there another way to get a central axis? Or a completely different approach, starting again from scratch? – Babel Apr 23 '19 at 8:05
  • 1
    I'm confused. You wrote in your question that you already succeeded at creating a skeleton. Is that not true? You should use the edit link under your question to update it with correct information. – csk Apr 23 '19 at 16:15
  • I was not able to create a skeleton for the whole railway-network, just for a small part of it for testing-purpose. So no, I was not able to create the skeleton, as the process to create it was going on even after more than one and a half day, so I had to cancel it. So I am still looking for a practicable solution. – Babel Apr 24 '19 at 7:55

Try this in QGIS Python Console:

from HCMGIS.hcmgis_library import *    
#Create Medial Axis/ Skeleton from Road in Polygon
input = "D:\\osm\\road.shp"
output = "D:\\osm\\skeleton.shp"
hcmgis_medialaxis(input, 'OBJECTID', 1,output,status_callback = None)
#hcmgis_medialaxis(layer, field, density,output,status_callback = None):
#field: unique field of inputlayer
#density (float value): densify geometries with given an interval (in this case the density is 1 meter). Smaller density value returns smoother centerline but slower

Full Reference: HCMGIS Plugin Library call in QGIS Python Console or Stand alone PyQGIS Application

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.