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.

Is there a way of establishing the order that a user selected features using QGIS and Python?

By example: we have a network of line features representing a road network, and the user defines a route from point A to point B. Currently, using layer.selectedFeatures() returns the correct features, but in the order they were drawn rather than the order they were selected.

I've tried a few things to do this manually:

  • QgsMapToolEmitPoint(canvas) obviously doesn't work - I want the feature not the point clicked. But I guess in worst case I could hack a spatial query to do a look up of the closest feature. Not ideal.
  • QgsMapToolSelect(canvas) doesn't seem to be available as part of the Python accessible GUI or CORE, although I can find mention of it existing elsewhere.

Can anyone suggest a best case solution for this? All suggestions/tutorials/related questions appreciated.

Thanks in advance for your time, Dan

share|improve this question
add comment

1 Answer

I see two possibilities:

  • Have a look at QgsSnapper (This is what you called the worst-case solution, although I don't think its bad at all. The internals would do the same)
  • Connect to the QgsVectorLayer.selectionChanged signal. In 1.9 you even get the delta of the selection.

from PyQt4.QtCore import pyqtSlot
@pyqtSlot( 'QgsFeatureIds', 'QgsFeatureIds', bool )
def onSelectionChanged( added, removed, cleared ):
    print( added )
iface.activeLayer().selectionChanged.connect( onSelectionChanged )

The @pyqtSlot line is optional. (See here for an explanation why it's good practice.)

share|improve this answer
    
Thanks for the tips, Matthias. It looks like the connection on selectionChanged will do what I need. Could you provide an example of the connection though? I'm not familiar with the structure here and quite how to pass the feature ids into my custom function. Thanks in advance. –  Dan Aug 30 '13 at 7:55
1  
Answer edited... –  Matthias Kuhn Aug 30 '13 at 8:45
    
Odd. I get the following error from your example: TypeError: onSelectionChanged() takes exactly 3 arguments (2 given). Perhaps an issue with it being a custom UI that uses self? –  Dan Aug 30 '13 at 9:40
1  
You will have to add self to onSelectionChanged if it's inside a class. def onSelectionChanged(self, added, removed, cleared) You also don't need the pyqtSlot stuff normally. –  Nathan W Aug 30 '13 at 9:52
add comment

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.