1. I wanted the angle of the first side of a polygon in SQL Server 2008+.  Our code for entering the shapes evolved over time, specifically which direction drew the shapes.

    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_8Angle
    33BAA7F180
    167AAEA176.395579460831
    F6469B3180
    33BAA7F180
    167AAEA177.137594773874
    0D34759154.490530508003
    33BAA7F165.499833233449
    CB398C0180
    CB398C0143.383248480932
    F6469B3179.283840054506
    33BAA7F-175.114797431858




    0

    Add a comment

Blog Archive
Contributors
Contributors
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.