Search code examples
sql-serversql-server-2012geospatialsqlgeometry

How define triangle with SqlGeometry in SQL Server 2012


In SQL Server 2012 a polygon is consists of an exterior ring with zero or some interior rings. So every simple polygon (rectangle, square and ...) must have an exterior ring and

From MSDN:

The following criteria are needed for a ring to be acceptable.

  • The LineString instance must be accepted.
  • The LineString instance must have at least four points.
  • The starting and ending points of the LineString instance must be the same.

So what if I want to declare a triangle with three points in SQL Server 2012?


Solution

  • From Microsoft: http://msdn.microsoft.com/en-us/library/bb895267.aspx

    DECLARE @g geometry;
    SET @g = geometry::STPolyFromText('POLYGON((100 100, 200 200, 300 100, 100 100))', 0);
    SELECT @g.ToString();
    

    It has 3 points and then returns to its starting point (100,100). You can see the area returns 10000, which is consistent with a triangle of base 200 and height 100:

    SELECT @g.STArea();