<div dir="ltr"><div>Try it</div><div><br></div><a href="http://nathanw.net/2013/01/04/using-a-qgis-spatial-index-to-speed-up-your-code/">http://nathanw.net/2013/01/04/using-a-qgis-spatial-index-to-speed-up-your-code/</a><br>
<div><br></div><div>Best regards.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Nov 4, 2013 at 9:16 AM, Stefano Masera <span dir="ltr"><<a href="mailto:stefano.masera@arpa.piemonte.it" target="_blank">stefano.masera@arpa.piemonte.it</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi list,<br>
<br>
I don't know exactly how spatial index works, so I ask this question.<br>
I have two layers and I want to check which features of the first layer intersect the features of the second one.<br>
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.<br>
<br>
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.<br>
Note:<br>
- The first layer (polilyne) has 20000 features, the second (polygon) has 600 features.<br>
- To test the script insert the path of a check file<br>
- 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.<br>
<br>
Thanks<br>
Stefano Masera<br>
<br>
<br>
====================================================================================<br>
##input_layer1_polyline=vector<br>
##input_layer2_polygon=vector<br>
<br>
from qgis.core import *<br>
from PyQt4.QtCore import *<br>
from PyQt4.QtGui import *<br>
import processing<br>
import time<br>
<br>
# start time<br>
start_time = time.time()<br>
<br>
# opens a check file: insert path<br>
file = open("INSERT PATH","w")<br>
<br>
# gets vector layer<br>
layer1_polyline = processing.getobject(input_layer1_polyline)<br>
layer2_polygon = processing.getobject(input_layer2_polygon)<br>
<br>
# creates SpatialIndex<br>
layer1_polyline.dataProvider().createSpatialIndex()<br>
layer2_polygon.dataProvider().createSpatialIndex()<br>
<br>
# gets features from layers<br>
polyline_feat_all = layer1_polyline.dataProvider().getFeatures()<br>
<br>
for polyline in polyline_feat_all:<br>
    # writes in a check file the polyline<br>
    file.write("polyline: " + str(<a href="http://polyline.id" target="_blank">polyline.id</a>()) + "\n")<br>
<br>
    polygon_feat_all = layer2_polygon.dataProvider().getFeatures()<br>
<br>
    for polygon in polygon_feat_all:<br>
        if polyline.geometry().intersects(polygon.geometry()) == 1:<br>
            # writes in a check file the intersect polygon<br>
            file.write("\t" + "intersect polygon: " + str(<a href="http://polygon.id" target="_blank">polygon.id</a>()) + "\n")<br>
<br>
# end time<br>
end_time = time.time()<br>
<br>
file.write("\n" + "Execution time: " + str(end_time - start_time) + "\n")<br>
<br>
file.close()<br>
====================================================================================<br>
<br>
<br>
<br>
_______________________________________________<br>
Qgis-developer mailing list<br>
<a href="mailto:Qgis-developer@lists.osgeo.org">Qgis-developer@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Daniel Vaz
</div>