Home Forums SQL Server 2008 T-SQL (SS2K8) Question about rounding the points in a Geometry. RE: Question about rounding the points in a Geometry.

  • Thanks Dwain,

    OK it's not pretty because of all the slicing and dicing of the geometries into sub-geometries, then points, then X's and Y's then assembling into a formatted string, then adding pre- and post - text strings, (which I've removed from the code here, but still not talking correctly to the google maps API) then adding sort orders so the text lines came out in the right sequence ...

    BUT ...

    for my first venture into the world of Cross Apply, it did work!

    DECLARE @g geometry;

    --Create a multi-polygon geometry

    SET @g = geometry::STGeomFromText('POLYGON((0 2, 2 0, 4 2, 0 2))', 0)

    SELECT @g = @g.STUnion(geometry::STGeomFromText('POLYGON((3 3, 3 4, 4 4, 3 3))',0));

    IF @g.STNumGeometries() > 1 PRINT 'Multi-polygon detected';

    SELECT c.LineText FROM dbo.tally a

    CROSS APPLY

    (SELECT 0 as N, 'var polyg = [' as LineText UNION ALL

    -- this line appears first, so sort order is zero

    SELECT b.N, 'new L(' + str(@g.STGeometryN(a.N).STPointN(b.N).STY, 10,6)+','

    + str(@g.STGeometryN(a.N).STPointN(n).STX, 10,6) +'),'

    FROM dbo.Tally b where b.N < @g.STGeometryN(a.N).STNumPoints()

    UNION ALL

    SELECT b.N, 'new L(' + str(@g.STGeometryN(a.N).STPointN(b.N).STY, 10,6)+','

    + str(@g.STGeometryN(a.N).STPointN(n).STX, 10,6) +')'

    FROM dbo.Tally b where b.N = @g.STGeometryN(a.N).STNumPoints()

    -- above select is for the last line of the list, no trailing comma

    -- following lines sort to appear after each geometry definition list

    UNION ALL Select @g.STGeometryN(a.N).STNumPoints() + 1, '];'

    UNION ALL Select @g.STGeometryN(a.N).STNumPoints() + 2, '//map cmds here//;'

    ) c

    WHERE a.N <= @g.STNumGeometries() --and a.N = 1

    order by a.N, c.N;

    As an added bonus, it also works if I throw a single polygon geometry at it.