Union all geometry in a SQL Server table like GeomUnion in Postgres

北慕城南 提交于 2020-01-29 11:33:47

问题


Just to clarify up-front: I'm talking about unioning geometry, not the SQL keyword UNION.

I'm trying to move some spatial data from Postgres with PostGIS to SQL Server 2008. It was fine until I saw a statement like this:

SELECT GeomUnion(the_geom) FROM some_table

This unions all geometry in that column and return it as one result (similar to how COUNT works). As far I know, SQL Server only has the STUnion function, which unions one geometry with another. Is there any way to do something similar to the Postgres way?

If it helps, the STUnion function works like this:

SELECT first_geometry_column.STUnion(second_geometry_column) FROM some_table

回答1:


Is the UnionAggregate function SQL2012 only?

SELECT geography::UnionAggregate( geometry ) FROM some_table

Hmm guess so. http://technet.microsoft.com/en-us/library/ff929095.aspx




回答2:


The way I ended up doing this is with variables:

DECLARE @Shape GEOMETRY
SET @Shape = GEOMETRY::STGeomFromText('GEOMETRYCOLLECTION EMPTY', @MySrid)

SELECT @Shape = @Shape.STUnion(Shape)
  FROM MyShapeTable

It's not as nice, but it works.




回答3:


Your best option is to create a CLR function to support the aggregate. There are a couple of existing solutions:

  • Sql spatial tools
  • Spatail aggregate


来源:https://stackoverflow.com/questions/3293190/union-all-geometry-in-a-sql-server-table-like-geomunion-in-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!