Spatial search error when using Swedish culture

25 views
Skip to first unread message

Christian Landgren

unread,
Mar 31, 2012, 5:12:18 PM3/31/12
to <ravendb@googlegroups.com>
Hi,

I just upgraded from buid 400 something to 700 and a code that previously worked fine now has broken and after some research I found that it must be because of the swedish decimal delimiter (comma) instead of . (dot).

This is the code:
public class City_ByCountryCodeAndLocation : AbstractIndexCreationTask<City>
        {
            public City_ByCountryCodeAndLocation()
            {
                Map = Cities => from r in Cities
                                     select new { r.CountryCode, _ = SpatialIndex.Generate(r.Position.Latitude, r.Position.Longitude) };
            }
        }

        public static City FindCity(Position near)
        {
            using (var session = Db.Store.OpenSession())
            {
                
                var nearestCity = session.Advanced.LuceneQuery<City>("City/ByCountryCodeAndLocation")
                    .Where("CountryCode:SE")
                    .WithinRadiusOf(radius: 10, latitude: near.Latitude, longitude: near.Longitude)
                    .SortByDistance().Take(10).ToList().OrderByDescending(c=>c.Region.Name).FirstOrDefault();
                return nearestCity;
            }
        }

This is the error:

{
  "Url": "/indexes/City/ByCountryCodeAndLocation?query=CountryCode%253ASE&start=0&pageSize=10&aggregation=None&sort=__distance&latitude=59%2C3389466&longitude=18%2C0572356&radius=10",
  "Error": "System.Data.ConstraintException: Illegal latitude value 593389466\r\n   at Lucene.Net.Spatial.Geometry.FloatLatLng..ctor(Double lat, Double lng) in z:\\Libs\\lucene.net\\src\\contrib\\Spatial\\Geometry\\FloatLatLng.cs:line 34

...

See that the %2C (comma) is removed in the illegal value 593389. 

This might be an error from lucene, if so please resend this email to them, otherwise please add an  InvariantCulture hint for the ToString method:
Convert.ToDouble(latitude, CultureInfo.InvariantCulture)

For me, the problem is solved right now by temporarily switch to a different culture for the Thread:
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

Keep up the good work! 


Best regards,

Christian Landgren, Iteam

Fitzchak Yitzchaki

unread,
Apr 1, 2012, 6:14:29 AM4/1/12
to rav...@googlegroups.com, Christian Landgren
Can you provide us with a full failing test?
You can follow up an existing test: Raven.Tests.Bugs.SpatialTest

Oren Eini (Ayende Rahien)

unread,
Apr 1, 2012, 6:17:02 AM4/1/12
to rav...@googlegroups.com, Christian Landgren
Note that we checked the code, and it says that it is using InvariantCulture there, so I am not sure how it go there.
Are you sure that you are also using the 700 client library?

Christian Landgren

unread,
Apr 1, 2012, 1:00:38 PM4/1/12
to Fitzchak Yitzchaki, <ravendb@googlegroups.com>
Hi,

After some more testing it was indeed wrong client assembly that was the reason for the problem. I had upgraded the web project but not the unit test project so was still using build 472.

If you want a test to test this here it is (it is working as it should :)


        [Fact]
        public void SpatialSearchWithSwedishCulture()
        {
            using (IDocumentStore store = NewDocumentStore())
            {
                using (IDocumentSession session = store.OpenSession())
                {
                    session.Store(new MySpatialDocument
                    {
                        Latitude = 12.3456789f,
                        Longitude = 12.3456789f
                    });
                    session.SaveChanges();
                }

                new MySpatialIndex().Execute(store);


                using (IDocumentSession session = store.OpenSession())
                {
                    System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("sv-SE");

                    Assert.Equal("1,5", (1.5f).ToString()); // Check that the culture change is affecting the current thread. Swedish decimal delimiter is comma.

                    var result = session.Advanced
                        .LuceneQuery<MySpatialDocument, MySpatialIndex>()
                        .WaitForNonStaleResults()
                        .WithinRadiusOf(radius:10, latitude: Convert.ToDouble(12.3456789f), longitude: Convert.ToDouble(12.3456789f))
                        .SortByDistance()
                        .Take(10).ToList()
                        .FirstOrDefault();

                    Assert.NotNull(result);
                }
            }
        }

Fitzchak Yitzchaki

unread,
Apr 16, 2012, 2:28:55 AM4/16/12
to Christian Landgren, <ravendb@googlegroups.com>
Test added
Reply all
Reply to author
Forward
0 new messages