3

In my table there is a geometry field that stores polygons.

The polygons that intersects each other must be attached to and form a single polygon (see below)

Image
(source: location-world.com)

The results can not be calculated but must be executed on the flight. I've done it with a cursor but it takes too much time.

Is there a way to perform the task without using cursors? I'm using Sql Server 2008 R2.

I have attached the source code that uses cursors.

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
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • 1
    What do you mean by (Photography)? – David Segonds Nov 26 '12 at 21:53
  • Sorry, I tried to attach a photography but cause I'm a new member I wasn't allowed to do it. – harleyrjacome Nov 26 '12 at 22:26
  • Can you post a link to the photography in a comment? – David Segonds Nov 26 '12 at 22:31
  • Having a complete sample (tables, sample data, then the cursor query) would help. My general strategy would be to calculate all pairs of IDs that intersect. You should then be able to compute the lowest ID that can be reached (transitively) from any particular ID, using a recursive CTE (This step doesn't depend on geometry at all). Then, if you need the actual shapes, you can use a second recursive CTE to start with the distinct lowest IDs and shapes and accumulate each other shape into it. – Damien_The_Unbeliever Nov 27 '12 at 15:16

1 Answers1

3

It's hard to create a working solution for this without a schema and data to test with, but here are a few pointers.
In this question they grouping spatial data, but with contains, instead of intersect: How can I group SQL Server geometry data by STWithin?
And in this question they are using the STUnion on the geometry column: Union all geometry in a SQL Server table like GeomUnion in Postgres
If you combine these methods you may come up with something that work.

HOWEVER: I have not tested the performance, and I posters did warn against it in the previous link...

I hope it at least can be of inspiration.

Community
  • 1
  • 1
Tomas
  • 3,573
  • 2
  • 20
  • 25