如何将多个LineString行组合到单个行集合中

时间:2011-11-16 16:00:43

标签: sql sql-server-2008 geospatial spatial

我正在使用SQL Server 2008和Geometry数据类型来存储我已从Ordanance Survey STRATEGI数据集导入的英国道路列表。

每条道路分为多个行,每行包含一行(A Linestring由一个段组成)。例如,A369由18条单独的线组成,如下图所示:

Screen capture of current linestrings

我想要做的是收集包含道路部分的所有单独行,并创建一个新行,将所有组合的单独行保存为一个线串。

换句话说,运行代码SELECT * FROM Structure WHERE Name = 'A369'只会返回一行,但仍会绘制上图中所示的道路。

2 个答案:

答案 0 :(得分:6)

只需使用.STUnion

BEGIN
-- create a test table
DECLARE @test TABLE(seg GEOMETRY);
INSERT INTO @test VALUES(geometry::STGeomFromText('LINESTRING (0 0, 50 100)', 0))
INSERT INTO @test VALUES(geometry::STGeomFromText('LINESTRING (50 100, 100 200)', 0))
INSERT INTO @test VALUES(geometry::STGeomFromText('LINESTRING (100 200, 150 300)', 0))
--SELECT seg.STAsText() FROM @test
DECLARE @geom GEOMETRY
SELECT @geom = (SELECT TOP 1 seg FROM @test)
-- union all the linestring points
SELECT @geom = @geom.STUnion([seg]) FROM @test
-- do what you want with the results
SELECT @geom
print(@geom.STAsText())
END

答案 1 :(得分:2)

在SQL 2012中,您可以使用UnionAggregate

SELECT geometry::UnionAggregate(shape) FROM Table

或者如果您有地理列

SELECT geography ::UnionAggregate(shape) FROM Table
相关问题