2009-12-09 31 views
1

我使用Spatial.NHibernate一些幾何形狀保存到SQL Server中的地理列2008年這裏是我的映射:堅持有效的幾何形狀到SQL Server 2008地理列

public class AreaMapping : ClassMap<Area> 
{ 
    public AreaMapping() 
    { 
     Id(c => c.Id).GeneratedBy.HiLo(100.ToString()); 
     Map(c => c.Name).Not.Nullable(); 
     Map(x => x.Boundary) 
      .CustomTypeIs<MsSql2008GeographyType>() 
      .Not.Nullable() 
      .CustomSqlTypeIs("GEOGRAPHY"); 
    } 
} 

的映射出現有效。這裏是類:

public class Area 
{ 
    public virtual Guid Id { get; set; } 
    public virtual Polygon Boundary { get; set; } 
    public virtual string Name { get; set; } 
} 

但是當我去救人這樣的區域:

Area area = new Area{ Boundary = new Polygon(new LinearRing(new ICoordinate[]{ 
        new Coordinate(-1.911524, 55.136334), 
        new Coordinate(-1.912679, 55.136293), 
        new Coordinate(-1.912689, 55.136178), 
        new Coordinate(-1.911507, 55.136194), 
        new Coordinate(-1.911524, 55.136334)})) 
Session.Save(area); 

我收到以下錯誤:

The specified input does not represent a valid geography instance.

Type: System.ArgumentException Source: Microsoft.SqlServer.Types TargetSite: Microsoft.SqlServer.Types.SqlGeography ConstructGeographyFromUserInput(Microsoft.SqlServer.Types.GeoData, Int32) ...etc.

我明白,一個有效的多邊形的地理類型必須逆時針繪製,並且必須關閉,並且不能重疊。我很確定我正在履行所有這些限制(雖然如果我錯了,請糾正我),所以我在這裏有點難住。要麼我的多邊形出現問題,要麼NHibernate不能正確保存它 - 歡迎任何幫助!

編輯 好吧,我現在很困惑。

爲了簡化問題,我改變了我的多邊形這樣:

Area area = new Area{ Boundary = new Polygon(new LinearRing(new ICoordinate[]{ 
        new Coordinate(10,15), 
        new Coordinate(10,5), 
        new Coordinate(20,5), 
        new Coordinate(20,15), 
        new Coordinate(10,15)})) 

我得到同樣的

The specified input does not represent a valid geography instance.

注意,多邊形繪製逆時針as it should be according to various sources)。但如果我改變我的座標爲順時針:

Area area = new Area{ Boundary = new Polygon(new LinearRing(new ICoordinate[]{ 
        new Coordinate(10,15), 
        new Coordinate(20,15), 
        new Coordinate(20,5), 
        new Coordinate(10,5), 
        new Coordinate(10,15)})) 

這似乎是好的。那麼順時針有效還是什麼?

回答

2

好的,我知道發生了什麼事。微軟以其無盡的智慧,似乎違背了整個世界其他國家的政策,並決定在他們的WKT地理座標表示中,他們將Latitiude放在Longitude(Y,X)之前。

X,Y? WTF你可能會問?!那麼a whole debate about it,顯然他們有他們的理由,但普遍的共識是,它很爛,所以他們是going to change it

它很糟糕,因爲它完全消除了互操作性。當NHibernate空間堅持NetTopologySuite對象我用來堅持地理(IGeography類型)它將X,Y(我認爲它應該工作的自然,預期的方式)的座標寫出來。當它碰到SQL服務器時,它試圖將X解釋爲Y,Y解釋爲X,因此我用無效類型解決了所有問題。

所以現在我要麼修復NHibernate,要麼在我的代碼中交換座標。恥辱你,微軟,讓我如此痛苦!