[Qgis-developer] Does spatial index improve performance using the class QgsGeometry.intersects()?

Daniel danielvaz at gmail.com
Mon Nov 4 06:55:25 PST 2013


Try it

http://nathanw.net/2013/01/04/using-a-qgis-spatial-index-to-speed-up-your-code/

Best regards.


On Mon, Nov 4, 2013 at 9:16 AM, Stefano Masera <
stefano.masera at arpa.piemonte.it> wrote:

> Hi list,
>
> I don't know exactly how spatial index works, so I ask this question.
> I have two layers and I want to check which features of the first layer
> intersect the features of the second one.
> I write a simple script where I create the spatial index for both themes
> and I use the class QgsGeometry.intersects() to check the intersections.
>
> I cannot understand why the executing time is the same even if I create or
> I don't create the spatial index for the layers.
> Note:
> - The first layer (polilyne) has 20000 features, the second (polygon) has
> 600 features.
> - To test the script insert the path of a check file
> - To compare time executing you have to comments the two lines (21 and 22)
> in which I create the spatial index and cancel the file *.qix on the hard
> disk.
>
> Thanks
> Stefano Masera
>
>
>
> ====================================================================================
> ##input_layer1_polyline=vector
> ##input_layer2_polygon=vector
>
> from qgis.core import *
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> import processing
> import time
>
> # start time
> start_time = time.time()
>
> # opens a check file: insert path
> file = open("INSERT PATH","w")
>
> # gets vector layer
> layer1_polyline = processing.getobject(input_layer1_polyline)
> layer2_polygon = processing.getobject(input_layer2_polygon)
>
> # creates SpatialIndex
> layer1_polyline.dataProvider().createSpatialIndex()
> layer2_polygon.dataProvider().createSpatialIndex()
>
> # gets features from layers
> polyline_feat_all = layer1_polyline.dataProvider().getFeatures()
>
> for polyline in polyline_feat_all:
>     # writes in a check file the polyline
>     file.write("polyline: " + str(polyline.id()) + "\n")
>
>     polygon_feat_all = layer2_polygon.dataProvider().getFeatures()
>
>     for polygon in polygon_feat_all:
>         if polyline.geometry().intersects(polygon.geometry()) == 1:
>             # writes in a check file the intersect polygon
>             file.write("\t" + "intersect polygon: " + str(polygon.id()) +
> "\n")
>
> # end time
> end_time = time.time()
>
> file.write("\n" + "Execution time: " + str(end_time - start_time) + "\n")
>
> file.close()
>
> ====================================================================================
>
>
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>



-- 
Daniel Vaz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20131104/564d7c40/attachment.html>


More information about the Qgis-developer mailing list