Determine Timezone from Latitude/Longitude Without Using Web Services Like Geonames.Org

Determine timezone from latitude/longitude without using web services like Geonames.org

I had this problem a while back and did exactly what adam suggested:

  • Download the database of cities from geonames.org
  • convert it to a compact lat/lon -> timezone list
  • use an R-Tree implementation to efficiently lookup the nearest city (or rather, its timezone) to a given coordinate

IIRC it took less than 1 second to populate the R-Tree, and it could then perform thousands of lookups per second (both on a 5 year old PC).

Timezone lookup from latitude longitude

I looked fairly deeply into this question for a project I am working on. GeoNames.org and EarthTools.com are both good options for many situations but with the following serious flaws:

  • GeoNames.org finds the time zone by searching for the nearest point in their database that contains a time zone field. This often leads to the wrong result near borders. It is also painfully slow, leading to query times on the order of a couple seconds per request. It also doesn't return a valid time zone if there is no item in their database near the query point. GeoNames also restricts the number of queries that can be made per day, making bulk operations difficult.
  • EarthTools.org uses a map and is able to return queries quickly, but it doesn't take into account daylight savings time for most locations, and it returns a raw offset rather than a time zone ID (i.e., they return "GMT-7" instead of "America/Chicago"). Also, I just looked at their page while preparing this post and Google Chrome warned about malware on their site. That is new to me and it may change, but is obviously a cause for concern.

These flaws meant that these existing tools were not suitable for my needs so I rolled my own solution and have published it for general use. You can find it here:

http://www.askgeo.com/

AskGeo is based on a time zone map of the world, so it returns a valid time zone for every valid latitude and longitude. It returns the standard time zone ID (e.g., "America/Los_Angeles") used on Linux and most other operating systems and programming frameworks. It also returns the current offset, taking full account of daylight savings time.

It is extremely easy to use and usage is documented on the main page of the site. The API supports batch queries, so if you need to do a lot of look-ups, please use the batch interface rather than bog down our servers with serial requests. The bulk queries are also much faster, so everybody wins.

When we first launched this, we built it on Google App Engine (GAE) and made it free to all users. This was possible because GAE's prices were so low at that time. Since then, our server load has increased substantially and GAE's prices went way up. Both factors combined led us to switch to Amazon Web Services for hosting and to start charging for commercial use, while keeping the service free for non-profit, non-commercial open source projects, and researchers. For commercial users, we provide 1000 free queries to let potential customers evaluate the API to make sure it meets their needs. See the web site for pricing and terms.

The underlying library was written in Java and due to popular demand, we also released the library under a commercial license. Full documentation of the library and pricing details are on the web site.

I hope this is useful. It certainly was useful for the project I was working on.

Get the Current Time from Latitude and Longitude Using GeoNames API

I would be very surprised if that didn't reflect the current local time. The results for Sao Paolo (which is currently observing DST) seem to agree with that:

<geonames>
<timezone tzversion="tzdata2012f">
<countryCode>BR</countryCode>
<countryName>Brazil</countryName>
<lat>-23.0</lat>
<lng>-46.0</lng>
<timezoneId>America/Sao_Paulo</timezoneId>
<dstOffset>-3.0</dstOffset>
<gmtOffset>-2.0</gmtOffset>
<rawOffset>-3.0</rawOffset>
<time>2012-12-17 10:48</time>
<sunrise>2012-12-17 06:14</sunrise>
<sunset>2012-12-17 19:47</sunset>
</timezone>
</geonames>

However, another alternative would be to use the GeoNames API to get the time zone ID, and then use Noda Time for everything to do with that time zone. (Disclaimer: I'm the primary developer of Noda Time.)

You can't just use the BCL's TimeZoneInfo class directly, as that doesn't support TZDB the time zone IDs that GeoNames provides (e.g. "Asia/Kolkata"). You could just use a mapping (CLDR provides one) but in my experience the Windows time zone information has some disturbing oddities.

If you just need to know the current time, GeoNames is probably fine -

How to get a time zone from a location using latitude and longitude coordinates?

Time Zone Location Web Services

  • Google Maps Time Zone API
  • Bing Maps Time Zone API
  • Azure Maps Time Zone API
  • GeoNames Time Zone API
  • TimeZoneDB API
  • AskGeo - commercial (but arguably more accurate than GeoNames)
  • GeoGarage Time Zone API - commercial, focusing on Nautical time zones.

Raw Time Zone Boundary Data

  • Timezone Boundary Builder - builds time zone shapefiles from OpenStreetMaps map data. Includes territorial waters near coastlines.

The following projects have previously been sources of time zone boundary data, but are no longer actively maintained.

  • tz_world - the original shapefile data from Eric Muller
  • whereonearth-timezone - GeoJSON version with WOEDB data merged in

Time Zone Geolocation Offline Implementations

Implementations that use the Timezone Boundary Builder data

  • node-geo-tz - JavaScript library (Node.js only)
  • timespace - JavaScript library
  • tz-lookup-oss - JavaScript library
  • GeoTimeZone - .NET library
  • Geo-Timezone - PHP library
  • timezonefinder - Python library
  • ZoneDetect - C library
  • Timeshape - Java library
  • TimeZoneMap - Java and Android library
  • lutz - R library
  • go-tz - Go library
  • Timezone lookup - Go library
  • docker-timezone-lookup - docker container wrapping node-geo-tz

Implementations that use the older tz_world data

  • latlong - Go library (Read this post also.)
  • TimeZoneMapper - Java library
  • tzwhere - JavaScript/Node library
  • pytzwhere - Python library
  • timezone_finder - Ruby library
  • LatLongToTimeZone - Java and Swift libraries
  • What Time is it here? - Blog post describing PHP and MongoDB
  • rundel/timezone - R library

Libraries that call one of the web services

  • timezone - Ruby gem that calls GeoNames
  • AskGeo has its own libraries for calling from Java or .Net
  • GeoNames has client libraries for just about everything

Self-hosted web services

  • geo2tz - based on Timezone lookup, available via Docker image

Other Ideas

  • Find the nearest city with an R-Tree
  • Find the nearest city with MySQL

Please update this list if you know of any others

Also, note that the nearest-city approach may not yield the "correct" result, just an approximation.

Conversion To Windows Zones

Most of the methods listed will return an IANA time zone id. If you need to convert to a Windows time zone for use with the TimeZoneInfo class in .NET, use the TimeZoneConverter library.

Don't use zone.tab

The tz database includes a file called zone.tab. This file is primarily used to present a list of time zones for a user to pick from. It includes the latitude and longitude coordinates for the point of reference for each time zone. This allows a map to be created highlighting these points. For example, see the interactive map shown on the moment-timezone home page.

While it may be tempting to use this data to resolve the time zone from a latitude and longitude coordinates, consider that these are points - not boundaries. The best one could do would be to determine the closest point, which in many cases will not be the correct point.

Consider the following example:

                            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 can be found in zone.tab. The blue dot represents the location we are attempting to find a time zone for. 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.

Geocoding with Timezone information

The answer in this other question is a great solution to this question - Determine timezone from latitude/longitude without using web services like Geonames.org



Related Topics



Leave a reply



Submit