0

考虑我们有一些区域和多个点存储在 SQL Server 的两个表中:

CREATE TABLE [dbo].[Areas](
    [Id] [int] NOT NULL,
        [Location] [geometry] NOT NULL)


        CREATE TABLE [dbo].[Points](
    [Id] [int] NOT NULL,
        [Location] [geometry] NOT NULL)

我知道 STIntersects 用于检查多边形是否包含点的函数。
有没有办法使用单个查询在每个区域中查找点?
我不知道如何在区域中找到点,因为有多个区域。我应该使用游标还是迭代?如果是怎么办?

4

2 回答 2

2

有趣的。我从未使用过几何数据类型。但这是我的第一个猜测。

您可以使用Point几何字段的几何方法STWithin并传入Area几何对象以查看该点是否在该区域“内”

declare @Areas table ( AreaID int identity(1,1), Description varchar(50), Area geometry)
declare @Points table ( PointID int identity(1,1), Description varchar(50), Point geometry )

insert @Areas ( Description, Area )
select 'Area 1', geometry::STGeomFromText('POLYGON((0 0, 0 3, 3 3, 3 0, 0 0))', 0)

insert @Areas ( Description, Area )
select 'Area 2', geometry::STGeomFromText('POLYGON((10 10, 10 15, 15 15, 15 10, 10 10))', 0)

insert @Points ( Description, Point )
select 'Point 1', geometry::STPointFromText('POINT(2 2)', 0)

insert @Points ( Description, Point )
select 'Point 2', geometry::STPointFromText('POINT(4 4)', 0)

insert @Points ( Description, Point )
select 'Point 3', geometry::STPointFromText('POINT(12 13)', 0)


select a.Description, p.Description, case when p.Point.STWithin(a.Area) = 1 then 'Yes' else 'No' end as PointWithin
from @Areas a, @Points p

结果:

Description Description PointWithin
----------- ----------- -----------
Area 1      Point 1     Yes
Area 2      Point 1     No
Area 1      Point 2     No
Area 2      Point 2     No
Area 1      Point 3     No
Area 2      Point 3     Yes

希望这可以帮助

于 2013-06-03T05:45:11.457 回答
1

您需要一种方法来比较每个点和每个区域。交叉连接实现了这一点。所以是这样的:

select a.Id as [AreaId]
     , p.Id as [PointId]
from dbo.Areas as a
cross join dbo.Points as p
where a.Location.STContains(p.Location) = 1
于 2013-06-03T12:53:48.017 回答