TSQL地理空间数据多边形纬度/长搜索

时间:2015-01-12 17:08:27

标签: sql-server-2008-r2 polygon geospatial sqlgeography

TSQL SQL Server 2008 r2

是否可以存储由纬度和经度组成的多边形,然后在该多边形内执行搜索,以查看给定的纬度/经度是否存在于该多边形内?

如果是这样,那怎么办呢?

如果没有,有人可以就最佳方式提出建议吗?

示例多边形

LatLng(55.297622963050465, -4.627166781574488),
LatLng(55.25851203752759, -3.5724792815744877),
LatLng(55.034056344339206, -3.5450134612619877),
LatLng(54.955277168321636, -3.7647400237619877),
LatLng(54.92372217917785, -4.113555941730738),
LatLng(54.96946883538248, -4.561248812824488),
LatLng(55.151935308382306, -4.712310824543238);

Lat / Long搜索

LatLng(55.10029008969451, -4.100017953449488),

提前致谢

2 个答案:

答案 0 :(得分:1)

简而言之,是的,这是可能的。我建议你从以下MSDN链接开始,并从那里做一些研究:

Working with Spatial Data

具体而言,请注意以下可用于获得答案的方法:

  • STWithin()
  • STIntersects()

请注意,对于SQL 2008 R2,您只能使用不超过单个半球的多边形。如果您需要支持更大的对象,我会考虑SQL 2012或更高版本。

基本示例:

declare @g geography = Geography::STPolyFromText("<WKT Polygon Text>, 4326); -- 4326 is a common SRID
declare @p point = Geography::STPointFromText("<WKT Point Text>, 4326); -- SRID MUST match polygon

select @p.STWithin(@g); -- 1 is true, 0 is false

答案 1 :(得分:0)

感谢@Jon Bellamy我能够找到这个解决方案

随着Ed's Spatial Blog的帮助,“修复”多边形环定位问题

IF OBJECT_ID ( 'dbo.SpatialTable', 'U' ) IS NOT NULL 
    DROP TABLE dbo.SpatialTable;
GO

CREATE TABLE SpatialTable 
    ( id INT IDENTITY (1,1),
    GeogCol1 GEOGRAPHY, 
    GeogCol2 AS GeogCol1.STAsText() );
GO

DECLARE @geom GEOMETRY = 'POLYGON(( 55.297622963050465 -4.627166781574488,
                                    55.25851203752759 -3.5724792815744877,
                                    55.034056344339206 -3.5450134612619877,
                                    54.955277168321636 -3.7647400237619877,
                                    54.92372217917785 -4.113555941730738,
                                    54.96946883538248 -4.561248812824488,
                                    55.151935308382306 -4.712310824543238,
                                    55.297622963050465 -4.627166781574488))';

DECLARE @geog GEOGRAPHY = @geom.MakeValid().STUnion(@geom.STStartPoint()).STAsText()

INSERT INTO SpatialTable (GeogCol1) VALUES (@geog)

DECLARE @g GEOGRAPHY 
SET @g = (SELECT GEOGRAPHY::STPolyFromText(GeogCol2, 4326) FROM dbo.SpatialTable); -- 4326 is a common SRID

DECLARE @p GEOGRAPHY
SET @p = GEOGRAPHY::STPointFromText('POINT(55.10029008969451 -4.100017953449488)', 4326); -- SRID MUST match polygon

SELECT @p.STIntersects(@g); -- 1 is true, 0 is false

IF OBJECT_ID ( 'dbo.SpatialTable', 'U' ) IS NOT NULL 
    DROP TABLE dbo.SpatialTable;
GO