DbGeography.PointFromText 24141: A number is expected at position
Asked Answered
F

1

9

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)

Ferruginous answered 1/3, 2013 at 22:17 Comment(3)
The values provided from your client are not correct double values. Note that this might be a matter locale settings - e.g. you write double with decimal coma and SqlTypes expect decimal point.Eleusis
Thanks Pawel, it realy helped.Ferruginous
Had the same issue and it was the locale settings on the second machine (the one that was giving problems). Thanks for the heads up Pawel!Ileus
C
13

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);
Changeup answered 21/10, 2014 at 16:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.