哪些多边形包含了这一点

时间:2014-04-14 20:21:09

标签: sql sql-server tsql geometry

我正在使用geometry::STPointFromText从文本中获取一个点,并使用geometry :: STGeomFromText创建我的多边形。 现在我在表格中设置了Area的多边形,并带有以下描述:

Id       int       PK
Name     varchar
Polygon  varchar

哪个区域包含STPointFromText('POINT(12 31)',4326)? 我为此目的需要查询。

2 个答案:

答案 0 :(得分:1)

如果向表中添加一个表示实际几何对象的计算列,则可以采用基于集合的方式(比上面基于光标的方法更好地扩展)。保持列是可选的,但是每次运行查询时都会阻止引擎动态创建几何对象。类似的东西:

alter table [Area] add g as as geometry::STGeomFromText([Polygon], 4326) persisted
DECLARE @point geometry = geometry::STPointFromText('POINT(539749 3953079)', 4326)
select * from [Area] where g.STIntersects(@point) = 1

答案 1 :(得分:0)

我找到了解决方案:

DECLARE @polygon geometry
DECLARE @selectedId int
DECLARE @point geometry = geometry::STPointFromText('POINT(539749 3953079)', 4326)
DECLARE geo_cur CURSOR FOR 
SELECT geometry::STGeomFromText('POLYGON(('+Polygon+'))', 4326),id FROM Area where Polygon is not null
OPEN geo_cur
FETCH NEXT FROM geo_cur
INTO @polygon,@selectedId

WHILE @@FETCH_STATUS = 0
    BEGIN

        IF @polygon.STIntersects(@point) = 1
            BEGIN
                PRINT @selectedId
            END

        FETCH NEXT FROM geo_cur
        INTO @polygon,@selectedId

    END
CLOSE geo_cur;
DEALLOCATE geo_cur;