I wanted to know how could I do a offset line of one layer in a new layer.

I take my layers, even if I only have one active (streets)

canvas = iface.mapCanvas()
layers = canvas.layers()

In my case, layers is just one layer, and this layer have 600 lines. After that I don't know first create a new layer where put the result (the offsets) and the other problem create the offset ( -2 and 2). I saw other post trying to resolv my problem, but I tried to do that but didn't work.

I did that manually, but I want that in python.

Manually, I had this:

enter image description here

And after offset I got this:

enter image description here

But I tried to write several codes and all codes didn't work.

  • Can you add screenshots so we can better understand your problem? Mar 5, 2020 at 14:48

2 Answers 2


By using 'qgis:offsetline' of Processing Tool Box, you can do that with a few lines of code; as in following script.

import processing

layer = iface.activeLayer()

parameters = { 'DISTANCE' : 5, #offset of 5 meters
               'INPUT' : layer, 
               'JOIN_STYLE' : 1, 
               'MITER_LIMIT' : 1, 
               'OUTPUT' : 'TEMPORARY_OUTPUT', 
               'SEGMENTS' : 8 }


I ran above script with line layer (in red) of following image; where offset line was drawn in blue.

enter image description here

  • The function "runAndLoadResults" can I use it with all function as offset or merger, or another function? Mar 7, 2020 at 20:03
  • Yes, you can use this method in other processing tools.
    – xunilk
    Mar 7, 2020 at 20:08
  • for example, When I do this code for the functions offset the lines are separated, so if after offset I want to merge the lines, I though use the function snap, but in the documentation api there are several function. (snap geometry, snap feature, snapped Segment) what functions should I use in this case? And the other cuestion is, in the same way than the offset function? And thank you very much for everything. Mar 7, 2020 at 20:23
  • If you want combine lines with its respective offset (an individual id for each one) you should use merge. If you want "merge" both lines with one id you should use collect instead. Remember that you can do another question about that.
    – xunilk
    Mar 8, 2020 at 0:08
  • Thank you very much, I am going to do another question. Mar 8, 2020 at 10:19

The Offset lines algorithm will do this for you. Have you considered using it instead of doing it in Python?

With Python, you can use the offsetCurve (double distance, int segments, JoinStyle joinStyle, double miterLimit) function for each feature geometry (https://qgis.org/api/classQgsGeometry.html#ad9508e6f1c6247ce6347f46740d61818).

To use it, you have to iterate over the features in your line layer and for each feature get its geometry and call the function:


How to use the offset line algorithm (GUI)

Offset lines - offset to one side (positive Distance): offset+

Offset lines - offset to the other side (negative Distance): offset-

Offset lines - result (input layer: black lines): result


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.