Here are a few new tricks, using:
EXCEPT
to remove geometries from either table that are the same, so we can focus only on geometries that are unique to each table (A_only
and B_only
).
ST_Snap
to get exact noding for overlay operators.
- Use the
ST_SymDifference
overlay operator to find the symmetric difference between the two geometry sets to show the differences. Update: ST_Difference
shows the same result for this example. You can try either function to see what they get.
This should get what you expect:
-- CREATE OR REPLACE VIEW polygon_SymDifference AS
SELECT row_number() OVER () rn, *
FROM (
SELECT (ST_Dump(ST_SymDifference(ST_Snap(A, B, tol), ST_Snap(B, A, tol)))).*
FROM (
SELECT ST_Union(DISTINCT A_only.geom) A, ST_Union(DISTINCT B_only.geom) B, 1e-5 tol
FROM (
SELECT ST_Boundary(geom) geom FROM polygon_a
EXCEPT SELECT ST_Boundary(geom) geom FROM polygon_b
) A_only,
(
SELECT ST_Boundary(geom) geom FROM polygon_b
EXCEPT SELECT ST_Boundary(geom) geom FROM polygon_a
) B_only
) s
) s;
rn | geom
----+-------------------------------------------------------------------------------------
1 | LINESTRING(206.234028204842 -92.0360704110685,219.846021625456 -92.5340701703592)
2 | LINESTRING(18.556700448873 -36.4496098325257,44.44438533894 -40.5104231486146)
3 | LINESTRING(-131.974995802602 -38.6145334122719,-114.067738329597 -39.0215165366584)
(3 rows)
To unpack this answer a bit more, the first step with ST_Boundary
gets the boundary of each polygon, rather than just the exterior. For instance, if there were holes, these would be traced by the boundary.
The EXCEPT
clause is used to remove geometries from A that are part of B, and rows from B that are part of A. This reduces the number of rows that are part of A only, and part of B only. E.g., to get A_only:
SELECT ST_Boundary(geom) geom FROM polygon_a
EXCEPT SELECT ST_Boundary(geom) geom FROM polygon_b
Here are the 6 rows of A_only, and 3 rows of B_only:
Next, ST_Union(DISTINCT A_only.geom)
is used to combine the linework into a single geometry, typically a MultiLineString.
ST_Snap is used to snap nodes from one geometry to another. For instance ST_Snap(A, B, tol)
will take the A geometry, and add more nodes from the B geometry, or move them to the B geometry, if they are within tol
distance. There are probably several ways to use these functions, but the idea is to get coordinates from each geometry that are exact to each other. So the two geometries after snapping look like this:
And to show differences, you can choose to use either ST_SymDifference
or ST_Difference
. They both show the same result for this example.
Best Answer
Use
ST_Union
to aggregate the polygons into one polygon and thenST_InteriorRingN
to get the border of the gap andST_BuildArea
to get polygon of the gap. If there is more than one such gap usegenerate_series
andST_NumInteriorRings
. Example:Blue-start polygons Purple-final polygons