Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using QGIS 1.9.0-master, I want to do a spatial search for features in a vector layer without selecting them, i.e., without using, QgsVectorLayer.selectedFeatures() et al.

More specifically:

I want to get the feature IDs inside a rectangle. That could be accomplished with something similar to QgsSpatialIndex.intersects() if I could gain access to the spatial index of the layer's dataprovider.

Unfortunately, I didn't find a way to do it. I was only able to find how to create the index using QgsVectorDataProvider::createSpatialIndex()

Does anybody know it this is possible?

share|improve this question

For now, as a workaround, I created a function that iterates over the layer features:

def spatial_select_features(layer, rect):


  feature = QgsFeature()
  features = []
  while layer.dataProvider().nextFeature(feature):
    if feature.geometry().intersects(rect):
       features += [QgsFeature(feature)]

  return features
share|improve this answer

Pending changes in the API ...

Another solution that combines geographic selection and loop on features.

Canvas freeze and restore the previous selection.

            oldSelection = layer.selectedFeaturesIds()
      , False)

                # refining interesection
                feature = QgsFeature()
                for feature in layer.selectedFeatures():
                    if feature.geometry().intersects(rectGeom):
                        # DO SOMETHING
share|improve this answer

Your Answer


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.