Reflecting a Geometry Object Around a Line

I had need, in SQL Server Spatial, for a function that reflected a geometry object around a supplied line.

So I wrote one, and this post describes how that function enables the reflecting of a geometry around a line.

SYNOPSIS

The function is called STReflect and its header is:

CREATE FUNCTION [$(Owner)].[STReflect] (
  @p_geometry        geometry,
  @p_reflection_line geometry,
  @p_round_xy        int = 8,
  @p_round_zm        int = 8
)
RETURNS geometry

DESCRIPTION

The function does as it describes. For any geometry object it reflects it about the supplied line of reflection.

The line of reflection can only be a LineString with 2-points.

Since the function computes new locations for all the points in the supplied geometry, its computed ordinate values can be restricted (rounded) to the supplied integer decimal digits of precision (eg 3 is 1mm or 0.001 for metric data).

EXAMPLES

The following three examples are offered to show how the function works.

EXAMPLE 1: Point

SELECT [dbo].[STReflect] ( 
           geometry::STGeomFromText('POINT(1 -2)',0),
           geometry::STGeomFromText('LINESTRING(3 2,2 6)',0),
           2,2
       ).AsTextZM() as rPoint; 

rPoint
POINT (6.65 -0.59)

EXAMPLE 2: Line

SELECT [dbo].[STReflect] ( 
            geometry::STGeomFromText('LINESTRING(1 -2, 1.5 0)',0),
            geometry::STGeomFromText('LINESTRING(3 2,2 6)',0),
            2,2
       ).AsTextZM() as rLine; 

rLine
LINESTRING (6.65 -0.59, 5.26 0.94)

EXAMPLE 3: Polygon

WITH data AS (
  SELECT geometry::STGeomFromText('POLYGON((1 4, 2 4, 1.5 3,1 4))',0) as polygon,
         geometry::STGeomFromText('LINESTRING(-5 0,5 0)',0) as rLine
)
SELECT [$(Owner)].[STReflect] ( 
            c.polygon,
            c.rLine,
            4,4
       ).AsTextZM() as rPolygon
 FROM data as c;
GO

rPolygon
POLYGON ((1 -4, 2 -4, 1.5 -3, 1 -4))

FINAL EXAMPLE: Multiple Reflections Of A Polygon around a rotating line of reflection


with data as (
  SELECT i.IntValue as angle,
         geometry::STGeomFromText('POLYGON((1 4, 2 4, 1.5 3,1 4))',0) as geom,
         [dbo].[STRotate](geometry::STGeomFromText('LINESTRING(3 2,2 6)',0),2.5,4,i.IntValue,4,4) as rLine
    FROM [dbo].[generate_series](0,155,25) as i
)
SELECT -1 as angle, geometry::STGeomFromText('POLYGON((1 4, 2 4, 1.5 3,1 4))',0) as geom
union all
select angle, a.rline from data as a
union all
SELECT a.angle,
       [dbo].[STReflect] ( a.geom,a.rLine,4,4 ) as rPoint
  FROM data as a;
  GO
Reflecting About Rotating Line
Reflect Geometry About a LineString

STRotate is documented in the SQL Server Spatial General Documentation.

The function is available with the 200+ TSQL functions available by donation from this website.

I hope this function is of use to someone.