2

I'm trying to implement in my plugin such features as "select by freehand" and "select by polygon" for some purposes, and I'm trying to search internet for how they work.

But all I can find is:

QAction *QgisAppInterface::actionSelectFreehand() { 
      return qgis->actionSelectFreehand(); 
}

I need to know how features are selected under rubberband which is created by this function(s).

It's certainly not QgsGeometry.intersect, or QgsGeometry.within, or QgisVectorLayer.select()

Is there any secret knowledge how this native built-in functions work?

Doesn't matter if it is cpp or Python documentation.

1

In my (ancient) plugin selectplus, which did pretty much what you are trying to do, I just tested the intersection of features with a polygon created from the freehand polygon coordinates in coords. Something like:

g=QgsGeometry().fromPolygon([coords])
# now look over current layer features f and test 
provider = layer.dataProvider()
allA=provider.attributeIndexes()
provider.select(allA)
feat = QgsFeature()
selectedSet = []
while provider.nextFeature(feat):
    geom = feat.geometry()
    if geom.intersects(g):
        selectedSet.append(feat.id())

Note the while loop over features in the layer and the geom.intersects(g) which tests if a feature overlaps the g polygon.

This was a while ago so I'm not sure if the API has changed, and this functionality is now in the core of QGIS so I've not bothered to keep this working. If you search for "bsrplugins" and "selectplus" you might find the code, but don't ask me questions about it!

You might find more up to date answers in the QGIS source code, but its probably coded in C++ now.

  • 1
    I already did the same code to my plugin. But it works much more slower than built-in selector in selectFreehand. So that's why I've noticed that I want to avoid 'QgsGeometry.intersect', or 'QgsGeometry.within'. I have a layer with 156 000 features and selection with code like yours - takes too much time to apply selection. That's why I need to discover API for native QGis selector or something like that. – justhateme Feb 2 '17 at 12:54

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.