Mapping lat/lng's to timezones

What's the quickest & easiest way to map lat/lng's to timezones, if you don't want to be relying on a web service to do this mapping?

Use my .java file or Swift file (CocoaPod) (thanks Andrew Kirmse) available here from github:


It's a database-in-a-single-source-file.  There are changes to daylight savings times and so on many times a year, but the geographical boundaries of timezones hardly ever change. The most recent change is the Crimea peninsular.  All the data for the polygons have been compressed and stored in source code so there's no database file you need.  It's accurate to about 2km.

The provided code is available under the MIT licence.

If you need this in other languages then feel free to contribute to the open source project, by modifying the generator software.  However I can't see this technique working with an interpreted language such as PHP.


More info:

I took shapefiles from here:   http://efele.net/maps/tz/world/
 
I processed them into json according to a C library I found referenced in here:
          http://en.wikipedia.org/wiki/Shapefile

Then I created a "lat,lng to timezone approximator" Java class, see below.  This class is nice and small and quick, the data is embedded in the code as 100's of 'if' statements implementing a 2kd-tree.  Also, since there's nothing other than if's and returns, the code can be used with very little work in C#, C++, JavaScript, PHP etc., (although you'd want a compiled language!).   The 500K .java file is accurate to half a degree, 55km at worst.  The 1.1Mb .java file is accurate to 0.25 of a degree, 22km at worst.

Then I realised that 22km was still producing a lot of errors, because a lot of cities are within 22km of a timezone boundary, e.g. Gold Coast QLD, Chattanooga.  Note - within 22km of a coast mostly works due to a custom rule I created, but still suspect there are some occasional errors there.

So I developed the '2km' version which uses a variety of techniques including polygons. But this is more effort to port to other languages. 

If anyone wants the .json file of the polygons, which should be fully accurate if you can write code that makes sense of it, then email me on tim @ edval.com.au - the file is too big for Google Sites, 29Mb compressed.  But it's dated late 2013, so doesn't have the change in Crimea.


ċ
TimeZoneMapper.22km.java
(1196k)
Tim Cooper,
Feb 15, 2015, 3:11 PM
ċ
TimeZoneMapper.55km.java
(601k)
Tim Cooper,
Feb 15, 2015, 3:12 PM
ċ
TimezoneMapper.2km.zip
(409k)
Tim Cooper,
Nov 10, 2015, 3:05 PM
Comments