6

I would like to calculate line sinuosity for line segments.

The sinuosity is defined as a ratio of the distance between start and end point along a line (line length) and the euclidean distance between both points. A large sinuosity signifies a large detour between start and end point and is shown as the red line in the next image:

enter image description here

Caculating this measure is straightforward in GRASS GIS with:

v.to.db map=lines option=sinuous columns=sinuosity

However, v.to.db is not part of QGIS processing and cannot be called from within the QGIS Python shell.

Is there another option to calculate line sinuosity in PyQGIS?

Here some links to related questions:

1 Answer 1

6

Since the calculation of the sinuosity is quiet simple, one option might be to calculate it with the QGIS fieldcalculator which is callable with PyQGIS:

processing.runalg('qgis:fieldcalculator',
                      {"INPUT_LAYER": lines,
                       "FIELD_NAME": "sinuosity",
                       "FIELD_TYPE": 0,
                       "FORMULA": 'length($geometry)/distance(start_point($geometry), end_point( $geometry))',
                       "NEW_FIELD": True,
                       "OUTPUT_LAYER": line_sinuosity})
2
  • 1
    The formula may give an incorrect result since $length measures using ellisoid and units from the QGIS settings. To get the line length in the CRS units use "length($geometry)". also, you can use the field calculator distance function like so: length($geometry)/distance(start_point($geometry), end_point( $geometry))
    – amcaninch
    Dec 30, 2017 at 14:27
  • Thanks @amac I changed the former formula ("length" / sqrt((xat(-1) - xat(0))^2 + (yat(-1) - yat(0))^2)) to (length($geometry)/distance(start_point($geometry), end_point( $geometry))) Jan 5, 2018 at 12:09

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.