Operators These entries document the spatial operators defined by PostGIS. Operators complement the function catalogue by supplying concise symbolic predicates for bounding-box interactions and spatial relationships. Their semantics follow the same geometric rules as the corresponding functions but are optimised for use in SQL expressions and index-supported filtering.
Bounding Box Operators && Returns TRUE if A's 2D bounding box intersects B's 2D bounding box. boolean && geometry A geometry B boolean && geography A geography B Description The && operator returns TRUE if the 2D bounding box of geometry A intersects the 2D bounding box of geometry B. This operand will make use of any indexes that may be available on the geometries. Enhanced: 2.0.0 support for Polyhedral surfaces was introduced. Availability: 1.5.0 support for geography was introduced. &curve_support; &P_support; Examples See Also , , , , , , , &&(geometry,box2df) Returns TRUE if a geometry's (cached) 2D bounding box intersects a 2D float precision bounding box (BOX2DF). boolean && geometry A box2df B Description The && operator returns TRUE if the cached 2D bounding box of geometry A intersects the 2D bounding box B, using float precision. This means that if B is a (double precision) box2d, it will be internally converted to a float precision 2D bounding box (BOX2DF) This operand is intended to be used internally by BRIN indexes, more than by users. Availability: 2.3.0 support for Block Range INdexes (BRIN) was introduced. &curve_support; &P_support; Examples See Also , , , , , , , &&(box2df,geometry) Returns TRUE if a 2D float precision bounding box (BOX2DF) intersects a geometry's (cached) 2D bounding box. boolean && box2df A geometry B Description The && operator returns TRUE if the 2D bounding box A intersects the cached 2D bounding box of geometry B, using float precision. This means that if A is a (double precision) box2d, it will be internally converted to a float precision 2D bounding box (BOX2DF) This operand is intended to be used internally by BRIN indexes, more than by users. Availability: 2.3.0 support for Block Range INdexes (BRIN) was introduced. &curve_support; &P_support; Examples See Also , , , , , , , &&(box2df,box2df) Returns TRUE if two 2D float precision bounding boxes (BOX2DF) intersect each other. boolean && box2df A box2df B Description The && operator returns TRUE if two 2D bounding boxes A and B intersect each other, using float precision. This means that if A (or B) is a (double precision) box2d, it will be internally converted to a float precision 2D bounding box (BOX2DF) This operator is intended to be used internally by BRIN indexes, more than by users. Availability: 2.3.0 support for Block Range INdexes (BRIN) was introduced. &curve_support; &P_support; Examples See Also , , , , , , , &&& Returns TRUE if A's n-D bounding box intersects B's n-D bounding box. boolean &&& geometry A geometry B Description The &&& operator returns TRUE if the n-D bounding box of geometry A intersects the n-D bounding box of geometry B. This operand will make use of any indexes that may be available on the geometries. Availability: 2.0.0 &curve_support; &P_support; &T_support; &Z_support; Examples 3D LineStrings. 3M LineStrings. See Also &&&(geometry,gidx) Returns TRUE if a geometry's (cached) n-D bounding box intersects a n-D float precision bounding box (GIDX). boolean &&& geometry A gidx B Description The &&& operator returns TRUE if the cached n-D bounding box of geometry A intersects the n-D bounding box B, using float precision. This means that if B is a (double precision) box3d, it will be internally converted to a float precision 3D bounding box (GIDX) This operator is intended to be used internally by BRIN indexes, more than by users. Availability: 2.3.0 support for Block Range INdexes (BRIN) was introduced. &curve_support; &P_support; &T_support; &Z_support; Examples See Also , &&&(gidx,geometry) Returns TRUE if a n-D float precision bounding box (GIDX) intersects a geometry's (cached) n-D bounding box. boolean &&& gidx A geometry B Description The &&& operator returns TRUE if the n-D bounding box A intersects the cached n-D bounding box of geometry B, using float precision. This means that if A is a (double precision) box3d, it will be internally converted to a float precision 3D bounding box (GIDX) This operator is intended to be used internally by BRIN indexes, more than by users. Availability: 2.3.0 support for Block Range INdexes (BRIN) was introduced. &curve_support; &P_support; &T_support; &Z_support; Examples See Also , &&&(gidx,gidx) Returns TRUE if two n-D float precision bounding boxes (GIDX) intersect each other. boolean &&& gidx A gidx B Description The &&& operator returns TRUE if two n-D bounding boxes A and B intersect each other, using float precision. This means that if A (or B) is a (double precision) box3d, it will be internally converted to a float precision 3D bounding box (GIDX) This operator is intended to be used internally by BRIN indexes, more than by users. Availability: 2.3.0 support for Block Range INdexes (BRIN) was introduced. &curve_support; &P_support; &T_support; &Z_support; Examples See Also , &< Returns TRUE if A's bounding box overlaps or is to the left of B's. boolean &< geometry A geometry B Description The &< operator returns TRUE if the bounding box of geometry A overlaps or is to the left of the bounding box of geometry B, or more accurately, overlaps or is NOT to the right of the bounding box of geometry B. This operand will make use of any indexes that may be available on the geometries. Examples See Also , , , &<| Returns TRUE if A's bounding box overlaps or is below B's. boolean &<| geometry A geometry B Description The &<| operator returns TRUE if the bounding box of geometry A overlaps or is below of the bounding box of geometry B, or more accurately, overlaps or is NOT above the bounding box of geometry B. &curve_support; &P_support; This operand will make use of any indexes that may be available on the geometries. Examples See Also , , , &> Returns TRUE if A' bounding box overlaps or is to the right of B's. boolean &> geometry A geometry B Description The &> operator returns TRUE if the bounding box of geometry A overlaps or is to the right of the bounding box of geometry B, or more accurately, overlaps or is NOT to the left of the bounding box of geometry B. This operand will make use of any indexes that may be available on the geometries. Examples tbl2.column2 AS overright FROM ( VALUES (1, 'LINESTRING(1 2,4 6)'::geometry)) AS tbl1, ( VALUES (2, 'LINESTRING(0 0,3 3)'::geometry), (3, 'LINESTRING(0 1,0 5)'::geometry), (4, 'LINESTRING(6 0,6 1)'::geometry)) AS tbl2; ]]> See Also , , , << Returns TRUE if A's bounding box is strictly to the left of B's. boolean << geometry A geometry B Description The << operator returns TRUE if the bounding box of geometry A is strictly to the left of the bounding box of geometry B. This operand will make use of any indexes that may be available on the geometries. Examples See Also , , <<| Returns TRUE if A's bounding box is strictly below B's. boolean <<| geometry A geometry B Description The <<| operator returns TRUE if the bounding box of geometry A is strictly below the bounding box of geometry B. This operand will make use of any indexes that may be available on the geometries. Examples See Also , , = Returns TRUE if the coordinates and coordinate order geometry/geography A are the same as the coordinates and coordinate order of geometry/geography B. boolean = geometry A geometry B boolean = geography A geography B Description The = operator returns TRUE if the coordinates and coordinate order geometry/geography A are the same as the coordinates and coordinate order of geometry/geography B. PostgreSQL uses the =, <, and > operators defined for geometries to perform internal orderings and comparison of geometries (ie. in a GROUP BY or ORDER BY clause). Only geometry/geography that are exactly equal in all respects, with the same coordinates, in the same order, are considered equal by this operator. For "spatial equality", that ignores things like coordinate order, and can detect features that cover the same spatial area with different representations, use or This operand will NOT make use of any indexes that may be available on the geometries. For an index assisted exact equality test, combine = with &&. Changed: 2.4.0, in prior versions this was bounding box equality not a geometric equality. If you need bounding box equality, use instead. &curve_support; &P_support; Examples SELECT 'LINESTRING(0 0,0 1,1 0)'::geometry = 'LINESTRING(1 1,0 0)'::geometry; Note: the GROUP BY uses the "=" to compare for geometry equivalency. In versions prior to 2.0, this used to return true. See Also , , >> Returns TRUE if A's bounding box is strictly to the right of B's. boolean >> geometry A geometry B Description The >> operator returns TRUE if the bounding box of geometry A is strictly to the right of the bounding box of geometry B. This operand will make use of any indexes that may be available on the geometries. Examples > tbl2.column2 AS right FROM ( VALUES (1, 'LINESTRING (2 3,5 6)'::geometry)) AS tbl1, ( VALUES (2, 'LINESTRING (1 4,1 7)'::geometry), (3, 'LINESTRING (6 1,6 5)'::geometry), (4, 'LINESTRING (0 0,4 3)'::geometry)) AS tbl2; ]]> See Also , , @ Returns TRUE if A's bounding box is contained by B's. boolean @ geometry A geometry B Description The @ operator returns TRUE if the bounding box of geometry A is completely contained by the bounding box of geometry B. This operand will make use of any indexes that may be available on the geometries. Examples See Also , @(geometry,box2df) Returns TRUE if a geometry's 2D bounding box is contained into a 2D float precision bounding box (BOX2DF). boolean @ geometry A box2df B Description The @ operator returns TRUE if the A geometry's 2D bounding box is contained the 2D bounding box B, using float precision. This means that if B is a (double precision) box2d, it will be internally converted to a float precision 2D bounding box (BOX2DF) This operand is intended to be used internally by BRIN indexes, more than by users. Availability: 2.3.0 support for Block Range INdexes (BRIN) was introduced. &curve_support; &P_support; Examples SELECT ST_Buffer('POINT(2 2)'::geometry, 1) @ ST_MakeBox2D(ST_Point(0, 0), ST_Point(5, 5)) AS is_contained; t See Also , , , , , , , @(box2df,geometry) Returns TRUE if a 2D float precision bounding box (BOX2DF) is contained into a geometry's 2D bounding box. boolean @ box2df A geometry B Description The @ operator returns TRUE if the 2D bounding box A is contained into the B geometry's 2D bounding box, using float precision. This means that if B is a (double precision) box2d, it will be internally converted to a float precision 2D bounding box (BOX2DF) This operand is intended to be used internally by BRIN indexes, more than by users. Availability: 2.3.0 support for Block Range INdexes (BRIN) was introduced. &curve_support; &P_support; Examples SELECT ST_MakeBox2D(ST_Point(2, 2), ST_Point(3, 3)) @ ST_Buffer('POINT(1 1)'::geometry, 10) AS is_contained; t See Also , , , , , , , @(box2df,box2df) Returns TRUE if a 2D float precision bounding box (BOX2DF) is contained into another 2D float precision bounding box. boolean @ box2df A box2df B Description The @ operator returns TRUE if the 2D bounding box A is contained into the 2D bounding box B, using float precision. This means that if A (or B) is a (double precision) box2d, it will be internally converted to a float precision 2D bounding box (BOX2DF) This operand is intended to be used internally by BRIN indexes, more than by users. Availability: 2.3.0 support for Block Range INdexes (BRIN) was introduced. &curve_support; &P_support; Examples SELECT ST_MakeBox2D(ST_Point(2, 2), ST_Point(3, 3)) @ ST_MakeBox2D(ST_Point(0, 0), ST_Point(5, 5)) AS is_contained; t See Also , , , , , , , |&> Returns TRUE if A's bounding box overlaps or is above B's. boolean |&> geometry A geometry B Description The |&> operator returns TRUE if the bounding box of geometry A overlaps or is above the bounding box of geometry B, or more accurately, overlaps or is NOT below the bounding box of geometry B. This operand will make use of any indexes that may be available on the geometries. Examples tbl2.column2 AS overabove FROM ( VALUES (1, 'LINESTRING(6 0,6 4)'::geometry)) AS tbl1, ( VALUES (2, 'LINESTRING(0 0,3 3)'::geometry), (3, 'LINESTRING(0 1,0 5)'::geometry), (4, 'LINESTRING(1 2,4 6)'::geometry)) AS tbl2; ]]> See Also , , , |>> Returns TRUE if A's bounding box is strictly above B's. boolean |>> geometry A geometry B Description The |>> operator returns TRUE if the bounding box of geometry A is strictly above the bounding box of geometry B. This operand will make use of any indexes that may be available on the geometries. Examples SELECT tbl1.column1, tbl2.column1, tbl1.column2 |>> tbl2.column2 AS above FROM ( VALUES (1, 'LINESTRING (1 4,1 7)'::geometry)) AS tbl1, ( VALUES (2, 'LINESTRING (0 0,4 2)'::geometry), (3, 'LINESTRING (6 1,6 5)'::geometry), (4, 'LINESTRING (2 3,5 6)'::geometry)) AS tbl2; column1 | column1 | above ---------+---------+------- 1 | 2 | t 1 | 3 | f 1 | 4 | f (3 rows) See Also , , ~ Returns TRUE if A's bounding box contains B's. boolean ~ geometry A geometry B Description The ~ operator returns TRUE if the bounding box of geometry A completely contains the bounding box of geometry B. This operand will make use of any indexes that may be available on the geometries. Examples SELECT tbl1.column1, tbl2.column1, tbl1.column2 ~ tbl2.column2 AS contains FROM ( VALUES (1, 'LINESTRING (0 0,3 3)'::geometry)) AS tbl1, ( VALUES (2, 'LINESTRING (0 0,4 4)'::geometry), (3, 'LINESTRING (1 1,2 2)'::geometry), (4, 'LINESTRING (0 0,3 3)'::geometry)) AS tbl2; column1 | column1 | contains ---------+---------+---------- 1 | 2 | f 1 | 3 | t 1 | 4 | t (3 rows) See Also , ~(geometry,box2df) Returns TRUE if a geometry's 2D bonding box contains a 2D float precision bounding box (GIDX). boolean ~ geometry A box2df B Description The ~ operator returns TRUE if the 2D bounding box of a geometry A contains the 2D bounding box B, using float precision. This means that if B is a (double precision) box2d, it will be internally converted to a float precision 2D bounding box (BOX2DF) This operand is intended to be used internally by BRIN indexes, more than by users. Availability: 2.3.0 support for Block Range INdexes (BRIN) was introduced. &curve_support; &P_support; Examples SELECT ST_Buffer('POINT(1 1)'::geometry, 10) ~ ST_MakeBox2D(ST_Point(0, 0), ST_Point(2, 2)) AS contains; t See Also , , , , , , , ~(box2df,geometry) Returns TRUE if a 2D float precision bounding box (BOX2DF) contains a geometry's 2D bonding box. boolean ~ box2df A geometry B Description The ~ operator returns TRUE if the 2D bounding box A contains the B geometry's bounding box, using float precision. This means that if A is a (double precision) box2d, it will be internally converted to a float precision 2D bounding box (BOX2DF) This operand is intended to be used internally by BRIN indexes, more than by users. Availability: 2.3.0 support for Block Range INdexes (BRIN) was introduced. &curve_support; &P_support; Examples SELECT ST_MakeBox2D(ST_Point(0, 0), ST_Point(5, 5)) ~ ST_Buffer('POINT(2 2)'::geometry, 1) AS contains; t See Also , , , , , , , ~(box2df,box2df) Returns TRUE if a 2D float precision bounding box (BOX2DF) contains another 2D float precision bounding box (BOX2DF). boolean ~ box2df A box2df B Description The ~ operator returns TRUE if the 2D bounding box A contains the 2D bounding box B, using float precision. This means that if A is a (double precision) box2d, it will be internally converted to a float precision 2D bounding box (BOX2DF) This operand is intended to be used internally by BRIN indexes, more than by users. Availability: 2.3.0 support for Block Range INdexes (BRIN) was introduced. &curve_support; &P_support; Examples SELECT ST_MakeBox2D(ST_Point(0, 0), ST_Point(5, 5)) ~ ST_MakeBox2D(ST_Point(2, 2), ST_Point(3, 3)) AS contains; t See Also , , , , , , , ~= Returns TRUE if A's bounding box is the same as B's. boolean ~= geometry A geometry B Description The ~= operator returns TRUE if the bounding box of geometry/geography A is the same as the bounding box of geometry/geography B. This operand will make use of any indexes that may be available on the geometries. Availability: 1.5.0 changed behavior &P_support; This operator has changed behavior in PostGIS 1.5 from testing for actual geometric equality to only checking for bounding box equality. To complicate things it also depends on if you have done a hard or soft upgrade which behavior your database has. To find out which behavior your database has you can run the query below. To check for true equality use or . Examples SELECT 'LINESTRING(0 0,1 1)'::geometry ~= 'LINESTRING(0 1,1 0)'::geometry AS equality; t See Also , ,
Distance Operators <-> Returns the 2D distance between A and B. double precision <-> geometry A geometry B double precision <-> geography A geography B Description The <-> operator returns the 2D distance between two geometries. Used in the "ORDER BY" clause provides index-assisted nearest-neighbor result sets with true KNN distance search for geometries, and distance on the sphere for geographies. This operand will make use of 2D GiST indexes that may be available on the geometries. It is different from other operators that use spatial indexes in that the spatial index is only used when the operator is in the ORDER BY clause. Index only kicks in if one of the geometries is a constant (not in a subquery/cte). e.g. 'SRID=3005;POINT(1011102 450541)'::geometry instead of a.geom Refer to PostGIS workshop: Nearest-Neighbor Searching for a detailed example. Enhanced: 2.2.0 -- True KNN ("K nearest neighbor") behavior for geometry and geography. Note for geography KNN is based on sphere rather than spheroid. Changed: 2.2.0 -- Old hybrid-syntax workarounds may be slower once true KNN is available. See examples below. Availability: 2.0.0 -- Weak KNN provides nearest neighbors based on geometry centroid distances instead of true distances. Exact results for points, inexact for all other types. Examples SELECT ST_Distance(geom, 'SRID=3005;POINT(1011102 450541)'::geometry) as d, edabbr, vaabbr FROM va2005 ORDER BY d limit 10; d | edabbr | vaabbr ------------------+--------+-------- 0 | ALQ | 128 5541.57712511724 | ALQ | 129A 5579.67450712005 | ALQ | 001 6083.4207708641 | ALQ | 131 7691.2205404848 | ALQ | 003 7900.75451037313 | ALQ | 122 8694.20710669982 | ALQ | 129B 9564.24289057111 | ALQ | 130 12089.665931705 | ALQ | 127 18472.5531479404 | ALQ | 002 (10 rows) Then the KNN raw answer: 'SRID=3005;POINT(1011102 450541)'::geometry limit 10; ]]> If you run "EXPLAIN ANALYZE" on the two queries you would see a performance improvement for the second. When a plan cannot produce exact distances directly, use a hybrid query to find the true nearest neighbors. First a CTE query using the index-assisted KNN, then an exact query to get correct ordering: 'SRID=3005;POINT(1011102 450541)'::geometry LIMIT 100) SELECT * FROM index_query ORDER BY d limit 10; ]]> See Also , , |=| Returns the distance between A and B trajectories at their closest point of approach. double precision |=| geometry A geometry B Description The |=| operator returns the 3D distance between two trajectories (See ). This is the same as but as an operator it can be used for doing nearest neighbor searches using an N-dimensional index. This operand will make use of ND GiST indexes that may be available on the geometries. It is different from other operators that use spatial indexes in that the spatial index is only used when the operator is in the ORDER BY clause. Index only kicks in if one of the geometries is a constant (not in a subquery/cte). e.g. 'SRID=3005;LINESTRINGM(0 0 0,0 0 1)'::geometry instead of a.geom Availability: 2.2.0. Examples This example orders stored trajectories by their distance from a literal query trajectory. \set qt 'ST_AddMeasure(ST_MakeLine(ST_MakePointM(-350, 300, 0), ST_MakePointM(-410, 490, 0)), 10, 20)' SELECT track_id, dist FROM ( SELECT track_id, ST_DistanceCPA(tr, :qt) dist FROM trajectories ORDER BY tr |=| :qt LIMIT 5 ) foo; track_id dist ----------+------------------- 395 | 0.576496831518066 380 | 5.06797130410151 390 | 7.72262293958322 385 | 9.8004461358071 405 | 10.9534397988433 (5 rows) See Also , , <#> Returns the 2D distance between A and B bounding boxes. double precision <#> geometry A geometry B Description The <#> operator returns distance between two floating point bounding boxes, possibly reading them from a spatial index. Useful for doing nearest neighbor approximate distance ordering. This operand will make use of any indexes that may be available on the geometries. It is different from other operators that use spatial indexes in that the spatial index is only used when the operator is in the ORDER BY clause. Index only kicks in if one of the geometries is a constant e.g. ORDER BY (ST_GeomFromText('POINT(1 2)') <#> geom) instead of g1.geom <#>. Availability: 2.0.0. Examples ST_GeomFromText('LINESTRING(746149 2948672,745954 2948576, 745787 2948499,745740 2948468,745712 2948438, 745690 2948384,745677 2948319)', 2249) As b_dist, ST_Distance(b.geom, ST_GeomFromText('LINESTRING(746149 2948672,745954 2948576, 745787 2948499,745740 2948468,745712 2948438, 745690 2948384,745677 2948319)', 2249)) As act_dist FROM bos_roads As b ORDER BY b_dist, b.tlid LIMIT 100) As foo ORDER BY act_dist, tlid LIMIT 10; ]]> See Also , , <<->> Returns the n-D distance between the A and B geometries or bounding boxes double precision <<->> geometry A geometry B Description The <<->> operator returns the n-D (euclidean) distance between the centroids of the bounding boxes of two geometries. Useful for doing nearest neighbor approximate distance ordering. This operand will make use of n-D GiST indexes that may be available on the geometries. It is different from other operators that use spatial indexes in that the spatial index is only used when the operator is in the ORDER BY clause. Index only kicks in if one of the geometries is a constant (not in a subquery/cte). e.g. 'SRID=3005;POINT(1011102 450541)'::geometry instead of a.geom Availability: 2.2.0. See Also