use tempdb;
if exists (select 1 from sys.tables where name = 'grid')
drop table grid;
if not exists (select 1 from sys.tables where name = 'tally')
begin
create table tally (i int not null);
with
a as (select 1 as [i] union select 0),
b as (select 1 as [i] from a as [a1] cross join a as [a2]),
c as (select 1 as [i] from b as [a1] cross join b as [a2]),
d as (select 1 as [i] from c as [a1] cross join c as [a2]),
e as (select 1 as [i] from d as [a1] cross join d as [a2])
insert into tally
select row_number() over (order by i) from e
create unique clustered index [CI_Tally] on tally (i)
end
create table grid (
x tinyint,
y tinyint,
cell as geometry::STGeomFromText(
'POLYGON( (' +
cast(x as varchar) + ' ' + cast(-1*y as varchar) + ', ' +
cast(x+1 as varchar) + ' ' + cast(-1*y as varchar) + ', ' +
cast(x+1 as varchar) + ' ' + cast(-1*(y+1) as varchar) + ', ' +
cast(x as varchar) + ' ' + cast(-1*(y+1) as varchar) + ', ' +
cast(x as varchar) + ' ' + cast(-1*y as varchar) +
') )'
, 0)
);
insert into grid (x, y)
values
(1,1),
(2,1),
(2,2),
(3,2),
(8,1),
(9,1),
(8,2),
(9,2),
(9,3),
(10,1);
with cte as (
select cell, row_number() over (order by x, y) as [rn]
from grid
),
cte2 as (
select cell, [rn]
from cte
where [rn] = 1
union all
select a.cell.STUnion(b.cell) as [cell], b.rn
from cte2 as a
inner join cte as b
on a.rn + 1 = b.[rn]
), cte3 as (
select cell
from cte2
where [rn] = (select count(*) from grid)
), clusters as (
select i, cell.STGeometryN(t.i) as c
from cte3 as [a]
cross join tally as [t]
where t.i <= cell.STNumGeometries()
)
select *, c.STEnvelope() from clusters
此解决方案解决了您的原始问题和这个问题。我喜欢这个,因为你仍然可以使用你想要的任何奇怪的坐标系,它会做你想做的事。您所要做的就是相应地修改网格表上的计算列。我将把信封角的计算留给读者作为练习。:)
作为解释,计算列根据给定的 x 和 y 坐标创建一个 1x1 几何实例。从那里,我基本上将所有这些结合在一起,这将产生一个多面体。从那里,我遍历多面体中的各个多边形以获得各个簇。信封免费提供。从这里,如果您愿意,您应该能够将最终选择(或非常类似的东西)包装在视图中。