[Qgis-developer] Spatial Index Destructor - Python

Jay L. jlaura at asu.edu
Mon Sep 9 21:33:24 PDT 2013

I am running QGIS 1.9 and developing a plugin that uses a spatial index to
improve intersection performance.  I am struggling to properly destroy the
spatial index and unload it.

The workflow that I am using is:

1. Load a point layer.
2. Create temporary, in-memory polylines and a spatial index on them.
3. Draw a line (rubberband) and split an in memory polyline.
4. Update the point layer attributes that tag a point to be a member of a
temporary line.
5. Remove the in memory layer and recreate it, plus spatial index, using
the new membership criteria.

The first time the temporary layer is created the spatial index works as
expected.  When I recreate the temporary layer, I create another instance
of QgsSpatialIndex.  The old spatial index appears to still be stored.  I
say this because the id() of the spatial index are doubled the send time.

For example, the first time I have ids -1 to -4 in the spatial index.  Then
I split and I have ids -1 to -9.  Re-instantiating the class should be
resetting the spatial index counter, but it isn't.

Is the ~QgsSpatialIndex method accessible via python?  This appears to be
the destructor of choice?  Any other ideas?

class slicer:

    def init(self):
        '''standard stuff here'''
    def activated(self):
        '''standard stuff here'''

    def create_temp_polylines(self):
        self.line_layer = QgsVectorLayer('Make the layer')
        self.spat_index = QgsSpatialIndex()
        self.point_layer = point_layer #Grabbed by iterating over layer list
        pt_prov = self.point_layer.dataProvider()
        polylines = {}
        for feat in self.point_layer.dataProvider():
            '''build the line geometries based on a membership attribute'''
        for key, value in polyline.iteritems():
            temp_polyline = QgsFeature()
            temp_polyline.setAttributes([attr1, attr2, ...])
            line_layer.addFeature(temp_polyline, True)

    def split_line(self, cut_line):
        '''logic to split a polyline'''
        '''logic to update the attribute table'''
        '''commit the changes to the DB'''

Originally posted here:

Thanks Nathan W. for the initial info.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130909/b9e94b41/attachment-0001.html>

More information about the Qgis-developer mailing list