Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DbGeography.PointFromText 24141: A number is expected at position

I have WCF REST service which contains following code

var geo = DbGeography.PointFromText(string.Format("POINT({0} {1})", longitude, latitude), DbGeography.DefaultCoordinateSystemId);

Unit test works fine, but when I call this code from client or HTTP debuder providing any values of latitude and longitude exept zeros, it fails with exception:

"24141: A number is expected at position X of the input. The input has ,XXXXXX."
   at Microsoft.SqlServer.Types.WellKnownTextReader.RecognizeDouble()
   at Microsoft.SqlServer.Types.WellKnownTextReader.ParsePointText(Boolean parseParentheses)
   at Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType type)
   at Microsoft.SqlServer.Types.WellKnownTextReader.Read(OpenGisType type, Int32 srid)
   at Microsoft.SqlServer.Types.SqlGeography.ParseText(OpenGisType type, SqlChars taggedText, Int32 srid)
   at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid)
   at Microsoft.SqlServer.Types.SqlGeography.STPointFromText(SqlChars pointTaggedText, Int32 srid)

latitude and longitute for example
lat:37.58336895 long:-122.40549454
lat:37.38931302 long:-122.16207476

I used Microsoft.SqlServer.Types referenced from SQLServer installation directory and SqlGeography.Point for working with spartial data on code side. Now I want to use EF 5 features directly without using reference to Microsoft.SqlServer.Types. It fails with and without this reference.

Any idea what is wrong?

.NET 4.5 installed, SQL Server version is Microsoft SQL Server 2008 (SP3) - 10.0.5500.0 (Intel X86) Sep 22 2011 00:28:06 Copyright (c) 1988-2008 Microsoft Corporation Standard Edition on Windows NT 6.1 (Build 7601: Service Pack 1)

like image 677
Pavel Kharibin Avatar asked Mar 01 '13 22:03

Pavel Kharibin


1 Answers

You have to transform the coordinates using the invariantCulture before the concatenation.

  String lon = longitude.ToString(CultureInfo.InvariantCulture);
  String lat = latitude.ToString(CultureInfo.InvariantCulture);
  var geo = DbGeography.PointFromText(string.Format("POINT({0} {1})", lon, lat), DbGeography.DefaultCoordinateSystemId);
like image 103
Marco Staffoli Avatar answered Nov 20 '22 22:11

Marco Staffoli