How the ST_Difference Geometry Tool works, and what it assumes

ST_Difference(A, B) returns what is left of A once every point it shares with B is removed. The order matters: A is the geometry you keep and B is the one you cut away. Swap A and B gives the other difference, and PostGIS's ST_SymDifference would give both at once.

When B lies inside A, the result is A with a hole where B was. When B covers all of A, the result is empty. When they do not overlap, A comes back unchanged. Removing a line or a point from a polygon removes no area at all; to cut a polygon along a line, buffer the line slightly with ST_Buffer and subtract the buffer.

As with the other overlays, invalid inputs are refused with the reason, collections of polygons are used as their union, and the area of the result is measured geodesically for longitude and latitude or in the input's own units for projected data.

Where this comes up in GIS work

  • Removing water bodies, roads or protected areas from a development site to find the usable land.
  • Cutting a hole for an exclusion zone out of a larger area.
  • Finding the part of a new boundary that falls outside the old one.
  • Trimming a buffer so it does not extend into a neighbouring region.

A worked example

The two offset squares again, subtracting the second from the first:

A: POLYGON ((0 0, 6 0, 6 6, 0 6, 0 0))
B: POLYGON ((3 3, 9 3, 9 9, 3 9, 3 3))

ST_Difference(A, B) = POLYGON ((6 3, 6 0, 0 0, 0 6, 3 6, 3 3, 6 3))

An L-shape of area 27: the 36 of A less the 9 it shares with B. Swapped, ST_Difference(B, A) is POLYGON ((3 6, 3 9, 9 9, 9 3, 6 3, 6 6, 3 6)), the mirror-image L, also 27.

Using the ST_Difference Geometry Tool

  1. Paste the geometry to keep into Geometry A, or select Load example.
  2. Paste the geometry to subtract into Geometry B.
  3. Choose the output format and decimal places.
  4. Select Subtract B from A, or press Ctrl + Enter (Cmd + Enter on a Mac).
  5. Check the remaining shape on the plot, then copy or download it; use Swap A and B for the other difference.

When the output looks wrong

The result is empty
Every point of A is also in B, so nothing is left. Check the order: ST_Difference(A, B) keeps A, and swapping the inputs usually gives the part you expected.
The result is unchanged from Geometry A
The geometries do not overlap, or share only a boundary, so there is nothing to remove. ST_Intersects tells you whether they meet at all.
Subtracting a line from a polygon changed nothing
A line has no area, so removing it from a polygon removes nothing of any area. To cut a polygon along a line, buffer the line by a small distance first and subtract the buffer.

Questions about the ST_Difference Geometry Tool

Does the order of the geometries matter?

Yes. ST_Difference(A, B) is the part of A not in B, and ST_Difference(B, A) the part of B not in A. For both at once, PostGIS has ST_SymDifference.

What happens when B is inside A?

A comes back with a hole where B was, as a polygon with an inner ring.

Is the area measured on the earth?

Yes, for longitude and latitude: the remaining area is measured geodesically, not in square degrees. Projected coordinates are measured in their own units.

Is my data uploaded?

No. The difference is computed in your browser.