SQL Server 2008 Spatial:在多边形中查找一个点

我正在使用 SQL Server 2008 空间数据类型.我有一个表,所有状态(作为多边形)作为数据类型 GEOMETRY.现在我想检查一个点的坐标(纬度、经度)作为数据类型 GEOGRAPHY 是否在该州内.

I am using SQL Server 2008 spatial data types. I have a table with all States (as polygons) as data type GEOMETRY. Now I want to check if a point's coordinates (latitudes, longitudes) as data type GEOGRAPHY, is inside that State or not.

我找不到任何使用新空间数据类型的示例.目前,我有一个多年前实施的解决方法,但它有一些缺点.

I could not find any example using the new spatial data types. Currently, I have a workaround which was implemented many years ago, but it has some drawbacks.

我同时拥有 SQL Server 2008 和 2012.如果新版本有一些增强,我也可以开始使用它.

I've both SQL Server 2008 and 2012. If the new version has some enhancements, I can start working in it too.

谢谢.

更新 1:

为了更清楚,我添加了一个代码示例.

I am adding a code sample for a bit more clarity.

declare @s geometry  --GeomCol is of this type too.
declare @z geography --GeogCol is of this type too.

select @s = GeomCol
from AllStates
where STATE_ABBR = 'NY'

select @z = GeogCol
from AllZipCodes
where ZipCode = 10101

推荐答案

我认为 geography 方法 STIntersects() 会做你想做的:

I think the geography method STIntersects() will do what you want:

DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326);
SET @h = geography::Point(47.653, -122.358, 4326)

SELECT @g.STIntersects(@h)

相关文章