Hi there,I recently switched my spatial analysis workflow from pyGIS (manipulating exclusively shapefiles) to Spatialite w/ pysqlite2. I had no prior experience with SQL but finally managed to set up my first database and execute some simple spatial queries. With that said, I am struggling to fully utilize the SpatialIndex. For example, the concepts of search_frame and ROWID are still elusive to me and I have trouble setting up a proper inner sub-query when invoking the spatial index. Does anyone a good step-by-step guide for idiots?
SELECT ST_Z(utm_point)FROM "berlin_dhhn92_2007"WHERE( -- First condition ( -- Out of the 1.116.000.000 records, select only those that are in range [4 will be returned] -- for a SpatialView, the Primary-Key must be used, otherwise ROWID can be used id_dem IN ( -- Use the Spatialite internal logic to simplify the SQL-Query, avoiding rounding errors that could miss a valid geometry SELECT ROWID FROM SpatialIndex WHERE ( -- Use the created index for the given TABLE -- To query an ATTACHED Database, replace 'main' with the used schema-name -- > 'DB=main.' for a non-ATTACHED Database is not mandatory (f_table_name = 'DB=main.berlin_dhhn92_2007') AND -- Use the given Geometry-Column [mandatory only where there is more than 1 Geometry-Column] (f_geometry_column = 'utm_point') AND -- search an area 0.5 meters around the given point (= 1 meter width/height) (search_frame = ST_Buffer(ST_Transform(MakePoint(24700.552,20674.744,3068),25833),0.5)) ) ) ) AND -- Second condition [will run only against records that fulfill the previous condition(s)] ( -- From the 4 records within the 1 meter range returned, select the nearest that is NOT NULL, insuring a valid result ST_ClosestPoint(utm_point, ST_Transform(MakePoint(24700.552,20674.744,3068),25833)) IS NOT Null) ) -- Order the 4 returned results by distanceORDER BY ST_Distance(utm_point,ST_Transform(MakePoint(24700.552,20674.744,3068),25833)) ASC -- force the return of 1 result, being the nearest pointLIMIT 1;
630745428 391427.000000 5819298.000000 33.560000 33.560000 0.703269630745429 391428.000000 5819298.000000 33.600000 33.600000 0.705196630709428 391427.000000 5819297.000000 33.620000 33.620000 0.709038630709429 391428.000000 5819297.000000 33.610000 33.610000 0.710950
with the agua/blue point being the given point and the four green points, that are partially covered, the points returned by the use of the SpatialIndex.
The circles are 0.5 meters around the points (1 meter width/height).
The final result is the upper-left green point partially covered by the agua/blue point.
Thanks so much,Stefano