geometry STBoundary: A Polygon Edge Is a Different Shape

geometry STBoundary returns the edge of a shape as another geometry instance. I inspect that result independently from the source. A polygon’s boundary is not another filled polygon with the same area.

A region has an interior and a boundary. Asking for the boundary keeps the points that form its edge. The resulting geometry can therefore have a lower dimension and different measurements from the original.

Two wooden lattice panels, one flat and one upright, beside a loose sage slat and small wooden blocks.
Two wooden lattice panels, one flat and one upright, beside a loose slat.

Compare a rectangle with a point

The rectangle extends four coordinate units horizontally and two vertically. Its boundary follows all four sides. The second input is a single point located within that rectangular extent.

Both inputs use reference identifier zero and valid text constructors. The query returns the input type beside the boundary type, empty flag, area and length. It uses an Empty display label only when the returned geometry is empty.

The example creates geometry values inside a read-only query. It does not store them or change a spatial index. The input literals appear in full in the query.

WITH Inputs AS
(
    SELECT CaseId, InputName, Wkt
    FROM (VALUES
        (1, CAST('Rectangle' AS varchar(20)), CAST('POLYGON((0 0, 4 0, 4 2, 0 2, 0 0))' AS varchar(200))),
        (2, 'Point', 'POINT(2 1)')
    ) AS v(CaseId, InputName, Wkt)
), Shapes AS
(
    SELECT CaseId, InputName, geometry::STGeomFromText(Wkt, 0) AS InputShape FROM Inputs
), Boundaries AS
(
    SELECT CaseId, InputName, InputShape.STGeometryType() AS InputType,
           InputShape.STBoundary() AS BoundaryShape
    FROM Shapes
)
SELECT CaseId, InputName, InputType,
       CASE WHEN BoundaryShape.STIsEmpty() = 1 THEN 'Empty' ELSE BoundaryShape.STGeometryType() END AS BoundaryKind,
       BoundaryShape.STIsEmpty() AS BoundaryIsEmpty,
       CAST(BoundaryShape.STArea() AS decimal(12,3)) AS BoundaryArea,
       CAST(BoundaryShape.STLength() AS decimal(12,3)) AS BoundaryLength
FROM Boundaries
ORDER BY CaseId;
Native SSMS results comparing the boundary of a rectangle and a point.
The rectangle has a LineString boundary with length 12.000 and area zero. The point has an empty boundary with both length and area zero. Open the result at full size.

Read the returned edge

The rectangle should return a nonempty LineString boundary. Its area is zero because the boundary is a line, not the filled region. Its expected length is twelve coordinate units.

That length follows four plus two plus four plus two around the rectangle. The repeated closing coordinate identifies the completed ring. It is not an additional side with another nonzero length.

STBoundary returns the rectangle's edge as a line. The region is not filled; its boundary length is twelve coordinate units.
STBoundary returns the rectangle’s edge as a line. The region is not filled; its boundary length is twelve coordinate units. Open the diagram at full size.

The point should have an empty boundary. Its boundary area and length are both zero, and BoundaryIsEmpty is one. The input point itself still exists; the query has not made the source geometry missing.

The two zero-area boundary results have different populations. One contains the rectangle’s edge, while the other contains no boundary points. The emptiness flag distinguishes them directly.

Do not confuse boundary with an enclosing outline

STBoundary asks for the source shape’s boundary. It does not calculate a bounding rectangle around an arbitrary object. Those operations can agree visually for one rectangle while representing different requirements.

A polygon with interior rings has additional boundaries around its holes. A more complex boundary may therefore have several components. The simple rectangle here is not a promise that every polygon returns one uninterrupted line.

The method also has type-specific behavior for other inputs. A line whose endpoints are the same has an empty boundary. That result follows the line’s boundary definition, rather than meaning the entire line is empty.

I don’t use boundary area as a substitute for original polygon area. Those are measurements of different geometries. Keep the input and returned boundary separate when a report needs both values.

Keep coordinate and application rules explicit

These are planar coordinates with an abstract agreed unit. The length is expressed in those units. No meter conversion or geographic reference is implied by the value twelve.

The query does not define a tolerance band around the boundary. A point near an edge is a different question from constructing the exact edge. Applications that need a buffer must state that requirement separately.

Only valid, bounded inputs are used here. A parsed spatial value still needs appropriate validity checks in a broader pipeline. This example demonstrates boundary construction rather than repairing an invalid source shape.

When adapting it, inspect the boundary type and emptiness alongside its measurements. A zero number by itself cannot reveal the resulting point set. The paired rectangle and point cases keep that distinction visible.

Draw the rectangle and its edge side by side and the difference is hard to forget.

A boundary is not a smaller copy of the shape, it is a different geometry.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Spatial Database, SQL Function, SQL Scripts, SQL Server
Previous Post
SQLAuthority News – Authors Visit – DelhiBuzz TechEd on July 11, 2009
Next Post
SQL SERVER – Maximizing View of SQL Server Management Studio – Full Screen – New Screen

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.