Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Something that is hopefully pretty easy.

I would like to be able to swap the direction of a polyline/line in QGIS. I made a custom tool to do this in MapInfo a couple of years ago, however I can't seem to find anything for QGIS.

Does anyone know of a tool to do this?

If one doesn't exist then I'm happy to have a go at making it, just didn't want to if one has already been made.

share|improve this question
3  
Would be great to have. I don't know of any such native QGIS function. –  underdark May 4 '11 at 6:26
3  
I have a good idea how I can do this pretty easily, I'll see if I can whip up something tonight. –  Nathan W May 4 '11 at 8:51
    
Great! Maybe it can be added to ftools/vector tool box. –  underdark May 4 '11 at 8:53
1  
since you have already got the core taken care of, any chance you want to tackle this question, but for QGIS? gis.stackexchange.com/questions/9286/… resources.arcgis.com/content/kbase?fa=articleShow&d=20961 –  RyanDalton May 4 '11 at 23:24
1  
Here is the ticket: hub.qgis.org/issues/4936 –  Mike T Mar 6 '12 at 20:58

3 Answers 3

If you have the GRASS plugin use the v.flip option - http://grass.osgeo.org/wiki/GRASS_AddOns#v.flip

enter image description here

share|improve this answer
    
hmmm was hoping for a more QGIS native way, don't have GRASS install at the moment. But it's a good tip for people that do. –  Nathan W May 4 '11 at 5:55
    
would be good to have in python and add to ftools noted by underdark. –  Mapperz May 4 '11 at 13:41
up vote 27 down vote accepted

Ok here is the Python that I used to do it:

layer = qgis.utils.iface.mapCanvas().currentLayer()
for feature in layer.selectedFeatures():
   geom = feature.geometry()
   nodes = geom.asPolyline()
   nodes.reverse() 
   newgeom = QgsGeometry.fromPolyline(nodes)
   layer.changeGeometry(feature.id(),newgeom)

Before running the above code:

  1. Select the layer you want to edit;
  2. Toggle Editing on;
  3. Select the features in this layer you want to reverse.

Run the above python code by:

  1. Going to Plugins > Python Console;
  2. Copying and pasting the code into the window;
  3. Hit Enter 2x to run.

It should output "True" for each feature whose direction was swapped. If it outputs "False" then it didn't swap that feature's direction. This will happen if the layer doesn't have Editing toggled on.

Pretty easy!

I have wrapped it up in a plugin called Swap Line Direction and it's available in the QGIS plugin installer.

I'll have a look at how easy it is to intergrate with fTools.

share|improve this answer
1  
so you created your own solution - well done - can it be added to ftools? –  Mapperz May 4 '11 at 21:36
    
I'm sure it can, I'll just have to look at the way fTools is coded and write a patch. –  Nathan W May 4 '11 at 22:02
    
Fantastic! Solutions like this are the reason I like using QGIS. Nice job Nathan - BTW I like your little catalogue type plugin aswell! –  Ando May 4 '11 at 22:05
    
@Ando Thanks! I'm beta testing a few new ideas, just wish I had more time to work on it so I could get it out quicker. –  Nathan W May 4 '11 at 22:17
3  
That is really awesome that you worked up a solution so quickly and elegantly. The power of Open Source wins again! –  RyanDalton May 4 '11 at 22:20

Following Nathan's answer, you can create a python action in the layer where you want to swap lines:

layer = QgsMapLayerRegistry.instance().mapLayer("_your_layer_id_")
r = QgsFeatureRequest([% $id %])
f = QgsFeature()
if layer.getFeatures(r).nextFeature(f):
  geom = f.geometry().asPolyline()
  geom.reverse()
  geom = QgsGeometry.fromPolyline(geom)
  if layer.changeGeometry([% $id %], geom):
    qgis.utils.iface.messageBar().pushMessage("Line swaped", QgsMessageBar.INFO, 2)
    qgis.utils.iface.mapCanvas().refresh()
  else:
    qgis.utils.iface.messageBar().pushMessage("Cannot swap line. Turn editing on.", QgsMessageBar.WARNING, 3)
else:
  qgis.utils.iface.messageBar().pushMessage("Cannot edit this feature.", QgsMessageBar.CRITICAL, 3)

You will be able to swap lines by one click on them. It is much user-friendly!

share|improve this answer
    
this code is for 2.x –  Denis Rouzaud Dec 12 '13 at 7:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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