ST_Different

更新时间:
复制 MD 格式

Returns the part of geometry A that does not intersect geometry B.

Syntax

geometry ST_Difference(geometry geomA, geometry geomB)

Parameters

ParameterDescription
geomAThe source geometry. The result is always a portion of this geometry.
geomBThe geometry to subtract from geomA.

Usage notes

  • Input order matters. ST_Difference(A, B) always returns a portion of A.

  • If geomA is completely contained within geomB, the function returns an empty geometry.

  • GeometryCollection inputs are not supported.

  • For 3D geometries, the function computes the difference using X and Y coordinates only. Z coordinate values in the result are preserved from the input geometries (z coordinates are not deleted).

Examples

The following example returns the segment of the first line that does not overlap with the second line:

SELECT ST_AsText(
  ST_Difference(
    'LINESTRING(0 0,0 2)'::geometry,
    'LINESTRING(0 1,0 3)'::geometry
  )
);

Result:

      st_astext
---------------------
 LINESTRING(0 0,0 1)
(1 row)