STConvexHull: Enclose a Shape Without Its Indentations

I use STConvexHull when I need a convex enclosure around a geometry. It fills a shape’s indentations. That enclosure is useful for a defined comparison, but it doesn’t preserve every membership rule of the original shape.

Gouache painting: seen from above, a square vegetable bed has a triangular notch of bare grass cut into one side
An open red garden gateway above a water channel: a hull fills in a gap like that.

Start with a visible indentation

The first input is a square-shaped polygon with a triangular notch at the top. Its vertices run through the inward point at two, two. The expected original area is twelve, while its hull has area sixteen.

I keep the source geometry and the derived hull in separate columns inside the CTE. Nothing is updated or replaced. That separation matters because the larger enclosing region answers a different question. A hull isn’t simply a neater serialization of the original polygon.

WITH Inputs AS
(
 SELECT CaseId, Shape
 FROM (VALUES
  (1,geometry::STGeomFromText('POLYGON((0 0,4 0,4 4,2 2,0 4,0 0))',0)),
  (2,geometry::STGeomFromText('LINESTRING(0 0,2 0,4 0)',0)),
  (3,geometry::Point(0,0,0))
 ) v(CaseId,Shape)
), Hulls AS
(
 SELECT CaseId, Shape, Shape.STConvexHull() AS Hull FROM Inputs
)
SELECT CaseId, CAST(Hull.STGeometryType() AS nvarchar(40)) AS HullType,
       CAST(Shape.STArea() AS decimal(12,2)) AS OriginalArea,
       CAST(Hull.STArea() AS decimal(12,2)) AS HullArea,
       Shape.STContains(geometry::Point(2,3,0)) AS OriginalContainsWitness,
       Hull.STContains(geometry::Point(2,3,0)) AS HullContainsWitness
FROM Hulls
ORDER BY CaseId;
Native SSMS result grids for convex hull, complete results, including all returned rows and columns.
The polygon hull expands the area from 12 to 16 and includes the witness point. A line and a point keep zero area and do not contain that witness. Open the result at full size.

Test a witness in the filled region

The witness point lies at two, three. It lies inside the notch, so it is outside the source polygon but inside the convex enclosure. The expected membership columns therefore change from false to true for case one.

I’d show that witness beside the area comparison. Area alone can reveal growth without showing how a query’s answer changes. A location admitted by the hull may still be outside the true service region. Retain the original geometry for decisions needing exact membership.

Case one, source polygon: the inward corner at (2, 2) leaves a triangular notch in the covered region.
Case one, source polygon: the inward corner at (2, 2) leaves a triangular notch in the covered region. Open the diagram at full size.
Case one, convex hull: the enclosure fills that notch. Its area is sixteen rather than the source's twelve square coordinate units. Both plots use the same axes.
Case one, convex hull: the enclosure fills that notch. Its area is sixteen rather than the source’s twelve square coordinate units. Both plots use the same axes. Open the diagram at full size.

Handle lower-dimensional inputs

The second input is a collinear LineString. The third is a Point. The hull of these inputs keeps their type: a line stays a line and a point stays a point. They don’t become artificial polygons merely because the method is commonly explained using polygon examples.

Both expected areas are zero for these cases. Neither contains the chosen witness. I’d inspect the returned type before sending a hull to a consumer that assumes a polygon. A successful method call doesn’t establish that consumer’s required shape type.

Choose properties that express the contract

The query reports geometry type, two areas and two membership results. It doesn’t depend on a specific starting vertex in a serialized hull. Those properties directly demonstrate the enclosure and its changed region.

I’d compare complete output tuples rather than accepting an attractive spatial preview alone. A plotted outline can hide small coordinate or membership mistakes. Explicit witnesses make the intended decision reviewable. The display remains useful, but it doesn’t replace the numeric and Boolean evidence.

Keep the coordinate model clear

All inputs use geometry with SRID zero. The areas are planar coordinate-square units, not automatically square metres or geographic surface areas. The coordinate values were chosen for transparent arithmetic in a small example.

I’d identify a real dataset’s coordinate system before interpreting an area as a physical measurement. Assigning a familiar unit label doesn’t transform coordinates. This lesson describes the enclosure operation. It doesn’t establish a projection or a conversion between geographic and planar data.

Use the hull for a specific purpose

The method returns another geometry value, so it can feed a subsequent read-only spatial expression. A hull may help summarize extent or define a broader candidate region. Its filled spaces still require conscious treatment.

I’d keep exact membership checks separate when the application’s decision depends on the original outline. This example makes no performance claim or index recommendation. A smaller expression isn’t automatically a faster complete query. Validate the geometric question first, then inspect the production workload separately.

Keep the original shape close, because the hull can admit points it excludes.

A convex hull is not the original shape, it is the smallest convex region around it.

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.

SQL Datatype, SQL Function, SQL Scripts
Previous Post
Describe a Result Set Without Executing Its Query
Next Post
SQL SERVER – Three T-SQL Script to Create Primary Keys on Table

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.