5

I have a line layer that has been formed from a raster layer (It was a skeletonised raster layer). It now has a lot of unwanted vertices and needs smoothing out before further analysis. I have tried various methods including:

  • Simplify (Geometry Tools)
  • Line Simplification and line smoothing (SAGA vector line tools)
  • v.clean and v.generalizer (GRASS)
  • Smooth (Vector Geometry)
  • Generalizer (Generalizer3 Plugin)

Where I can I have tried the Chaiken's Algorithm. I have also tried various tolerances for the functions that didn't just freeze/fail outright. When I run all of these methods they all return the same line with no change. I'm not sure if I am using these wrong. So any advice on them would also be useful.

Here is an example of the line. Between vertices in the line is about 1m. Ideally this would be just one curved line.

enter image description here

However the whole layer is much larger. It has a 15km radius as you can see below. So these small lines could really do with smoothing out.

enter image description here

Coordinate Reference System: EPSG:27700 - OSGB 1936 / British National Grid

Melanie Baker
  • 511
  • 2
  • 13
  • 2
    What tolerances or similar settings did you use for the different tools? I would run a few tools with several different settings (perhaps 1m, 5m, 10m to start) and then adjust between these until you get a result you like. Since the results should all be temporary this is pretty easy to do even with several tools. If you want to get rid of the very small isolated lines entirely calculate line length and then select all under a certain size and delete them. – John Feb 08 '22 at 16:53
  • To expand on the comment by johns, the default tolerance of most of these tools is very low: they err on the side of keeping these details. Although this will also depend on your Coordinate Reference System; the Simplify tool defaults to 1, but these can mean 1 meter or 1 degree! To see them in action, run them several times with much higher tolerances, like 10, 100, and 1,000. – Cowirrie Feb 08 '22 at 23:14
  • 1
    Any chance that your single line made of thousands separate segments? – FelixIP Feb 09 '22 at 03:36
  • 1
    If each segment is separate line, I.e. has its own record in the table, Yes. You cannot smooth line made of 2 points. – FelixIP Feb 09 '22 at 08:39
  • Surely qgis should have arcgis equivalent of Dissolve to single parts. – FelixIP Feb 09 '22 at 20:07

4 Answers4

5

Geometry Generator

If you are just looking for a dynamic, visual smoothing (without altering the underlying geometries), the easiest way is Geometry Generator, one of QGIS' powerful features.

  1. Double click on the layer's name
  2. Select Symbology
  3. Under Symbol Layer Type choose Geometry Generator
  4. In the function area, input smooth($geometry,xxx) (use a real value instead of xxx) and click Apply

This trick is also useful because you can save the symbology as style and re-use it for any number of layers.

enter image description here

RafDouglas C. Tommasi
  • 6,479
  • 1
  • 15
  • 41
3

Line smoothing from the SAGA toolbox does a pretty good job on default settings.

enter image description here

Matt
  • 16,843
  • 3
  • 21
  • 52
2

I have also a few times needed to straighten lines and found no tool which did the trick.

So I wrote a python script which removes every nth vertice. This will create a new memory line layer:

lyr = iface.activeLayer() #Click/highlight the layer in layer tree

#Create an empty line layer vl = QgsVectorLayer("LineString?crs={}&index=yes".format(lyr.crs().authid()), "simplified_line", "memory") provider = vl.dataProvider()

for feature in lyr.getFeatures(): #For each line in the jagged line layer geom = feature.geometry() #Fetch the geometry newline = QgsMultiLineString() #Create a new empty multilinestring for subline in geom.parts(): #For each subline in the current line vertices = [p for p in subline.vertices()] #List all vertices vertices_thinned = [p for e, p in enumerate(vertices) if e%2==0] #Remove every other vertice. You can change 2 to 3 or higher to remove more vertices newsubline = QgsLineString(vertices_thinned) #Create a new linestring from the thinned lines newline.addGeometry(newsubline) #Add this linestring to the empty multilinestring f = QgsFeature() #Create a new feature f.setGeometry(newline) #Set the geometry to the new multilinestring provider.addFeature(f) #Add the feature

QgsProject.instance().addMapLayer(vl) #Add the new layer to the map

enter image description here

BERA
  • 72,339
  • 13
  • 72
  • 161
1

As stated in one of the comments posted on my question, the problem was each straight line was an individual line and therefore couldn't be smoothed by any of the functions I had tried.

  1. I used Geometric Attributes 'Merge Linestrings' tool. This took over 12 hours.
  2. Then, the Geometry tool 'Simplify', which worked very quickly.

(Note: I found the Geoprocessing tool 'Dissolve' didn't work as I lost too much information for further analysis this way.)

Melanie Baker
  • 511
  • 2
  • 13