Reputation: 349
Is there a better way to create multipolygon from polygons than text concatenation?
I have multiple polygons inside my database (one per one row in database) and for some of those I need to create multipolygons. The only solution I can see is creating function that will:
I want from two polygons
polygon1: 'POLYGON((1 1, 1 -1, -1 -1, -1 1, 1 1))'
polygon2: 'POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))'
create one multipolygon:
result: 'MULTIPOLYGON(((1 1, 1 -1, -1 -1, -1 1, 1 1)), ((1 1, 3 1, 3 3, 1 3, 1 1)))'
It doesn't sound like a perfect solution so I'd like to know if there is a better way to do this.
Upvotes: 3
Views: 2187
Reputation: 176174
You could use STUnion
:
CREATE TABLE t(ID INT IDENTITY(1,1), c GEOMETRY);
INSERT INTO t(c)
VALUES ('POLYGON((1 1, 1 -1, -1 -1, -1 1, 1 1))')
,('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))');
DECLARE @g geometry = geometry::STGeomFromText('GEOMETRYCOLLECTION EMPTY', 0);
SELECT @g = @g.STUnion(c) FROM t
SELECT @g;
Upvotes: 2