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

Stefano Masera stefano.masera at arpa.piemonte.it
Wed Nov 6 00:07:51 PST 2013


Thank you very much. 
It's a good explanation. 
Only another think. 


Which is the difference to create a spatial index in this way: 


feat_all = layer.dataProvider().getFeatures() 

layer_spIndex = QgsSpatialIndex() 

for feat in feat_all: 
layer_spIndex .insertFeature(feat ) 


Or in this way: 


layer.dataProvider().createSpatialIndex() 


It seems to work only in the first case. 


Thanks 


Stefano 










----- Messaggio originale -----

Da: "Daniel" <danielvaz at gmail.com> 
A: "Stefano Masera" <stefano.masera at arpa.piemonte.it> 
Cc: "qgis-developer" <qgis-developer at lists.osgeo.org> 
Inviato: Lunedì, 4 novembre 2013 15:55:25 
Oggetto: Re: [Qgis-developer] Does spatial index improve performance using the class QgsGeometry.intersects()? 



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/20131106/bf5b95f9/attachment-0001.html>


More information about the Qgis-developer mailing list