I built a manual index on a shapefile, through the process "Create spatial index", which can be found in the process toolbox > general vector tools. This process creates a persistent spatial index, a .qix file.

I want to optimize the query: "How many polygons of a layer are contained within one or more larger polygons that I select" using PyQGIS.

Both layers are based on files in polygon shapefile format. The layer of the polygons that will be evaluated has more than 300.000 entities.

I can build and use a custom index with this code:


tiempo_inicial = time.time()
index = QgsSpatialIndex() # Spatial index
index = QgsSpatialIndex(parcelas.getFeatures())
duracionSI=time.time()-tiempo_inicial  #  6.816403388977051 sec

tiempo_inicial = time.time()
for i in radios.getSelectedFeatures():
   bbx= i.geometry().boundingBox()
   for j in listIds:
        if i.geometry().contains(parcelas.getFeature(j).geometry()):

But what I really want to do is leverage the existing .qix file directly, so I don't have to spend six seconds compiling the index every time. How can I do this from PyQGIS?

Note: I am aware that the spatial index file must be updated if geometries are modified, removed or added.

  • 2
    The capabilities of spatial indexes are dependent on the data source and the operation to be performed. Without any mention or dataset or operation, or code representing your attempt, it seems likely this question will be closed.
    – Vince
    Commented Jan 13, 2021 at 4:28
  • 1
    You didn't specify the data format associated with your parcels layer (in the body). And at this point, you don't seem to have a question being asked.
    – Vince
    Commented Jan 13, 2021 at 21:42
  • 1
    Your initial question didn't have the needed details; now you have too many, obscuring your issue. If you strip it down to "If I build a manual index on a shapefile {with example} I can optimize this query, but how can I access the .quix file directly, so I don't have to spend six seconds compiling the index every time?" your Question will be clearer.
    – Vince
    Commented Jan 14, 2021 at 1:52
  • 2
    I would be surprised if QGIS does not automatically use an available index (e.g. an existing .qix file when you open the layer another time)! Are you sure that it is not used? Commented Jan 15, 2021 at 12:54
  • 3
    It seems that QgsSpatialIndex is a temporary index and it is not the same than qix index gis.stackexchange.com/questions/234467/…. So if you want to use the qix index you will not use index.intersects. I guess that you should rather open the shapefile that has .qix index with the ogr provider and query as a.geometry().intersects(b.geometry()) and hope that the qix index is utilized transparently. See also gis.stackexchange.com/questions/168266/…
    – user30184
    Commented Jan 16, 2021 at 21:44


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.