On Mon, 1 Apr 2024 11:55:22 -0700 (PDT), Jim wrote:
> I have a database created from a shapefile by spatialite_gui where I
> set the SRID to 4326 and requested that a SpatialIndex be created and
> UTF-8. All other settings for loading a shape file were left as
> presented.
>
> The database contains a Polygon named 'Geometry'.
>
> If I query the database using the following, I get the expected
> result.
>
> SELECT PO_NAME, STATE, ZIP_CODE
> FROM uszip
> WHERE ST_Contains(uszip.Geometry, MakePoint(-122.365680,47.763610,
> 4326)) = 1
>
> However, if I use this query, I get an empty result set:
>
> SELECT PO_NAME, STATE, ZIP_CODE
> FROM uszip
> WHERE
> ST_Contains(uszip.Geometry, MakePoint(-122.365680,47.763610, 4326)) =
> 1
> AND uszip.PK_UID IN
> (SELECT ROWID FROM SpatialIndex WHERE f_table_name = 'uszip'
> AND search_frame = 'Geometry') ;
>
> I am at a loss.
>
> Is there anything obviously wrong with the second query?
>
YES ;-)
... AND search_frame = 'Geometry'
if you write 'geometry' (quoted) it becomes a text constant,
it cannot be the reference to the name of a column where you
expect to find somem BLOB-Geometry.
when SpatiaLite finds a text string where it expects to find
a BLOB-Geometry it will obviously fail returning a NULL.
but there is a second more substantial error.
... SELECT ROWID FROM SpatialIndex WHERE f_table_name = 'uszip'
you are querying the SpatialIndex which supports the "uszip" table;
therefore the filter-geometry defined by search_frame certainly
cannot be the one taken from the same table.
it should necessarily be the other one coming fro
MakePoint().
> How do I tell if, in fact, the Spatialndex was created and properly
> populated?
>
a quick and fast check:
SELECT * FROM idx_<table>_<geometry>;
where <table> is the name of the Table
and <geometry> is the name of the Column
containing the Geometries.
a more sophisticated method:
SELECT CheckSpatialIndex('table', 'geometry');
if the SpatialIndex is actually present and is
correctly populated it will return 1 (TRUE).
best regards,
Sandro