2

I want to create a tool layer in QGIS 2.4 that will select any vector features that intersect with that point. In my script I tried the following function:

def selectFeature(self, point, button):

       pntGeom = QgsGeometry.fromPoint(point)
       pntBuff = pntGeom.buffer( (self.canvas.mapUnitsPerPixel() * 2),0)
       rect = pntBuff.boundingBox()
       cLayer = self.canvas.currentLayer()
       selectList = []
       if cLayer:
               provider = cLayer.dataProvider()
               feat = QgsFeature()
               provider.select([],rect)
               while provider.nextFeature(feat):
                       if feat.geometry().intersects(pntGeom):
                               selectList.append(feat.id())
               cLayer.setSelectedFeatures(selectList)

But I have the following error:

AttributeError: 'QgsVectorDataProvider' object has no attribute 'select' in line "provider.select([],rect)"

What am I doing wrong?

2 Answers 2

1

I've tried your code, but it not work. It can't find selected feature. So, I make an edit from your code. This is the edited code:

def selectFeature(self, point, button):
        pntGeom = QgsGeometry.fromPoint(point)
        pntBuff = pntGeom.buffer((self.canvas.mapUnitsPerPixel() * 2), 0)
        rect = pntBuff.boundingBox()

        cLayer = self.canvas.currentLayer()
        selectList = []
        if cLayer:
            rq = QgsFeatureRequest(rect)
            for feature in cLayer.getFeatures(rq):
                print feature
                if feature.geometry().intersects(rect):
                    selectList.append(feature.id())

            cLayer.setSelectedFeatures(selectList)
        else:
            print "No layer selected on TOC"

        print selectList

I am sorry I can't add this answer as comment, because my reputation are not enough. I hope this can help others.

1
  • it's OK to put his as an answer if it is the solution to your problem. Otherwise, please write this as an edit of your question
    – radouxju
    Aug 28, 2015 at 11:42
0

The follow code should replace everything in your if block:

if cLayer:
    for features in cLayer.getFeatures():
        if feat.geometry().intersects(pntGeom):
              selectList.append(feat.id())
     cLayer.setSelectedFeatures(selectList)

The select method doesn't exist in any of the 2.0 version of QGIS. There is a select on the layer object however it is for visually selecting the objects on the screen which is not what you want here.

Pro tip:

Use the following code to limit your search area or else you will be looping every feature everytime regardless:

  if cLayer:
    rq = QgsFeatureRequest(rect)
    for features in cLayer.getFeatures(rq):
        if feat.geometry().intersects(pntGeom):
              selectList.append(feat.id())
     cLayer.setSelectedFeatures(selectList)

This will create a QgsFeatureRequest which limits the search area to return features from via the passed in QgsRectangle

3
  • Thanks Nathan!!But if use this code don't solve the error, because it's in the previous line at cycle while or for.
    – Davide G.
    Oct 8, 2014 at 14:38
  • See my updated code example
    – Nathan W
    Oct 8, 2014 at 22:18
  • Hi Nathan!!!I used your updated code and I solved the problem. I made a change to putting in place of "for features" -> "for feat" and it works!!Thanks so much!!
    – Davide G.
    Oct 9, 2014 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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