Spatial predicates and reference systems
Overview
Spatial queries describe relationships between geometries: intersection, containment, adjacency and distance. Coordinate reference systems define how coordinates relate to the world and their units. The local exercise uses synthetic Cartesian coordinates, not longitude and latitude or real-world distances.
First example
SELECT id FROM sr_point WHERE x BETWEEN -5 AND 5 AND y BETWEEN -5 AND 5 ORDER BY id;Observed SQLite result (rows in JSON):
[ [ 1 ], [ 2 ], [ 3 ], [ 5 ]]How it works
A closed rectangle includes its boundary; a strict interior predicate excludes it. A radius predicate uses squared Euclidean distance to avoid square roots. PostGIS geometry uses units of its CRS, while geography distance is measured in meters. ST_SetSRID assigns reference metadata; ST_Transform changes coordinates. Choose compatible CRS and units before comparisons; do not treat degrees as meters.
Practical example
SELECT id FROM sr_point WHERE x*x+y*y<=25 ORDER BY id;Observed SQLite result (rows in JSON):
[ [ 1 ], [ 2 ], [ 5 ]]Practice and mistakes
Practice: return points in the closed square [-5,5] on both axes and compare with the circle of radius 5. Hint 1: use BETWEEN for the box. Hint 2: require xx+yy <=25 for the circle. Point 3 (4,4) is a box match but not a circle match.
PostgreSQL 17 example — NOT RUN
-- Requires PostGIS 3.5.x already installed in a disposable database.SELECT ST_Covers(ST_MakeEnvelope(-5,-5,5,5,0),ST_MakePoint(-5,0)), ST_DWithin(ST_MakePoint(0,0),ST_MakePoint(3,4),5);Expected result, not executed: [[true, true]]