I am attempting to convert some coordinates into a geography object. Below is a simple test harness duplicating the problem.
Due to MS Geography requiring the coordinates in counter-clockwise, and my having no idea what the geozones I have are in, I get the coordinates and then prepare a second set sorted in descending order. This is not lat/long to long/lat, just 1,2,3,4,5 to 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
I receive the following error
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)
The polygon is a square. Last coordinate matches first coordinate. Looking at the coords they are all in lat/lng, with all lats = -33.something, well within the -90 to 90 limit
From what I can see I have no problem with invalid lats or longs. Why does MS Geography believe that I do?
You've got your latitude and longitude values switched around. How could you prove this to yourself?
DECLARE @g GEOGRAPHY = geography::STPointFromText('POINT(10 20)', 4326)
SELECT @g.[Lat]; --returns 20
So, the WKT representation is going to be in (Longitude, Latitude) pairs.