Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get country code by latitude and longitude using NodaTime

Tags:

c#

nodatime

Since NodaTime already has lat/long and country code data within the olson database, I was wondering if we can specify any lat/long (say any lat/long returned by GeoLocator.GetGeopositionAsync in windows store apps) and determine the timezone and country code from it?

Something similar to this: var zone = session.GetZoneForLocation(latitude, longitude); This is from https://github.com/mj1856/Raven.TimeZones

I am specifically looking at an offline solution like NodaTime and not using web services.

like image 367
Girish Avatar asked Sep 20 '13 07:09

Girish


1 Answers

The lat/lon data that exists within NodaTime comes from the zone.tab file in the Olson data. This gives the location of the point on the map that the zone uses as a reference.

If that was the only data you had available, the best you could do for an arbitrary location would be to find the closest point. In some cases, this will give you an accurate time zone, but in many cases it will not.

Consider the following example (and please excuse my poor artwork)

                            Time Zone Example Art

The two squares represent different time zones, where the black dot in each square is the reference location, such as what you would find in zone.tab. The blue dot represents the location you are going to pass to the input query. Clearly, this location is within the orange zone on the left, but if we just look at closest distance to the reference point, it will resolve to the greenish zone on the right.

So zone.tab data (such as found in Noda Time) is not sufficient to perform this operation. Instead we need something that describes zones in terms of the shapes that define their boundaries, not just a single point. Fortunately, Eric Muller has been so kind to provide these shapes and put them in the public domain. You can find this data here.

My Raven.TimeZones project that you found does exactly that. It imports the data from Eric's shapefiles, and uses the geospatial features of RavenDB to index and query that data.

You can certainly use my implementation directly, or copy from it whatever parts you need. It works completely offline, making no web service calls. But it does require a license of RavenDB to operate.

If you are not able to use RavenDB, you can probably take a similar approach using any other database that supports complex spatial queries.

In particular - RavenDB cannot currently run in a pure WinRT environment, so you won't be able to use this directly in a Windows Store app. I'm uncertain if there are any embedded databases for WinRT that can perform geospatial queries. If anyone knows if any, please let us know.

Update

A consolidated list of time zone lookup methods can be found here.

like image 75
Matt Johnson-Pint Avatar answered Nov 15 '22 19:11

Matt Johnson-Pint