TimeZone from latitude/longtitude

Hi,
maybe very silly question, but can not find solution so far.
I have gps coordinates of the city, ie place in Stockholm/Sweden: [59.3725,18.0].
I would like to get TimeZone for this place: ‘Europe/Stockholm’

In python, there is lib: timezonefinder · PyPI

Please, what is the best solution to get this in Julia

Thanks a lot for help
Lubo

1 Like

Perhaps:

julia> using TimeZones

julia> TimeZone("Europe/Stockholm")
Europe/Stockholm (UTC+1/UTC+2)

(Assuming you know the “Europe/Stockholm”… I don’t know how you get the country from the Latitude/Longitude though…)

Hi, thanks for the reply, but no, the point is that I do not know that these point belongs to Europe/Stockholm zone.

Yes, I did wonder if you really meant "I would like to get TimeZone for this place: [59.3725,18.0]. :smile:You probably could use the Google Maps API or OpenStreetMap.

I don’t know this exists in Julia. Then, in this case (or for other obscure Python packages) I would use PyCall.jl, and use that Python package.

julia> using PyCall

julia> py"""
       from timezonefinder import TimezoneFinder

       tf = TimezoneFinder()
       latitude, longitude = 52.5061, 13.358
       """
/home/pharaldsson_sym/.local/lib/python3.6/site-packages/numba/core/errors.py:149: UserWarning: Insufficiently recent colorama version found. Numba requires colorama >= 0.3.9
  warnings.warn(msg)

julia> my_timezone = py"tf.timezone_at(lng=longitude, lat=latitude)"
"Europe/Berlin"

You would want to change it a bit more, e.g. to interpolate Julia variables in (and import differently, see PyCall docs, maybe use Conda):

julia> my_timezone = py"tf.timezone_at(lng=$longitude, lat=$latitude)"

and get rid of the warning, but that seems like a problem you would also need to figure out in Python anyway.

Ironically, the example from the Python docs didn’t work for me in Python3 nor 2, while it did work in Julia.

I first did (you would also need in Python):

$ pip3 install timezonefinder
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: timezonefinder in ./.local/lib/python3.6/site-packages (5.2.0)
Requirement already satisfied: numpy>=1.16 in ./.local/lib/python3.6/site-packages (from timezonefinder) (1.18.1)

I finally tried:

$ pip3 install timezonefinder[numba]

and it does something differently, and Julia keeps working but not Python, neither with pip (for Julia pip3 may be needed). I thought [numba] meant option as in use timezonefindernumba, but that’s not it so I learned something new.

If you wanted to make a pure-Julia solution it looks like the python package uses the shapefiles provided by this project:

https://github.com/evansiroky/timezone-boundary-builder

If one combines this with a point-in-polygon algorithm e.g. from here:

https://github.com/JuliaGeometry/PolygonOps.jl

this should be all you need for a Julia TimeZoneFinder.jl

8 Likes

Thank you all for answers, for the reference, there is also web API: API Access for Developers - TimeZoneDB

1 Like

I was trying to do this too, and still couldn’t find a native Julia solution, so I had a go at making one: https://github.com/tpgillam/TimeZoneFinder.jl (the implementation is pretty trivial, and primarily delgates to Meshes.jl for actual work)

The initial version will be available on the General registry after the 3 day cliff — after that there’s some performance optimisation I’ll hopefully have a chance to work on.

Please file issues liberally! :slight_smile:

5 Likes