Search code examples
c#sql-server-2012sqlgeography

c# SqlGeography latitude & longitude unexpected behavior


Microsoft.SqlServer.Types.SqlGeography.ToString has unexpected behavior.

Take a look at my unit test:

 latitude = 40.584474F;
 longitude = -111.633491F;
 var location = SqlGeography.Point( latitude, longitude, 4326 );
 var point = location.ToString();

At this point, the variable point has a value of: POINT (-111.63349151611328 40.58447265625)

As you can see, latitude and longitude have been swapped from the norm.

This is from a nuget package downloaded today. (v11.0.2)

Clarification: Object values are correct but ToString() is producing a format which is not appropriate to SQL Server itself, even though this is a SQLServer specific class in my opinion.

Update 2: This is not a duplicate because it is different from the question with a similar title, since this is about a .NET class (Microsoft.SqlServer.Types.SqlGeography.ToString), rather than SQL Server itself.


Solution

  • SQL Server 12 will accept: geography::Point(47.65100, -122.3490, 4326). To produce the string that would be acceptable to SQL Server:

    var point = ColValue as SqlGeography;
    if (point != null)
    return "geography::Point(" + point.Lat + ", " + point.Long + ", 4326)";