为什么MS地理位置抱怨这个多边形?

时间:2016-07-28 00:53:37

标签: sql-server tsql sqlgeography

我正在尝试将一些坐标转换为地理对象。下面是一个简单的测试工具,重复了这个问题。

由于MS地理位置需要逆时针方向的坐标,而我不知道我所拥有的geozones是什么,我得到坐标然后准备第二组按降序排序。这不是lat / long到long / lat,只是1,2,3,4,5到5,4,3,2,1:

DECLARE @MyPolygon geography
DECLARE @FwdCoords varchar(max) = 'POLYGON((-33.871090 150.823941, -33.878274 150.823941, -33.878274 150.831348, -33.871090 150.831348, -33.871090 150.823941))'
DECLARE @RevCoords varchar(max) = 'POLYGON((-33.871090 150.823941, -33.871090 150.831348, -33.878274 150.831348, -33.878274 150.823941, -33.871090 150.823941))'

BEGIN TRY
    RAISERROR('Attempt to make polygon from forward string', 0, 1)
    SET @MyPolygon = geography::STPolyFromText(@FwdCoords, 4326)
    PRINT @MyPolygon.ToString()
END TRY
BEGIN CATCH
    RAISERROR('Attempt failed.  Try with reversed coordinates', 0, 1)
    IF @@ERROR <> 0
    BEGIN
        PRINT 'Proc: ' + ERROR_PROCEDURE()
        PRINT 'Line: ' + CONVERT(VARCHAR(10), ERROR_LINE())
        PRINT 'Number: ' + CONVERT(VARCHAR(10), ERROR_NUMBER())
        PRINT 'Message: ' + ERROR_MESSAGE()
    END
    BEGIN TRY
        RAISERROR('Attempt to make polygon from reversed string', 0, 1)
        SET @MyPolygon = geography::STPolyFromText(@RevCoords, 4326)
        PRINT @MyPolygon.ToString()
    END TRY
    BEGIN CATCH
        IF @@ERROR <> 0
        BEGIN
            PRINT 'Proc: ' + ERROR_PROCEDURE()
            PRINT 'Line: ' + CONVERT(VARCHAR(10), ERROR_LINE())
            PRINT 'Number: ' + CONVERT(VARCHAR(10), ERROR_NUMBER())
            PRINT 'Message: ' + ERROR_MESSAGE()
        END
    END CATCH
END CATCH

我收到以下错误

Line: 22
Number: 6522
Message: A .NET Framework error occurred during execution of user-defined routine or aggregate "geography": 
System.FormatException: 24201: Latitude values must be between -90 and 90 degrees.
System.FormatException: 
   at Microsoft.SqlServer.Types.GeographyValidator.ValidatePoint(Double x, Double y, Nullable`1 z, Nullable`1 m)
   at Microsoft.SqlServer.Types.Validator.BeginFigure(Double x, Double y, Nullable`1 z, Nullable`1 m)
   at Microsoft.SqlServer.Types.ForwardingGeoDataSink.BeginFigure(Double x, Double y, Nullable`1 z, Nullable`1 m)
   at Microsoft.SqlServer.Types.CoordinateReversingGeoDataSink.BeginFigure(Double x, Double y, Nullable`1 z, Nullable`1 m)
   at Microsoft.SqlServer.Types.OpenGisWktReader.ParseLineStringText()
   at Microsoft.SqlServer.Types.OpenGisWktReader.ParsePolygonText()
   at Microsoft.SqlServer.Types.OpenGisWktReader.ParseTaggedText(OpenGisType type)
   at Microsoft.SqlServer.Types.OpenGisWktReader.Read(OpenGisType type, Int32 srid)
   at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid)

多边形是正方形。上一个坐标匹配第一个坐标看看它们都在lat / lng中,所有lats = -33.something,都在-90到90之间

从我所看到的情况来看,无效的拉特或多头没问题。为什么MS地理相信我这样做?

1 个答案:

答案 0 :(得分:2)

您已经切换了纬度和经度值。你怎么能证明这一点?

DECLARE @g GEOGRAPHY = geography::STPointFromText('POINT(10 20)', 4326)

SELECT @g.[Lat]; --returns 20

因此,WKT表示将在(经度,纬度)对中。