2016-06-21 86 views
0

我正在編寫一個使用實體框架6來做一些GeoSpatial的東西的API。實體框架DbGeography給我一個緯度FormatException錯誤

我已將zip data加載到數據庫表中,我通過郵政編碼查找並獲取其相應的經度和緯度信息。

正如我在測試不同的郵政編碼,我得到這個錯誤,當我擡起頭,用緯度/長90210

{ 
    "Message": "An error has occurred.", 
    "ExceptionMessage": "24201: Latitude values must be between -90 and 90 degrees.", 
    "ExceptionType": "System.FormatException", 
    "StackTrace": " at Microsoft.SqlServer.Types.GeographyValidator.ValidatePoint(Double x, Double y, Nullable`1 z, Nullable`1 m)\r\n at Microsoft.SqlServer.Types.Validator.BeginFigure(Double x, Double y, Nullable`1 z, Nullable`1 m)\r\n at Microsoft.SqlServer.Types.ForwardingGeoDataSink.BeginFigure(Double x, Double y, Nullable`1 z, Nullable`1 m)\r\n at Microsoft.SqlServer.Types.CoordinateReversingGeoDataSink.BeginFigure(Double x, Double y, Nullable`1 z, Nullable`1 m)\r\n at Microsoft.SqlServer.Types.WellKnownTextReader.ParsePointText(Boolean parseParentheses)\r\n at Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType type)\r\n at Microsoft.SqlServer.Types.WellKnownTextReader.Read(OpenGisType type, Int32 srid)\r\n at Microsoft.SqlServer.Types.SqlGeography.ParseText(OpenGisType type, SqlChars taggedText, Int32 srid)\r\n at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid)\r\n at Microsoft.SqlServer.Types.SqlGeography.Parse(SqlString s)" 
} 

是緯度從拉鍊查找/長數據回報:

latitude: "34.0901" 
longitude: "-118.4065" 

兩個當我仰望這些座標在谷歌地圖它確實表明了正確的位置(比佛利山莊,CA)被作爲字符串返回

此外,我得到的錯誤沒有意義,因爲34.0901是在-90 to 90範圍內。

以下是發生問題的相關代碼。當我創建zip數據庫時,我刪除了重複的zip文件,並將該zip文件作爲該表的關鍵字。

USZip zipData = context.USZip.Find(zip); 
string point = string.Format("POINT({0} {1})", zipData.latitude, zipData.longitude); 
var myLocation = DbGeography.FromText(point); 
+0

我張貼此我能準確地找到像我這樣的重複舊的問題後:http://stackoverflow.com/questions/16546598/dbgeography -pointfromtext-throws-latitude-values-must-between-90-and-90-d – Francisc0

回答

3

你有經緯度扭轉所以價值118.4065被視爲緯度,並且落在-90至90的範圍之外。

校正線應該是這樣的:

string point = string.Format("POINT({0} {1})", zipData.longitude, zipData.latitude); 
當然
+0

好奇的是爲什麼DBGeography要點違背了先放寬緯度的規範。但是,這工作,謝謝! – Francisc0