Spatial Relationships These functions determine spatial relationships between geometries.
Topological Relationships ST_3DIntersects Tests if two geometries spatially intersect in 3D - only for points, linestrings, polygons, polyhedral surface (area) boolean ST_3DIntersects geometry geomA geometry geomB Description Overlaps, Touches, Within all imply spatial intersection. If any of the aforementioned returns true, then the geometries also spatially intersect. Disjoint implies false for spatial intersection. &index_aware; Because of floating robustness failures, geometries don't always intersect as you'd expect them to after geometric processing. For example the closest point on a linestring to a geometry may not lie on the linestring. For these kind of issues where a distance of a centimeter you want to just consider as intersecting, use . Changed: 3.0.0 SFCGAL backend removed, GEOS backend supports TINs. Availability: 2.0.0 &Z_support; &P_support; &T_support; &sqlmm_compliant; SQL-MM IEC 13249-3: 5.1 Geometry Examples SELECT ST_3DIntersects(pt, line), ST_Intersects(pt, line) FROM (SELECT 'POINT(0 0 2)'::geometry As pt, 'LINESTRING (0 0 1,0 2 3)'::geometry As line) As foo; st_3dintersects | st_intersects -----------------+--------------- f | t (1 row) TIN Examples SELECT ST_3DIntersects('TIN(((0 0 0,1 0 0,0 1 0,0 0 0)))'::geometry, 'POINT(.1 .1 0)'::geometry); t See Also , ST_Contains Tests if every point of B lies in A, and their interiors have a point in common boolean ST_Contains geometry geomA geometry geomB Description Returns TRUE if geometry A contains geometry B. A contains B if and only if all points of B lie inside (i.e. in the interior or boundary of) A (or equivalently, no points of B lie in the exterior of A), and the interiors of A and B have at least one point in common. In mathematical terms: ST_Contains(A, B) ⇔ (A ⋂ B = B) ∧ (Int(A) ⋂ Int(B) ≠ ∅) The contains relationship is reflexive: every geometry contains itself. (In contrast, in the predicate a geometry does not properly contain itself.) The relationship is antisymmetric: if ST_Contains(A,B) = true and ST_Contains(B,A) = true, then the two geometries must be topologically equal (ST_Equals(A,B) = true). ST_Contains is the converse of . So, ST_Contains(A,B) = ST_Within(B,A). Because the interiors must have a common point, a subtlety of the definition is that polygons and lines do not contain lines and points lying fully in their boundary. For further details see Subtleties of OGC Covers, Contains, Within. The predicate provides a more inclusive relationship. &index_aware; To avoid index use, use the function _ST_Contains. Performed by the GEOS module Enhanced: 2.3.0 Enhancement to PIP short-circuit extended to support MultiPoints with few points. Prior versions only supported point in polygon. Enhanced: 3.0.0 enabled support for GEOMETRYCOLLECTION Do not use this function with invalid geometries. You will get unexpected results. NOTE: this is the "allowable" version that returns a boolean, not an integer. &sfs_compliant; s2.1.1.2 // s2.1.13.3 - same as within(geometry B, geometry A) &sqlmm_compliant; SQL-MM 3: 5.1.31 Examples ST_Contains returns TRUE in the following situations: A LineString contains a MultiPoint whose points lie in its interior or boundary. SELECT ST_Contains(a, b) FROM (SELECT 'LINESTRING (10 190,60 80,130 120,190 10)'::geometry AS a, 'MULTIPOINT (60 80,190 10)'::geometry AS b ) AS example; t A Polygon contains a Point in its interior. SELECT ST_Contains(a, b) FROM (SELECT 'POLYGON ((10 130,50 190,110 190,140 150,150 80,100 10,20 40,10 130))'::geometry AS a, 'POINT (99 130)'::geometry AS b ) AS example; t A Polygon contains a LineString with interior points inside the Polygon. SELECT ST_Contains(a, b) FROM (SELECT 'POLYGON ((10 130,50 190,110 190,140 150,150 80,100 10,20 40,10 130))'::geometry AS a, 'LINESTRING (50 190,110 190,99 130)'::geometry AS b ) AS example; t A Polygon contains another Polygon in its interior. SELECT ST_Contains(a, b) FROM (SELECT 'POLYGON ((10 130,50 190,110 190,140 150,150 80,100 10,20 40,10 130))'::geometry AS a, 'POLYGON ((70 40,100 50,120 80,80 110,50 90,70 40))'::geometry AS b ) AS example; t ST_Contains returns FALSE in the following situations: A Polygon does not contain a MultiPoint when one point lies in a hole. SELECT ST_Contains(a, b) FROM (SELECT 'POLYGON ((10 130,50 190,110 190,140 150,150 80,100 10,20 40,10 130),(70 40,100 50,120 80,80 110,50 90,70 40))'::geometry AS a, 'MULTIPOINT (99 130,87 70)'::geometry AS b ) AS example; f A Polygon does not contain a LineString extending outside it. SELECT ST_Contains(a, b) FROM (SELECT 'POLYGON ((10 130,50 190,110 190,140 150,150 80,100 10,20 40,10 130))'::geometry AS a, 'LINESTRING (99 130,170 131)'::geometry AS b ) AS example; f Due to the interior intersection condition ST_Contains returns FALSE in the following situations (whereas ST_Covers returns TRUE): A LineString does not contain a Point lying only on its boundary. SELECT ST_Contains(a, b), ST_Covers(a, b) FROM (SELECT 'LINESTRING (10 190,60 80,130 120,190 10)'::geometry AS a, 'POINT (190 10)'::geometry AS b ) AS example; f | t A Polygon does not contain a LineString lying entirely on its boundary. SELECT ST_Contains(a, b), ST_Covers(a, b) FROM (SELECT 'POLYGON ((10 130,50 190,110 190,140 150,150 80,100 10,20 40,10 130))'::geometry AS a, 'LINESTRING (80 190,110 190,140 150,150 80)'::geometry AS b ) AS example; f | t This example tests a circle within another circle. WITH circles AS ( SELECT ST_Buffer(center, 10) AS smallc, ST_Buffer(center, 20) AS bigc FROM (SELECT 'POINT(1 2)'::geometry AS center) AS p ) SELECT ST_Contains(smallc, bigc) AS smallcontainsbig, ST_Contains(bigc, smallc) AS bigcontainssmall, ST_Contains(bigc, ST_Union(smallc, bigc)) AS bigcontainsunion, ST_Equals(bigc, ST_Union(smallc, bigc)) AS bigisunion, ST_Covers(bigc, ST_ExteriorRing(bigc)) AS bigcoversexterior, ST_Contains(bigc, ST_ExteriorRing(bigc)) AS bigcontainsexterior FROM circles; -[ RECORD 1 ]----- smallcontainsbig | f bigcontainssmall | t bigcontainsunion | t bigisunion | t bigcoversexterior | t bigcontainsexterior | f This example demonstrates the difference between contains and contains-properly predicates. geomtype | acontainsa | acontainspropa | acontainsba | acontainspropba --------------+------------+----------------+-------------+----------------- ST_Polygon | t | f | f | f ST_LineString | t | f | f | f ST_Point | t | t | f | f See Also , , , , , ST_ContainsProperly Tests if every point of B lies in the interior of A boolean ST_ContainsProperly geometry geomA geometry geomB Description Returns true if every point of B lies in the interior of A (or equivalently, no point of B lies in the the boundary or exterior of A). In mathematical terms: ST_ContainsProperly(A, B) ⇔ Int(A) ⋂ B = B A contains B properly if the DE-9IM Intersection Matrix for the two geometries matches [T**FF*FF*] A does not properly contain itself, but does contain itself. A use for this predicate is computing the intersections of a set of geometries with a large polygonal geometry. Since intersection is a fairly slow operation, it can be more efficient to use containsProperly to filter out test geometries which lie fully inside the area. In these cases the intersection is known a priori to be exactly the original test geometry. &index_aware; To avoid index use, use the function _ST_ContainsProperly. The advantage of this predicate over and is that it can be computed more efficiently, with no need to compute topology at individual points. Performed by the GEOS module. Availability: 1.4.0 Enhanced: 3.0.0 enabled support for GEOMETRYCOLLECTION Do not use this function with invalid geometries. You will get unexpected results. Examples This example tests a circle within another circle. WITH circles AS ( SELECT ST_Buffer(center, 10) AS smallc, ST_Buffer(center, 20) AS bigc FROM (SELECT 'POINT(1 2)'::geometry AS center) AS p ) SELECT ST_ContainsProperly(smallc, bigc) AS smallcontainspropbig, ST_ContainsProperly(bigc, smallc) AS bigcontainspropsmall, ST_ContainsProperly(bigc, ST_Union(smallc, bigc)) AS bigcontainspropunion, ST_Equals(bigc, ST_Union(smallc, bigc)) AS bigisunion, ST_Covers(bigc, ST_ExteriorRing(bigc)) AS bigcoversexterior, ST_ContainsProperly(bigc, ST_ExteriorRing(bigc)) AS bigcontainsexterior FROM circles; -[ RECORD 1 ]----- smallcontainspropbig | f bigcontainspropsmall | t bigcontainspropunion | f bigisunion | t bigcoversexterior | t bigcontainsexterior | f This example demonstrates the difference between contains and contains-properly predicates. WITH input(geom) AS (VALUES (ST_Buffer(ST_Point(1, 1), 5, 1)), (ST_MakeLine(ST_Point(1, 1), ST_Point(-1, -1))), (ST_Point(1, 1)) ) SELECT ST_GeometryType(geom) AS geomtype, ST_Contains(geom, geom) AS acontainsa, ST_ContainsProperly(geom, geom) AS acontainspropa, ST_Contains(geom, ST_Boundary(geom)) AS acontainsba, ST_ContainsProperly(geom, ST_Boundary(geom)) AS acontainspropba FROM input; geomtype | acontainsa | acontainspropa | acontainsba | acontainspropba --------------+------------+----------------+-------------+----------------- ST_Polygon | t | f | f | f ST_LineString | t | f | f | f ST_Point | t | t | f | f See Also , , , , , , , ST_CoveredBy Tests if every point of A lies in B boolean ST_CoveredBy geometry geomA geometry geomB boolean ST_CoveredBy geography geogA geography geogB Description Returns true if every point in Geometry/Geography A lies inside (i.e. intersects the interior or boundary of) Geometry/Geography B. Equivalently, tests that no point of A lies outside (in the exterior of) B. In mathematical terms: ST_CoveredBy(A, B) ⇔ A ⋂ B = A ST_CoveredBy is the converse of . So, ST_CoveredBy(A,B) = ST_Covers(B,A). Generally this function should be used instead of , since it has a simpler definition which does not have the quirk that "boundaries are not within their geometry". &index_aware; To avoid index use, use the function _ST_CoveredBy. Enhanced: 3.0.0 enabled support for GEOMETRYCOLLECTION Do not use this function with invalid geometries. You will get unexpected results. Performed by the GEOS module Availability: 1.2.2 NOTE: this is the "allowable" version that returns a boolean, not an integer. Not an OGC standard, but Oracle has it too. Examples This example tests a circle covered by another circle. WITH circles AS ( SELECT ST_Buffer(center, 10) AS smallc, ST_Buffer(center, 20) AS bigc FROM (SELECT 'POINT(1 2)'::geometry AS center) AS p ) SELECT ST_CoveredBy(smallc, smallc) AS smallinsmall, ST_CoveredBy(smallc, bigc) AS smallcoveredbybig, ST_CoveredBy(ST_ExteriorRing(bigc), bigc) AS exteriorcoveredbybig, ST_Within(ST_ExteriorRing(bigc), bigc) AS exteriorwithinbig FROM circles; smallinsmall | smallcoveredbybig | exteriorcoveredbybig | exteriorwithinbig --------------+-------------------+----------------------+------------------ t | t | t | f (1 row) See Also , , , ST_Covers Tests if every point of B lies in A boolean ST_Covers geometry geomA geometry geomB boolean ST_Covers geography geogpolyA geography geogpointB Description Returns true if every point in Geometry/Geography B lies inside (i.e. intersects the interior or boundary of) Geometry/Geography A. Equivalently, tests that no point of B lies outside (in the exterior of) A. In mathematical terms: ST_Covers(A, B) ⇔ A ⋂ B = B ST_Covers is the converse of . So, ST_Covers(A,B) = ST_CoveredBy(B,A). Generally this function should be used instead of , since it has a simpler definition which does not have the quirk that "geometries do not contain their boundary". &index_aware; To avoid index use, use the function _ST_Covers. Enhanced: 3.0.0 enabled support for GEOMETRYCOLLECTION Do not use this function with invalid geometries. You will get unexpected results. Performed by the GEOS module Enhanced: 2.4.0 Support for polygon in polygon and line in polygon added for geography type Enhanced: 2.3.0 Enhancement to PIP short-circuit for geometry extended to support MultiPoints with few points. Prior versions only supported point in polygon. Availability: 1.5 - support for geography was introduced. Availability: 1.2.2 NOTE: this is the "allowable" version that returns a boolean, not an integer. Not an OGC standard, but Oracle has it too. Examples Geometry example This example tests a circle covering another circle. WITH circles AS ( SELECT ST_Buffer(center, 10) AS smallc, ST_Buffer(center, 20) AS bigc FROM (SELECT 'POINT(1 2)'::geometry AS center) AS p ) SELECT ST_Covers(smallc, smallc) AS smallinsmall, ST_Covers(smallc, bigc) AS smallcoversbig, ST_Covers(bigc, ST_ExteriorRing(bigc)) AS bigcoversexterior, ST_Contains(bigc, ST_ExteriorRing(bigc)) AS bigcontainsexterior FROM circles; smallinsmall | smallcoversbig | bigcoversexterior | bigcontainsexterior --------------+----------------+-------------------+--------------------- t | f | t | f (1 row) Geography example This example compares a point with a 300-meter buffer against a point and against a 10-meter buffer around that point. SELECT ST_Covers(geog_poly, geog_pt) As poly_covers_pt, ST_Covers(ST_Buffer(geog_pt, 10), geog_pt) As buff_10m_covers_cent FROM (SELECT ST_Buffer('SRID=4326;POINT(-99.327 31.4821)'::geography, 300) As geog_poly, 'SRID=4326;POINT(-99.33 31.483)'::geography As geog_pt ) As foo; poly_covers_pt | buff_10m_covers_cent ----------------+------------------ f | t See Also , , ST_Crosses Tests if two geometries have some, but not all, interior points in common boolean ST_Crosses geometry g1 geometry g2 Description Compares two geometry objects and returns true if their intersection "spatially crosses"; that is, the geometries have some, but not all interior points in common. The intersection of the interiors of the geometries must be non-empty and must have dimension less than the maximum dimension of the two input geometries, and the intersection of the two geometries must not equal either geometry. Otherwise, it returns false. The crosses relation is symmetric and irreflexive. In mathematical terms: ST_Crosses(A, B) ⇔ (dim( Int(A) ⋂ Int(B) ) < max( dim( Int(A) ), dim( Int(B) ) )) ∧ (A ⋂ B ≠ A) ∧ (A ⋂ B ≠ B) Geometries cross if their DE-9IM Intersection Matrix matches: T*T****** for Point/Line, Point/Area, and Line/Area situations T*****T** for Line/Point, Area/Point, and Area/Line situations 0******** for Line/Line situations the result is false for Point/Point and Area/Area situations The OpenGIS Simple Features Specification defines this predicate only for Point/Line, Point/Area, Line/Line, and Line/Area situations. JTS / GEOS extends the definition to apply to Line/Point, Area/Point and Area/Line situations as well. This makes the relation symmetric. &index_aware; Enhanced: 3.0.0 enabled support for GEOMETRYCOLLECTION &sfs_compliant; s2.1.13.3 &sqlmm_compliant; SQL-MM 3: 5.1.29 Examples The following situations all return true. A LineString crosses a MultiPoint when some, but not all, points lie on the line. SELECT ST_Crosses(a, b) FROM (SELECT 'LINESTRING (10 190,60 80,130 120,190 10)'::geometry AS a, 'MULTIPOINT (80 170,120 13,130 120,181 142)'::geometry AS b ) AS example; t A Polygon crosses a MultiPoint when some, but not all, points lie in its interior. SELECT ST_Crosses(a, b) FROM (SELECT 'POLYGON ((30 190,10 100,10 10,90 20,110 70,80 130,30 190))'::geometry AS a, 'MULTIPOINT (56 60,120 13,130 119,181 142)'::geometry AS b ) AS example; t A LineString crosses a Polygon when part of the line lies inside and part outside. SELECT ST_Crosses(a, b) FROM (SELECT 'POLYGON ((30 190,10 100,10 10,90 20,110 70,80 130,30 190))'::geometry AS a, 'LINESTRING (30 40,70 50,120 150,190 190)'::geometry AS b ) AS example; t Two LineStrings cross when they share an interior point. SELECT ST_Crosses(a, b) FROM (SELECT 'LINESTRING (10 190,60 80,130 120,190 10)'::geometry AS a, 'LINESTRING (10 10,70 30,110 120,190 190)'::geometry AS b ) AS example; t Consider a situation where a user has two tables: a table of roads and a table of highways. CREATE TABLE roads ( id serial NOT NULL, geom geometry, CONSTRAINT roads_pkey PRIMARY KEY (road_id) ); CREATE TABLE highways ( id serial NOT NULL, the_gem geometry, CONSTRAINT roads_pkey PRIMARY KEY (road_id) ); To determine a list of roads that cross a highway, use a query similar to: SELECT roads.id FROM roads, highways WHERE ST_Crosses(roads.geom, highways.geom); See Also , ST_Disjoint Tests if two geometries have no points in common boolean ST_Disjoint geometry A geometry B Description Returns true if two geometries are disjoint. Geometries are disjoint if they have no point in common. If any other spatial relationship is true for a pair of geometries, they are not disjoint. Disjoint implies that is false. In mathematical terms: ST_Disjoint(A, B) ⇔ A ⋂ B = ∅ Enhanced: 3.0.0 enabled support for GEOMETRYCOLLECTION Performed by the GEOS module This function call does not use indexes. A negated predicate can be used as a more performant alternative that uses indexes: ST_Disjoint(A,B) = NOT ST_Intersects(A,B) NOTE: this is the "allowable" version that returns a boolean, not an integer. &sfs_compliant; s2.1.1.2 //s2.1.13.3 - a.Relate(b, 'FF*FF****') &sqlmm_compliant; SQL-MM 3: 5.1.26 Examples SELECT ST_Disjoint('POINT(0 0)'::geometry, 'LINESTRING ( 2 0,0 2 )'::geometry); t SELECT ST_Disjoint('POINT(0 0)'::geometry, 'LINESTRING ( 0 0,0 2 )'::geometry); f See Also ST_Equals Tests if two geometries include the same set of points boolean ST_Equals geometry A geometry B Description Returns true if the given geometries are "topologically equal". Use this for a 'better' answer than '='. Topological equality means that the geometries have the same dimension, and their point-sets occupy the same space. This means that the order of vertices may be different in topologically equal geometries. To verify the order of points is consistent use (it must be noted ST_OrderingEquals is a little more stringent than simply verifying order of points are the same). In mathematical terms: ST_Equals(A, B) ⇔ A = B The following relation holds: ST_Equals(A, B) ⇔ ST_Within(A,B) ∧ ST_Within(B,A) Enhanced: 3.0.0 enabled support for GEOMETRYCOLLECTION &sfs_compliant; s2.1.1.2 &sqlmm_compliant; SQL-MM 3: 5.1.24 Changed: 2.2.0 Returns true even for invalid geometries if they are binary equal Examples SELECT ST_Equals('LINESTRING(0 0,10 10)', 'LINESTRING(0 0,5 5,10 10)'); t SELECT ST_Equals(ST_Reverse('LINESTRING(0 0,10 10)'), 'LINESTRING(0 0,5 5,10 10)'); t See Also , , , ST_Intersects Tests if two geometries intersect (they have at least one point in common) boolean ST_Intersects geometry geomA geometry geomB boolean ST_Intersects geography geogA geography geogB Description Returns true if two geometries intersect. Geometries intersect if they have any point in common. For geography, a distance tolerance of 0.00001 meters is used (so points that are very close are considered to intersect). In mathematical terms: ST_Intersects(A, B) ⇔ A ⋂ B ≠ ∅ Geometries intersect if their DE-9IM Intersection Matrix matches one of: T******** *T******* ***T***** ****T**** Spatial intersection is implied by all the other spatial relationship tests, except , which tests that geometries do NOT intersect. &index_aware; Changed: 3.0.0 SFCGAL version removed and native support for 2D TINS added. Enhanced: 2.5.0 Supports GEOMETRYCOLLECTION. Enhanced: 2.3.0 Enhancement to PIP short-circuit extended to support MultiPoints with few points. Prior versions only supported point in polygon. Performed by the GEOS module (for geometry), geography is native Availability: 1.5 support for geography was introduced. For geography, this function has a distance tolerance of about 0.00001 meters and uses the sphere rather than spheroid calculation. NOTE: this is the "allowable" version that returns a boolean, not an integer. &sfs_compliant; s2.1.1.2 //s2.1.13.3 - ST_Intersects(g1, g2 ) --> Not (ST_Disjoint(g1, g2 )) &sqlmm_compliant; SQL-MM 3: 5.1.27 &curve_support; &T_support; Geometry Examples SELECT ST_Intersects('POINT(0 0)'::geometry, 'LINESTRING ( 2 0,0 2 )'::geometry); f SELECT ST_Intersects('POINT(0 0)'::geometry, 'LINESTRING ( 0 0,0 2 )'::geometry); t This example looks up rows in a table; for faster lookup, the geometry column should have a GiST index. SELECT id, name FROM cities WHERE ST_Intersects(geom, 'SRID=4326;POLYGON((28 53,27.707 52.293,27 52,26.293 52.293,26 53,26.293 53.707,27 54,27.707 53.707,28 53))'); id | name ----+------- 2 | Minsk (1 row) Geography Examples SELECT ST_Intersects('SRID=4326;LINESTRING(-43.23456 72.4567,-43.23456 72.4568)'::geography, 'SRID=4326;POINT(-43.23456 72.4567772)'::geography ); t See Also , , ST_LineCrossingDirection Returns a number indicating the crossing behavior of two LineStrings integer ST_LineCrossingDirection geometry linestringA geometry linestringB Description Given two linestrings returns an integer between -3 and 3 indicating what kind of crossing behavior exists between them. 0 indicates no crossing. This is only supported for LINESTRINGs. The crossing number has the following meaning: 0: LINE NO CROSS -1: LINE CROSS LEFT 1: LINE CROSS RIGHT -2: LINE MULTICROSS END LEFT 2: LINE MULTICROSS END RIGHT -3: LINE MULTICROSS END SAME FIRST LEFT 3: LINE MULTICROSS END SAME FIRST RIGHT Availability: 1.4 Examples Example: LINE CROSS LEFT and LINE CROSS RIGHT SELECT ST_LineCrossingDirection(lineA, lineB) As A_cross_B, ST_LineCrossingDirection(lineB, lineA) As B_cross_A FROM (SELECT 'LINESTRING(25 169,89 114,40 70,86 43)'::geometry As lineA, 'LINESTRING (20 140,71 74,161 53)'::geometry As lineB ) As foo; A_cross_B | B_cross_A -----------+----------- -1 | 1 Example: LINE MULTICROSS END SAME FIRST LEFT and LINE MULTICROSS END SAME FIRST RIGHT SELECT ST_LineCrossingDirection(lineA, lineB) As A_cross_B, ST_LineCrossingDirection(lineB, lineA) As B_cross_A FROM (SELECT 'LINESTRING(25 169,89 114,40 70,86 43)'::geometry As lineA, 'LINESTRING(171 154,20 140,71 74,161 53)'::geometry As lineB ) As foo; A_cross_B | B_cross_A -----------+----------- 3 | -3 Example: LINE MULTICROSS END LEFT and LINE MULTICROSS END RIGHT SELECT ST_LineCrossingDirection(lineA, lineB) As A_cross_B, ST_LineCrossingDirection(lineB, lineA) As B_cross_A FROM (SELECT 'LINESTRING(25 169,89 114,40 70,86 43)'::geometry As lineA, 'LINESTRING(5 90,71 74,20 140,171 154)'::geometry As lineB ) As foo; A_cross_B | B_cross_A -----------+----------- -2 | 2 Example: Finds all streets that cross 0; ]]> See Also ST_OrderingEquals Tests if two geometries represent the same geometry and have points in the same directional order boolean ST_OrderingEquals geometry A geometry B Description ST_OrderingEquals compares two geometries and returns t (TRUE) if the geometries are equal and the coordinates are in the same order; otherwise it returns f (FALSE). This function is implemented as per the ArcSDE SQL specification rather than SQL-MM. http://edndoc.esri.com/arcsde/9.1/sql_api/sqlapi3.htm#ST_OrderingEquals &sqlmm_compliant; SQL-MM 3: 5.1.43 Examples SELECT ST_OrderingEquals('LINESTRING(0 0,10 10)', 'LINESTRING(0 0,5 5,10 10)'); f SELECT ST_OrderingEquals('LINESTRING(0 0,10 10)', 'LINESTRING(0 0,10 10)'); t SELECT ST_OrderingEquals('POLYGON((0 0,0 1,1 1,1 0,0 0))', 'POLYGON((0 0,1 0,1 1,0 1,0 0))'); f See Also , , ST_Overlaps Tests if two geometries have the same dimension and intersect, but each has at least one point not in the other boolean ST_Overlaps geometry A geometry B Description Returns TRUE if geometry A and B "spatially overlap". Two geometries overlap if they have the same dimension, their interiors intersect in that dimension. and each has at least one point inside the other (or equivalently, neither one covers the other). The overlaps relation is symmetric and irreflexive. In mathematical terms: ST_Overlaps(A, B) ⇔ ( dim(A) = dim(B) = dim( Int(A) ⋂ Int(B) )) ∧ (A ⋂ B ≠ A) ∧ (A ⋂ B ≠ B) &index_aware; To avoid index use, use the function _ST_Overlaps. Performed by the GEOS module Enhanced: 3.0.0 enabled support for GEOMETRYCOLLECTION NOTE: this is the "allowable" version that returns a boolean, not an integer. &sfs_compliant; s2.1.1.2 // s2.1.13.3 &sqlmm_compliant; SQL-MM 3: 5.1.32 Examples ST_Overlaps returns TRUE in the following situations: Two MultiPoints overlap when they share some, but not all, points. SELECT ST_Overlaps(a, b) FROM (SELECT 'MULTIPOINT ((20 20),(40 90),(80 130),(140 100),(110 40),(120 170),(150 70))'::geometry AS a, 'MULTIPOINT ((180 10),(40 90),(80 130),(160 150),(110 40),(50 50),(50 160))'::geometry AS b ) AS example; t Two LineStrings overlap along part of their paths. SELECT ST_Overlaps(a, b) FROM (SELECT 'LINESTRING (10 10,40 90,70 110,140 110,170 130,190 190)'::geometry AS a, 'LINESTRING (10 190,50 130,90 110,130 110,160 70,180 10)'::geometry AS b ) AS example; t Two Polygons overlap where their interiors intersect and neither contains the other. SELECT ST_Overlaps(a, b) FROM (SELECT 'POLYGON ((140 140,140 122,135 105,126 100,118 99,110 94,100 86,97 73,102 59,98 49,87 38,70 30,55 29,40 30,28 38,20 50,14 66,10 84,6 100,4 119,4 143,6 166,10 180,18 191,28 195,40 190,55 189,67 186,86 179,112 165,124 158,133 148,140 140),(48 177,40 177,30 174,24 166,22 159,25 155,30 153,40 154,45 157,52 162,53 169,51 174,48 177))'::geometry AS a, 'POLYGON ((75 63,79 50,87 38,95 31,108 25,124 22,140 18,154 11,166 6,176 10,184 21,188 35,190 58,190 82,193 104,190 121,185 139,178 154,166 163,154 171,139 172,124 171,112 165,96 152,92 142,92 126,86 116,79 110,75 104,72 94,73 86,75 76,75 63))'::geometry AS b ) AS example; t A Point on a LineString is contained, but since it has lower dimension it does not overlap or cross. SELECT ST_Overlaps(a, b) AS overlaps, ST_Crosses(a, b) AS crosses, ST_Intersects(a, b) AS intersects, ST_Contains(b, a) AS b_contains_a FROM (SELECT 'POINT (100 100)'::geometry As a, 'LINESTRING (30 50,40 160,160 40,180 160)'::geometry AS b) AS t overlaps | crosses | intersects | b_contains_a ---------+----------------------+-------------- f | f | t | t A LineString that partly covers a Polygon intersects and crosses, but does not overlap since it has different dimension. SELECT ST_Overlaps(a, b) AS overlaps, ST_Crosses(a, b) AS crosses, ST_Intersects(a, b) AS intersects, ST_Contains(a, b) AS contains FROM (SELECT 'POLYGON ((40 170,90 30,180 100,40 170))'::geometry AS a, 'LINESTRING(10 10,190 190)'::geometry AS b) AS t; overlap | crosses | intersects | contains ---------+---------+------------+-------------- f | t | t | f Two Polygons that intersect but with neither contained by the other overlap, but do not cross because their intersection has the same dimension. SELECT ST_Overlaps(a, b) AS overlaps, ST_Crosses(a, b) AS crosses, ST_Intersects(a, b) AS intersects, ST_Contains(b, a) AS b_contains_a, ST_Dimension(a) AS dim_a, ST_Dimension(b) AS dim_b, ST_Dimension(ST_Intersection(a, b)) AS dim_int FROM (SELECT 'POLYGON ((40 170,90 30,180 100,40 170))'::geometry AS a, 'POLYGON ((110 180,20 60,130 90,110 180))'::geometry AS b) As t; overlaps | crosses | intersects | b_contains_a | dim_a | dim_b | dim_int ----------+---------+------------+--------------+-------+-------+----------- t | f | t | f | 2 | 2 | 2 See Also , , , ST_Relate Tests if two geometries have a topological relationship matching an Intersection Matrix pattern, or computes their Intersection Matrix boolean ST_Relate geometry geomA geometry geomB text intersectionMatrixPattern text ST_Relate geometry geomA geometry geomB text ST_Relate geometry geomA geometry geomB integer boundaryNodeRule Description These functions allow testing and evaluating the spatial (topological) relationship between two geometries, as defined by the Dimensionally Extended 9-Intersection Model (DE-9IM). The DE-9IM is specified as a 9-element matrix indicating the dimension of the intersections between the Interior, Boundary and Exterior of two geometries. It is represented by a 9-character text string using the symbols 'F', '0', '1', '2' (e.g. 'FF1FF0102'). A specific kind of spatial relationship can be tested by matching the intersection matrix to an intersection matrix pattern. Patterns can include the additional symbols 'T' (meaning "intersection is non-empty") and '*' (meaning "any value"). Common spatial relationships are provided by the named functions , , , , , , , , , , and . Using an explicit pattern allows testing multiple conditions of intersects, crosses, etc in one step. It also allows testing spatial relationships which do not have a named spatial relationship function. For example, the relationship "Interior-Intersects" has the DE-9IM pattern T********, which is not evaluated by any named predicate. For more information refer to . Variant 1: Tests if two geometries are spatially related according to the given intersectionMatrixPattern. Unlike most of the named spatial relationship predicates, this does NOT automatically include an index call. The reason is that some relationships are true for geometries which do NOT intersect (e.g. Disjoint). If you are using a relationship pattern that requires intersection, then include the && index call. It is better to use a named relationship function if available, since they automatically use a spatial index where one exists. Also, they may implement performance optimizations which are not available with full relate evaluation. Variant 2: Returns the DE-9IM matrix string for the spatial relationship between the two input geometries. The matrix string can be tested for matching a DE-9IM pattern using . Variant 3: Like variant 2, but allows specifying a Boundary Node Rule. A boundary node rule allows finer control over whether the endpoints of MultiLineStrings are considered to lie in the DE-9IM Interior or Boundary. The boundaryNodeRule values are: 1: OGC-Mod2 - line endpoints are in the Boundary if they occur an odd number of times. This is the rule defined by the OGC SFS standard, and is the default for ST_Relate. 2: Endpoint - all endpoints are in the Boundary. 3: MultivalentEndpoint - endpoints are in the Boundary if they occur more than once. In other words, the boundary is all the "attached" or "inner" endpoints (but not the "unattached/outer" ones). 4: MonovalentEndpoint - endpoints are in the Boundary if they occur only once. In other words, the boundary is all the "unattached" or "outer" endpoints. This function is not in the OGC spec, but is implied. see s2.1.13.2 &sfs_compliant; s2.1.1.2 // s2.1.13.3 &sqlmm_compliant; SQL-MM 3: 5.1.25 Performed by the GEOS module Enhanced: 2.0.0 - added support for specifying boundary node rule. Enhanced: 3.0.0 enabled support for GEOMETRYCOLLECTION Examples Using the boolean-valued function to test spatial relationships. SELECT ST_Relate('POINT(1 2)', ST_Buffer('POINT(1 2)', 2), '0FFFFF212'); t SELECT ST_Relate('POINT(1 2)', ST_Buffer('POINT(1 2)', 2), '*FF*FF212'); t Testing a custom spatial relationship pattern as a query condition, with && to enable using a spatial index. Find compounds that properly intersect (not just touch) a poly (Interior Intersects) SELECT c.* , p.name As poly_name FROM polys AS p INNER JOIN compounds As c ON c.geom && p.geom AND ST_Relate(p.geom, c.geom, 'T********'); Computing the intersection matrix for spatial relationships. SELECT ST_Relate('POINT(1 2)', ST_Buffer('POINT(1 2)', 2)); 0FFFFF212 SELECT ST_Relate('LINESTRING(1 2,3 4)', 'LINESTRING(5 6,7 8)' ); FF1FF0102 Using different Boundary Node Rules to compute the spatial relationship between a LineString and a MultiLineString with a duplicate endpoint (3 3): Using the OGC-Mod2 rule (1) the duplicate endpoint is in the interior of the MultiLineString, so the DE-9IM matrix entry [aB:bI] is 0 and [aB:bB] is F. Using the Endpoint rule (2) the duplicate endpoint is in the boundary of the MultiLineString, so the DE-9IM matrix entry [aB:bI] is F and [aB:bB] is 0. WITH data AS (SELECT 'LINESTRING(1 1,3 3)'::geometry AS a_line, 'MULTILINESTRING((3 3,3 5),(3 3,5 3))'::geometry AS b_multiline ) SELECT ST_Relate(a_line, b_multiline, 1) AS bnr_mod2, ST_Relate(a_line, b_multiline, 2) AS bnr_endpoint FROM data; bnr_mod2 | bnr_endpoint -----------+-------------- FF10F0102 | FF1F00102 See Also , , , , , , , , , , , , ST_RelateMatch Tests if a DE-9IM Intersection Matrix matches an Intersection Matrix pattern boolean ST_RelateMatch text intersectionMatrix text intersectionMatrixPattern Description Tests if a Dimensionally Extended 9-Intersection Model (DE-9IM) intersectionMatrix value satisfies an intersectionMatrixPattern. Intersection matrix values can be computed by . For more information refer to . Performed by the GEOS module Availability: 2.0.0 Examples SELECT ST_RelateMatch('101202FFF', 'TTTTTTFFF') ; t Patterns for common spatial relationships matched against intersection matrix values, for a line in various positions relative to a polygon SELECT pat.name AS relationship, pat.val AS pattern, mat.name AS position, mat.val AS matrix, ST_RelateMatch(mat.val, pat.val) AS match FROM (VALUES ( 'Equality', 'T1FF1FFF1' ), ( 'Overlaps', 'T*T***T**' ), ( 'Within', 'T*F**F***' ), ( 'Disjoint', 'FF*FF****' )) AS pat(name, val) CROSS JOIN (VALUES ('non-intersecting', 'FF1FF0212'), ('overlapping', '1010F0212'), ('inside', '1FF0FF212')) AS mat(name, val); relationship | pattern | position | matrix | match --------------+-----------+------------------+-----------+------- Equality | T1FF1FFF1 | non-intersecting | FF1FF0212 | f Equality | T1FF1FFF1 | overlapping | 1010F0212 | f Equality | T1FF1FFF1 | inside | 1FF0FF212 | f Overlaps | T*T***T** | non-intersecting | FF1FF0212 | f Overlaps | T*T***T** | overlapping | 1010F0212 | t Overlaps | T*T***T** | inside | 1FF0FF212 | f Within | T*F**F*** | non-intersecting | FF1FF0212 | f Within | T*F**F*** | overlapping | 1010F0212 | f Within | T*F**F*** | inside | 1FF0FF212 | t Disjoint | FF*FF**** | non-intersecting | FF1FF0212 | t Disjoint | FF*FF**** | overlapping | 1010F0212 | f Disjoint | FF*FF**** | inside | 1FF0FF212 | f See Also , ST_Touches Tests if two geometries have at least one point in common, but their interiors do not intersect boolean ST_Touches geometry A geometry B Description Returns TRUE if A and B intersect, but their interiors do not intersect. Equivalently, A and B have at least one point in common, and the common points lie in at least one boundary. For Point/Point inputs the relationship is always FALSE, since points do not have a boundary. In mathematical terms: ST_Touches(A, B) ⇔ (Int(A) ⋂ Int(B) = ∅) ∧ (A ⋂ B ≠ ∅) This relationship holds if the DE-9IM Intersection Matrix for the two geometries matches one of: FT******* F**T***** F***T**** &index_aware; To avoid using an index, use _ST_Touches instead. Enhanced: 3.0.0 enabled support for GEOMETRYCOLLECTION &sfs_compliant; s2.1.1.2 // s2.1.13.3 &sqlmm_compliant; SQL-MM 3: 5.1.28 Examples The ST_Touches predicate returns TRUE in the following examples. Two Polygons touching along a boundary segment. SELECT ST_Touches(a, b) FROM (SELECT 'POLYGON ((10 190,10 70,80 70,80 130,50 160,120 160,120 190,10 190))'::geometry AS a, 'POLYGON ((80 110,140 140,190 110,180 40,140 10,80 50,80 70,80 110))'::geometry AS b ) AS example; t Two Polygons touching at a boundary point. SELECT ST_Touches(a, b) FROM (SELECT 'POLYGON ((10 190,10 70,80 70,80 130,50 160,120 160,120 190,10 190))'::geometry AS a, 'POLYGON ((140 140,190 110,180 40,140 10,80 70,140 140))'::geometry AS b ) AS example; t A Polygon and a LineString touching at the line endpoint. SELECT ST_Touches(a, b) FROM (SELECT 'POLYGON ((80 10,50 40,50 90,80 130,140 120,160 70,140 30,80 10))'::geometry AS a, 'LINESTRING (30 190,180 150,160 70)'::geometry AS b ) AS example; t Two LineStrings touching at their endpoints. SELECT ST_Touches(a, b) FROM (SELECT 'LINESTRING (10 10,60 140,140 190)'::geometry AS a, 'LINESTRING (140 190,190 10)'::geometry AS b ) AS example; t Two LineStrings touching where an endpoint meets the other line. SELECT ST_Touches(a, b) FROM (SELECT 'LINESTRING (10 10,60 140,140 190)'::geometry AS a, 'LINESTRING (60 140,190 10)'::geometry AS b ) AS example; t A Point touching a Polygon boundary. SELECT ST_Touches(a, b) FROM (SELECT 'POLYGON ((30 30,10 110,40 190,120 170,180 180,170 100,130 10,30 30))'::geometry AS a, 'POINT (170 100)'::geometry AS b ) AS example; t SELECT ST_Touches('LINESTRING(0 0,1 1,0 2)'::geometry, 'POINT(1 1)'::geometry); f SELECT ST_Touches('LINESTRING(0 0,1 1,0 2)'::geometry, 'POINT(0 2)'::geometry); t ST_Within Tests if every point of A lies in B, and their interiors have a point in common boolean ST_Within geometry A geometry B Description Returns TRUE if geometry A is within geometry B. A is within B if and only if all points of A lie inside (i.e. in the interior or boundary of) B (or equivalently, no points of A lie in the exterior of B), and the interiors of A and B have at least one point in common. For this function to make sense, the source geometries must both be of the same coordinate projection, having the same SRID. In mathematical terms: ST_Within(A, B) ⇔ (A ⋂ B = A) ∧ (Int(A) ⋂ Int(B) ≠ ∅) The within relation is reflexive: every geometry is within itself. The relation is antisymmetric: if ST_Within(A,B) = true and ST_Within(B,A) = true, then the two geometries must be topologically equal (ST_Equals(A,B) = true). ST_Within is the converse of . So, ST_Within(A,B) = ST_Contains(B,A). Because the interiors must have a common point, a subtlety of the definition is that lines and points lying fully in the boundary of polygons or lines are not within the geometry. For further details see Subtleties of OGC Covers, Contains, Within. The predicate provides a more inclusive relationship. &index_aware; To avoid index use, use the function _ST_Within. Performed by the GEOS module Enhanced: 2.3.0 Enhancement to PIP short-circuit for geometry extended to support MultiPoints with few points. Prior versions only supported point in polygon. Enhanced: 3.0.0 enabled support for GEOMETRYCOLLECTION Do not use this function with invalid geometries. You will get unexpected results. NOTE: this is the "allowable" version that returns a boolean, not an integer. &sfs_compliant; s2.1.1.2 // s2.1.13.3 - a.Relate(b, 'T*F**F***') &sqlmm_compliant; SQL-MM 3: 5.1.30 Examples This example defines two concentric circles once in a CTE so the same inputs can be rendered and then reused in related predicate checks. WITH circles AS ( SELECT ST_Buffer(center, 20) AS smallc, ST_Buffer(center, 40) AS bigc FROM (SELECT 'POINT(50 50)'::geometry AS center) AS p ) SELECT smallc AS small_circle, bigc AS big_circle FROM circles; POLYGON((70 50,69.616 46.098,68.478 42.346,66.629 38.889,64.142 35.858,61.111 33.371,57.654 31.522,53.902 30.384,50 30,46.098 30.384,42.346 31.522,38.889 33.371,35.858 35.858,33.371 38.889,31.522 42.346,30.384 46.098,30 50,30.384 53.902,31.522 57.654,33.371 61.111,35.858 64.142,38.889 66.629,42.346 68.478,46.098 69.616,50 70,53.902 69.616,57.654 68.478,61.111 66.629,64.142 64.142,66.629 61.111,68.478 57.654,69.616 53.902,70 50)) | POLYGON((90 50,89.231 42.196,86.955 34.693,83.259 27.777,78.284 21.716,72.223 16.741,65.307 13.045,57.804 10.769,50 10,42.196 10.769,34.693 13.045,27.777 16.741,21.716 21.716,16.741 27.777,13.045 34.693,10.769 42.196,10 50,10.769 57.804,13.045 65.307,16.741 72.223,21.716 78.284,27.777 83.259,34.693 86.955,42.196 89.231,50 90,57.804 89.231,65.307 86.955,72.223 83.259,78.284 78.284,83.259 72.223,86.955 65.307,89.231 57.804,90 50)) Using the same circles, ST_Within is reflexive on each circle, false from the larger circle into the smaller one, and true for ST_Union only when the union does not extend beyond the larger circle. WITH circles AS ( SELECT ST_Buffer(center, 20) AS smallc, ST_Buffer(center, 40) AS bigc FROM (SELECT 'POINT(50 50)'::geometry AS center) AS p ) SELECT ST_Within(smallc, smallc) AS smallinsmall, ST_Within(smallc, bigc) AS smallinbig, ST_Within(bigc, smallc) AS biginsmall, ST_Within(ST_Union(smallc, bigc), bigc) AS unioninbig, ST_Within(bigc, ST_Union(smallc, bigc)) AS biginunion, ST_Equals(bigc, ST_Union(smallc, bigc)) AS bigisunion FROM circles; t | t | f | t | t | t See Also , , ,
Distance Relationships ST_3DDWithin Tests if two 3D geometries are within a given 3D distance boolean ST_3DDWithin geometry g1 geometry g2 double precision distance_of_srid Description Returns true if the 3D distance between two geometry values is no larger than distance distance_of_srid. The distance is specified in units defined by the spatial reference system of the geometries. For this function to make sense the source geometries must be in the same coordinate system (have the same SRID). &index_aware; &Z_support; &P_support; &sqlmm_compliant; SQL-MM ? Availability: 2.0.0 Examples Compare the same transformed point and line in both 3D and 2D. The 3D test fails because the Z difference matters even though the 2D footprints are close enough. WITH sample AS ( SELECT ST_Transform('SRID=4326;POINT(-72.1235 42.3521 4)'::geometry, 2163) AS input_point, ST_Transform( 'SRID=4326;LINESTRING(-72.1260 42.45 15,-72.123 42.1546 20)'::geometry, 2163 ) AS input_line ) SELECT ST_3DDWithin(input_point, input_line, 126.8) AS within_dist_3d, ST_DWithin(input_point, input_line, 126.8) AS within_dist_2d, input_point AS input_point, input_line AS input_line FROM sample; -[ RECORD 1 ]--+---------------------------------------------------------------------------------------------------------------------------------- within_dist_3d | f within_dist_2d | t input_point | POINT Z (-11774128.9 5237160.2 4) input_line | LINESTRING Z (-11774339.5 5243556.8 15,-11773706.7 5215256.3 20) See Also , , , , , , ST_3DDFullyWithin Tests if two 3D geometries are entirely within a given 3D distance boolean ST_3DDFullyWithin geometry g1 geometry g2 double precision distance Description Returns true if the 3D geometries are fully within the specified distance of one another. The distance is specified in units defined by the spatial reference system of the geometries. For this function to make sense, the source geometries must both be of the same coordinate projection, having the same SRID. &index_aware; Availability: 2.0.0 &Z_support; &P_support; Examples This example compares distance-within predicates in 2D and 3D. The point is distance-within the line in 3D, but the full 3D line is not within the same distance of the point. d3dfullywithin10 | d3dwithin10 | d2dfullywithin20 | d3dfullywithin20 ------------------+-------------+------------------+------------------ f | t | t | f See Also , , , ST_DFullyWithin Tests if a geometry is entirely inside a distance of another boolean ST_DFullyWithin geometry g1 geometry g2 double precision distance Description Returns true if g2 is entirely within distance of g1. Visually, the condition is true if g2 is contained within a distance buffer of g1. The distance is specified in units defined by the spatial reference system of the geometries. &index_aware; Availability: 1.5.0 Changed: 3.5.0 : the logic behind the function now uses a test of containment within a buffer, rather than the ST_MaxDistance algorithm. Results will differ from prior versions, but should be closer to user expectations. Examples SELECT ST_DFullyWithin(geom_a, geom_b, 10) AS DFullyWithin10, ST_DWithin(geom_a, geom_b, 10) AS DWithin10, ST_DFullyWithin(geom_a, geom_b, 20) AS DFullyWithin20 FROM (VALUES ('POINT(1 1)'::geometry, 'LINESTRING(1 5,2 7,1 9,14 12)'::geometry) ) AS v(geom_a, geom_b) dfullywithin10 | dwithin10 | dfullywithin20 ----------------+-----------+---------------- f | t | t See Also , , , ST_DWithin Tests if two geometries are within a given distance boolean ST_DWithin geometry g1 geometry g2 double precision distance_of_srid boolean ST_DWithin geography gg1 geography gg2 double precision distance_meters boolean use_spheroid = true Description Returns true if the geometries are within a given distance For geometry: The distance is specified in units defined by the spatial reference system of the geometries. For this function to make sense, the source geometries must be in the same coordinate system (have the same SRID). For geography: units are in meters and distance measurement defaults to use_spheroid = true. For faster evaluation use use_spheroid = false to measure on the sphere. Use for 3D geometries. This function call includes a bounding box comparison that makes use of any indexes that are available on the geometries. &sfs_compliant; Availability: 1.5.0 support for geography was introduced Enhanced: 2.1.0 improved speed for geography. See Making Geography faster for details. Enhanced: 2.1.0 support for curved geometries was introduced. Prior to 1.3, was commonly used in conjunction with && and ST_Distance to test for distance, and in pre-1.3.4 this function used that logic. From 1.3.4, ST_DWithin uses a faster short-circuit distance function. Examples This self-contained geometry example keeps the compared shapes visible while also returning the boolean results. WITH sample AS ( SELECT ST_Buffer('POINT(100 90)'::geometry, 50, 'quad_segs=2') AS coverage, ST_Point(120, 120) AS inside_pt, ST_Point(170, 90) AS outside_pt ) SELECT ST_DWithin(coverage, inside_pt, 0) AS inside_now, ST_DWithin(coverage, outside_pt, 10) AS outside_within_10, coverage AS coverage_geom, inside_pt AS inside_geom, outside_pt AS outside_geom FROM sample; -[ RECORD 1 ]----- inside_now | t outside_within_10 | f coverage_geom | POLYGON((150 90,135 55,100 40,65 55,50 90,65 125,100 140,135 125,150 90)) inside_geom | POINT(120 120) outside_geom | POINT(170 90) Find the nearest hospital to each school. That is within 3000 units of the school. We do an ST_DWithin search to utilize indexes to limit our search list. That the non-indexable ST_Distance needs to process. If the units of the spatial reference is meters then units would be meters. SELECT DISTINCT ON (s.gid) s.gid, s.school_name, s.geom, h.hospital_name FROM schools s LEFT JOIN hospitals h ON ST_DWithin(s.geom, h.geom, 3000) ORDER BY s.gid, ST_Distance(s.geom, h.geom); The schools with no close hospitals. Find all schools with no hospital within 3000 units away from the school. Units are in units of spatial reference (for example meters, feet, or degrees). SELECT s.gid, s.school_name FROM schools s LEFT JOIN hospitals h ON ST_DWithin(s.geom, h.geom, 3000) WHERE h.gid IS NULL; Find locations more than 3000 units away from their nearest hospital. Use NOT EXISTS so that a location is returned only when no nearby hospital exists. SELECT l.location_name FROM locations l WHERE NOT EXISTS ( SELECT 1 FROM hospitals h WHERE ST_DWithin(l.geom, h.geom, 3000) ); Find broadcasting towers that a receiver with limited range can receive. Data is geometry in Spherical Mercator (SRID=3857), ranges are approximate. CREATE INDEX ON broadcasting_towers USING gist (geom); CREATE INDEX ON broadcasting_towers USING gist (ST_Expand(geom, sending_range)); Query towers that a 4-kilometer receiver in Minsk Hackerspace can get. Note: two conditions are required because the shorter LEAST(b.sending_range, 4000) will not use an index. SELECT b.tower_id, b.geom FROM broadcasting_towers b WHERE ST_DWithin(b.geom, 'SRID=3857;POINT(3072163.4 7159374.1)', 4000) AND ST_DWithin(b.geom, 'SRID=3857;POINT(3072163.4 7159374.1)', b.sending_range); See Also , ST_PointInsideCircle Tests if a point geometry is inside a circle defined by a center and radius boolean ST_PointInsideCircle geometry a_point float center_x float center_y float radius Description Returns true if the geometry is a point and is inside the circle with center center_x,center_y and radius radius. Does not use spatial indexes. Use instead. Availability: 1.2 Changed: 2.2.0 In prior versions this was called ST_Point_Inside_Circle Examples SELECT ST_PointInsideCircle(ST_Point(1, 2), 0.5, 2, 3); t See Also