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.

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.select(), 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
add comment

2 Answers

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

def spatial_select_features(layer, rect):

  layer.dataProvider().rewind()

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

  return features
share|improve this answer
add comment

Pending changes in the API ...

Another solution that combines geographic selection and loop on features.

Canvas freeze and restore the previous selection.

            self.canvas.freeze(True)
            oldSelection = layer.selectedFeaturesIds()
            try:
                layer.select(MyRect, False)

                # refining interesection
                feature = QgsFeature()
                for feature in layer.selectedFeatures():
                    if feature.geometry().intersects(rectGeom):
                        # DO SOMETHING
            finally:
                layer.setSelectedFeatures(oldSelection)
                self.plugin.canvas.freeze(False)
share|improve this answer
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.