将线串分解为SQL Server中的线段

时间:2012-08-25 08:38:01

标签: sql-server sql-server-2008 spatial sql-server-2012 spatial-query

如何将线串分解为线段?

行字符串如:

'LINESTRING(0 0, 2 2, 0 2, 0 5)'

1 个答案:

答案 0 :(得分:1)

这是我的解决方案!

DECLARE @g GEOMETRY
DECLARE @h GEOMETRY

SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 0 2, 0 5)', 0);
SET @h = geometry::STGeomFromText('LINESTRING(0 0, 3 3, 4 4, 0 4, 4 0, 2 1)', 0)

CREATE TABLE #Lines
(
   ID  int IDENTITY(1,1)PRIMARY KEY CLUSTERED,
   LINE GEOMETRY
)

CREATE TABLE #Segments
(
   ID  int IDENTITY(1,1)PRIMARY KEY CLUSTERED,
   LINE int,
   SEGMENT GEOMETRY
)

INSERT INTO #Lines (LINE)
SELECT @g

INSERT INTO #Lines (LINE)
SELECT @h

DECLARE @MaxNumOfPoints int 
SET @MaxNumOfPoints = (SELECT MAX(LINE.STNumPoints()) FROM #Lines)

SELECT TOP (SELECT @MaxNumOfPoints) IDENTITY(int,1,1) AS n
INTO #Numbers
FROM MASTER..spt_values a

INSERT INTO #Segments (LINE, SEGMENT)
SELECT ID, LINE.STPointN(#Numbers.n).STUnion(LINE.STPointN(#Numbers.n + 1)).STConvexHull()
FROM
 #Lines JOIN #Numbers
 ON #Numbers.n <= LINE.STNumPoints() - 1


SELECT ID, LINE, SEGMENT.ToString() FROM #Segments

DROP TABLE #Segments
DROP TABLE #Lines
DROP TABLE #Numbers