SQL 合并多组重叠多边形的服务器空间方法

SQL Server Spatial approach to merging sets of overlapping polygons

我们需要合并多组重叠的几何多边形。使用溶解或类似功能在桌面 GIS 中通常很容易。然而,我们有超过 1000 万个多边形需要测试,而桌面 GIS 无法应对 - 不足为奇。

所以我在 SQL 服务器上尝试这个,我用它来进行交叉分析,以对相似数量的多边形和线进行交叉分析,但我对此感到困惑。

使用以下 9 个方块的测试数据,我希望得到 5 个合并方块、2 个合并方块和两个未合并的相邻方块的结果。

CREATE TABLE [dbo].[TestPolygons](
        [OBJECTID] [int] IDENTITY(1,1) NOT NULL,
        [GeomPoly] [geometry] NULL,
     CONSTRAINT [PK_TestPolygons] PRIMARY KEY CLUSTERED 
    (
        [OBJECTID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO

    -- Overlapping pair   
    insert into dbo.TestPolygons (GeomPoly) values (geometry::STGeomFromText('POLYGON ((0 0, 40 0, 40 40, 0 40, 0 0))',27700));
    insert into dbo.TestPolygons (GeomPoly) values (geometry::STGeomFromText('POLYGON ((30 30, 70 30, 70 70, 30 70, 30 30))',27700));

    -- no overlapping, just adjacent
    insert into dbo.TestPolygons (GeomPoly) values (geometry::STGeomFromText('POLYGON ((120 0, 160 0, 160 40, 120 40, 120 0))',27700));
    insert into dbo.TestPolygons (GeomPoly) values (geometry::STGeomFromText('POLYGON ((160 40, 200 40, 200 80, 160 80, 160 40))',27700));

    -- additional overlaps
    insert into dbo.TestPolygons (GeomPoly) values (geometry::STGeomFromText('POLYGON ((60 60, 100 60, 100 100, 60 100, 60 60))',27700));
    insert into dbo.TestPolygons (GeomPoly) values (geometry::STGeomFromText('POLYGON ((90 90, 130 90, 130 130, 90 130, 90 90))',27700));
    insert into dbo.TestPolygons (GeomPoly) values (geometry::STGeomFromText('POLYGON ((60 0, 100 0, 100 40, 60 40, 60 0))',27700));

    -- overlapping pair
    insert into dbo.TestPolygons (GeomPoly) values (geometry::STGeomFromText('POLYGON ((220 0, 260 0, 260 40, 220 40, 220 0))',27700));
    insert into dbo.TestPolygons (GeomPoly) values (geometry::STGeomFromText('POLYGON ((250 30, 290 30, 290 70, 250 70, 250 30))',27700));

Test Data

我已经将所有内容合并到一个包含两个多边形的记录中,但这已经删除了未合并的记录。这是代码;

select geometry::UnionAggregate(inter_geometry) as intersection_union
from
(
    select P.objectid as id1, T.objectid as id2, P.GeomPoly.STUnion(T.GeomPoly) as inter_geometry
    from dbo.TestPolygons P
    inner join dbo.TestPolygons T on P.objectid < T.objectid
    where P.GeomPoly.STOverlaps(T.GeomPoly) = 1
) N

This is what the result looks like.

希望有人能提供帮助 - 我发现 Whosebug 过去非常有用,但这是我的第一个问题。

阿德里安

The result I'm after for the test data is four records; 1 record of a merged set of the five squares that serially overlap, 1 record of a merged set of the two overlapping squares, 2 records each of a single square.

酷。 UnionAggregate 为您完成所有艰苦的工作,将这些形状编织成一个单一的多边形。然后你只需要枚举组成它的几何图形,就像这样:

with q as
(
    select geometry::UnionAggregate(GeomPoly) g
    from TestPolygons
), n as
(
  select top 10000 row_number() over (order by (select null)) i
  from (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10))       v1(i)

) 
select q.g.STGeometryN(n.i) 
from q
cross apply (select i from n where i <= q.g.STNumGeometries())  n

输出四行: