在我的表中有一个存储多边形的几何字段。
彼此相交的多边形必须连接并形成一个多边形(见下文)
(来源:location-world.com)
结果无法计算,但必须在飞行中执行。我已经用光标完成了,但它需要太多时间。
有没有办法在不使用游标的情况下执行任务?我正在使用 Sql Server 2008 R2。
我附上了使用游标的源代码。
DECLARE @Results TABLE (ResultId INT, Shape GEOMETRY)
DECLARE @ExistingIds TABLE (Id VARCHAR(50))
DECLARE @Id VARCHAR(50), @Shape GEOMETRY
DECLARE ViasCursor CURSOR FOR
SELECT ID, shape
FROM [dbo].[layer.2502]
OPEN ViasCursor
FETCH ViasCursor INTO @Id, @Shape
DECLARE @ResultId INT = 0
WHILE ( @@FETCH_STATUS = 0 )
BEGIN
IF NOT EXISTS ( SELECT Id FROM @ExistingIds WHERE Id = @Id )
BEGIN
DECLARE @Geo GEOMETRY = NULL
SELECT @Geo = COALESCE(@Geo.STUnion(Shape), Shape)
WHERE [Shape].STIntersects(@Shape) = 1
INSERT INTO @ExistingIds ( Id )
SELECT [Id]
FROM [dbo].[layer.2502]
WHERE [Shape].STIntersects(@Shape) = 1
DECLARE @ExistingId INT = NULL
SELECT @ExistingId = [ResultId]
FROM @Results
WHERE Shape.STIntersects(@Geo) = 1
IF @ExistingId IS NOT NULL
UPDATE @Results SET [Shape] = [Shape].STUnion(@Geo)
WHERE ResultId = @ExistingId
ELSE
BEGIN
INSERT INTO @Results ( [ResultId], [Shape] )
VALUES ( @ResultId, @Geo )
SET @ResultId = @ResultId + 1
END
END
FETCH ViasCursor INTO @Id, @Shape
END
CLOSE ViasCursor
DEALLOCATE ViasCursor
SELECT * FROM @Results