2

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 anyone know if this is possible?

3

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
2

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)

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.