1

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.

Gunnar
  • 339
  • 2
  • 13

2 Answers2

3

The latitude and longitude have not been swapped. It's just that the Point() constructor accepts the longitude after latitude, but in ToString() longitude is printed before latitude.

Print the values location.Lat and location.Long to know for sure.

displayName
  • 13,888
  • 8
  • 60
  • 75
  • @Gunnar: It can't be called a bug as the values are just fine. It, however, is a little poorly designed API that swaps the order of variables and leads to confusion. – displayName Aug 21 '15 at 14:03
  • displayName, That's a design bug at least. Logically, the ToString of a class in Microsoft.SqlServer.Types should naturally produce the string that would be acceptable to SQL Server. SQL Server will accept: geography::Point(47.65100, -122.3490, 4326) If a different format is needed for some edge case, the function should make that clear, e.g. AsWGS84() or PointLongLat() – Gunnar Aug 21 '15 at 14:49
  • 30+ years of software engineering experience tells me that designs can have bugs, same as code. In fact, design bugs are far more prevalent and important. A bug is any problem. A code bug = "not working as designed", while design bug = "not designed to meet requirements" and a specification bug = "not specified to meet user needs". Good bug tracking systems support these types of issues. Your definition is too narrow for industry best practices. – Gunnar Aug 21 '15 at 15:56
0

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)";
Gunnar
  • 339
  • 2
  • 13