Bug report #19220
Python Mem Provider Example Spatial Index not working
|Affected QGIS version:||3.1(master)||Regression?:||No|
|Operating System:||Easy fix?:||No|
|Pull Request or Patch supplied:||No||Resolution:|
|Crashes QGIS or corrupts data:||No||Copied to github as #:||27049|
First issue so my apologies if I commit any community faux pas.
I've been working quite a lot with the new Python-based QgsVectorDataProvider. One thing I cannot get working is the Spatial Index.
If I insert a bunch of features without a spatial index, they render, are queryable, appear in the table. If I "Create Spatial Index" it announces that one has been created, but my features all immediately disappear. It's as if every query is returning no results.
One thing I noticed in the code was this line: https://github.com/qgis/QGIS/blob/master/tests/src/python/provider_python.py#L390
It appears to be iterating over the keys (feature ids) and attempting to add features to the index by passing integers, not features. Looking at the docs, it doesn't look like there's a valid override for that. Signature 1 accepts a feature and Signature 2 accepts an id AND a bounds. https://qgis.org/api/3.0/classQgsSpatialIndex.html#aba57698626fcd78c42fb00570e6cbceb
So I dug deeper, trying to fix this myself with no luck. I instead pass it the feature, but nothing seems to be any different. I think one issue I have is that because of Qt5 bindings, sometimes errors seem to pass silently. Another issue is that this is new code for me and I haven't used QGIS or Qt in years. So this may be a me issue.
I read through the unit tests: https://github.com/qgis/QGIS/blob/master/tests/src/python/test_provider_python.py. It looks like none of them end up testing that features are queryable. So I'm not confident this has worked in the past?
I'm pretty motivated to help address this, so once I learn how to compile and run tests (and effectively have a dev environment for QGIS in Ubuntu 16.04), I will keep digging.
#1 Updated by Andrew Blakey about 2 years ago
- Assignee set to Andrew Blakey
I found the issues. I will attempt to get an environment together and make a pull request.
f.id() is always 0.
_f.id() is what was intended.
Also as described in the initial bug report, the iterator in createSpatialIndex needs to iterate over values, not keys.