SQL: Union of polygons

2019-02-14 14:58发布

I have a table with a single column of geometry type, containing polygons. How do I get the union of all the polygons in the table?

4条回答
\"骚年 ilove
2楼-- · 2019-02-14 15:11

you mean, if you have 2 squares, lets say

(0 0 , 1 0 , 0 1 , 1 1) 

and

(1 0 , 1 1 , 2 0 , 2 1)

you end up with a rectangle:

(0 0, 2 0 ,0 1, 2 1)

if yes, try the GPC (General Polygon Clipper library)

This link may also help you

查看更多
爷的心禁止访问
3楼-- · 2019-02-14 15:21

This worked for me:

CREATE TABLE #g (i INT IDENTITY, a geometry)
INSERT INTO #g (a)
VALUES
    (geometry::STGeomFromText(
        'POLYGON((0 0, 3 0, 3 3, 0 3, 0 0))', 0)
    ),
    (geometry::STGeomFromText(
        'POLYGON((5 2, 7 2, 7 0, 5 0, 5 2))', 0)
    )

DECLARE @g geometry
SELECT TOP 1 @g = a FROM [#g]
SELECT @g = @g.STUnion(a) FROM #g

SELECT @g

So, apparently, the STUnion method returns null when either the instance on which it's being called or the operand is null, hence the select top 1 hack.

查看更多
淡お忘
4楼-- · 2019-02-14 15:30

In SQL Server 2012:

SELECT geometry::UnionAggregate(geomcolumn) FROM YourTable;

In SQL Server 2008/R2:

DECLARE @g = geometry::STGeomFromText('GEOMETRYCOLLECTION EMPTY', YourSRID);
SELECT @g = @g.STUnion(geomcolum) FROM YourTable;
查看更多
叼着烟拽天下
5楼-- · 2019-02-14 15:33

SELECT DISTINCT geometry from [your table] ?

查看更多
登录 后发表回答