1

I have ONE layer of linestrings and some of them are overlapping, I need to display the overlapping lines with an offset.

I have seen the solution using the offset in style for the whole layer but it applies it to all the lines.

Is there a way to do it either with qgis or programatically using pyqgis ?

  • 1
    AFAIK no offset to the part of a layer. Can you select by attribute the lines to be moved? You can create tematic map where a class can be moved or use wider lines to be visible. – Zoltan Jul 5 '17 at 15:33
  • @Zoltan I thought about somethin glike the fusion mode betzeen features option in style but I hoped there would be something more like an offset ( only for display ). anyway thanks for your reply – Hicham Zouarhi Jul 5 '17 at 15:40
  • @Hicham Zouarhi I posted a way to do it programatically by using pyqgis. I hope that it helps. – xunilk Jul 5 '17 at 16:43
  • is it working with ArcGIS? i had SAM problem – Musallam Nov 9 '17 at 4:30
  • @Musallam no this is just for Qgis as it uses only pyqgis and not arcpy – Hicham Zouarhi Nov 9 '17 at 10:20
2

With PyQGIS you can use next code:

canvas = iface.mapCanvas()

layers = canvas.layers()

n = canvas.layerCount()

symbol = range(n)

symbol[0] = QgsLineSymbolV2.createSimple({'color':'green',
                                       'width':'1',
                                       'offset':'1.0'})

symbol[1] = QgsLineSymbolV2.createSimple({'color':'red',
                                       'width':'1',
                                       'offset':'0.0'})

symbol[2] = QgsLineSymbolV2.createSimple({'color':'blue',
                                       'width':'1',
                                       'offset':'-1.0'})

renderers = [ layer.rendererV2()  for layer in layers ]

for i, render in enumerate(renderers):
    render.setSymbol(symbol[i].clone())

for layer in layers:
    layer.triggerRepaint()

In my example with three polylines superposed of next image:

enter image description here

after running the code at the Python Console of QGIS I got:

enter image description here

Editing Note:

In a single layer it's necessary a QgsCategorizedSymbolRendererV2 object. So, code is the following:

layer = iface.activeLayer()

symbols = []

symbol = QgsLineSymbolV2.createSimple({'color':'green',
                                       'width':'1',
                                       'offset':'1'})

symbols.append(symbol)

symbol = QgsLineSymbolV2.createSimple({'color':'red',
                                       'width':'1',
                                       'offset':'0'})

symbols.append(symbol)

symbol = QgsLineSymbolV2.createSimple({'color':'blue',
                                       'width':'1',
                                       'offset':'-1'})

symbols.append(symbol)

# creating categories
categories = [ QgsRendererCategoryV2(i, symbol, str(i)) for i, symbol in enumerate(symbols) ]

renderer = QgsCategorizedSymbolRendererV2('id')

for category in categories:
    renderer.addCategory(category) 

layer.setRendererV2(renderer)

layer.triggerRepaint()

After running the code at the Python Console of QGIS, in my new example with three features superposed, it works perfectly:

enter image description here

  • thanks for your solution, but I'm using a single layer and what I try to do is apply the offset within its features – Hicham Zouarhi Jul 6 '17 at 7:11
  • In a single layer it's necessary a QgsCategorizedSymbolRendererV2 object. I included this possibility in my answer. PyQGIS solution is more elegant because you don't need add another column in your attribute table for considering offset. – xunilk Jul 6 '17 at 14:11
  • cool, +1 for the QgsCategorizedSymbolRendererV2 solution – Hicham Zouarhi Jul 6 '17 at 14:28
2

I have seen the solution using the offset in style for the whole layer but it applies it to all the lines

You could also add another column in your attribute table. Mark all your lines that should have an offset, add a value like "1" for those lines in your column and apply the offset style just for those lines. (Rule-based styling)

  • So far this solution works well I'll wait to see if there aother suggestions then I'll accept it. Thanks @AndreasK – Hicham Zouarhi Jul 5 '17 at 15:46
  • I tried various possibilities including some PyQGIS stuff and in the end this simple approach was the only one, that worked and where I was possible to explain what's going on;-) – AndreasK Jul 6 '17 at 11:31

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.