3

我正在使用 SQL Server 2012 geography.STContains文档),但我不明白为什么下面的代码会失败。

如果我切换到geometry它工作。

有人可以解释吗?

//丹尼尔

declare @geo geography
set @geo = geography::STPolyFromText('POLYGON ((17.519133203852 59.8297423369731, 17.5190071588812 59.8296936773323, 17.5189979955459 59.8298203729009, 17.5191345140461 59.8298223425544, 17.519133203852 59.8297423369731))', 4326)

-- Is not within
declare @p1 geography
set @p1 = geography::STPointFromText('POINT(17.5184709839477 59.829925754067)', 4326)

-- Is within
declare @p2 geography
set @p2 = geography::STPointFromText('POINT(17.519060 59.829774)', 4326) 

select
    @geo.STContains(@p1), -- should be 0 is 1
    @geo.STContains(@p2) -- should be 1 is 0

更新: 如果我反转它们就可以了,但是我不明白:

declare @geo geography
set @geo = geography::STPolyFromText('POLYGON ((17.519133203852 59.8297423369731, 17.5190071588812 59.8296936773323, 17.5189979955459 59.8298203729009, 17.5191345140461 59.8298223425544, 17.519133203852 59.8297423369731))', 4326)

select
    @geo.STAsText() Polygon,
    @geo.STPointN(1).STAsText() Point1,
    @geo.STPointN(1).Lat Point1Latitud,
    @geo.STPointN(1).Long Point1Longitude

结果是:

Polygon 
POLYGON ((17.519133203852 59.8297423369731, 17.5190071588812 59.8296936773323, 17.5189979955459 59.8298203729009, 17.5191345140461 59.8298223425544, 17.519133203852 59.8297423369731))

Point1  
POINT (17.519133203852 59.8297423369731)

Point1Latitud   
59,8297423369731

Point1Longitude
17,519133203852
4

1 回答 1

3

现在我发现了问题。用户从右下角开始绘制多边形并顺时针方向移动。如果我从最大纬度重新排序这些点,然后通过对 long 进行逆向排序,lat 就可以了。为它找到了一个助手,但只有当你“知道这是错误的”时才有效:

if(sqlGeography.EnvelopeAngle() > 90)
    sqlGeography ? sqlGeography.ReorientObject();

只需组合一个可以修复我的价值观的小解决方案:https ://github.com/danielwertheim/GeographyFactory

和一篇关于它的博文:http: //danielwertheim.se/sqlgeography-in-sql-server-2012-polygon-must-start-on-correct-position/

并跟进真正的“问题”,即左手规则:

http://danielwertheim.se/sqlgeography-in-sql-server-2012-polygon-must-start-on-correct-position-no/

于 2012-11-30T15:35:45.463 回答