On Tue, 28 Nov 2023 09:19:30 -0800 (PST), Fabian wrote:
> The query has been executed on the database that I uploaded in the
> github repo.
>
Hi Fabian,
after performing a debug session on your data it finally turned
out that there is a bug in CheckSpatialIndex()
but to explain where the problem is we have to start from afar:
1. coordinates in SpatiaLite are represented as 64-bit DOUBLE
precision floating point values.
2. but coordinates in the R*Tree are stored as 32-bit FLOAT
values, and this implying some rounding.
3. but that's not all: both SQLite and SpatiaLite deliberately
alter the values that are inserted into the RTree in such
a way as to ensure that the MBR present in the RTree is
always a litlle bit larger than the actual one.
the reason should be obvious; given that the RTree is
ultimately just a coarse filter, it's unacceptable to
lose even a single feature due to rounding problems,
while obtaining a few false positives does not create
any serious issue.
4. drawing conclusions: the coordinate values in the table
and in the RTree are never exactly the same, there is
always some small difference, and precisely for this
reason CheckSpatialIndex() must rely on an approximate
comparison.
5. unfortunately your geometries have SRID=3875 (Web Mercator),
which is very popular among Web developers but has a very
bad reputation among professional geographers.
This very particular SRID presents an unusual characteristic:
the values of its coordinates (especially the X) are unusually
large thus throwing the approximate comparison criterion
adopted by CheckSpatialIndex() into haywire.
--------------
a quick and dirty fix for your issue:
a) open the spatialite.c source into src/spatialite
b) find the function body of eval_rtree_entry()
c) change "if (diff >= 1.5)" as "if (diff > 2.0)"
d) rebuild and install: problem solved :-D
(obviously it's NOT a robust solution, but it works)
bye Sandro