For example: starting with the NW point drawing clockwise (the anglican way) or drawing counter-clockwise (the way SQL draws Geography).
I have to convert to geography on the fly because the geometry in the DB is combined with other shapes, hence the 2-level CTE.
WITH PolygonGeo (Id, Polygon)
AS (
SELECT
[MyIDColumn] AS [id]
,geometry::STGeomFromText(GeometryText, 4326) as [Polygon]
FROM [MyDatabase].[dbo].[MyTable]
WHERE GeometryText LIKE 'POLYGON%'
),
Points ([Id], Pt0, Pt1)
AS (
SELECT Id
, [Polygon].STPointN(1) AS Pt0
, [Polygon].STPointN(2) AS Pt1
FROM PolygonGeo
)
SELECT [Id]
, ATN2(Pt1.STX - Pt0.STX, Pt1.STY - Pt0.STY) / PI() * 180 AS Angle
FROM Points
Results:
Id_8 | Angle |
---|---|
33BAA7F | 180 |
167AAEA | 176.395579460831 |
F6469B3 | 180 |
33BAA7F | 180 |
167AAEA | 177.137594773874 |
0D34759 | 154.490530508003 |
33BAA7F | 165.499833233449 |
CB398C0 | 180 |
CB398C0 | 143.383248480932 |
F6469B3 | 179.283840054506 |
33BAA7F | -175.114797431858 |
Add a comment