Giter Club home page Giter Club logo

pytzwhere's Introduction

pytzwhere Build Status Coverage Status

pytzwhere is a Python library to lookup the timezone for a given lat/lng entirely offline.

Version 3.0 fixes how pytzwhere deals with holes in timezones. It is recommended that you use version 3.0.

It is a port from https://github.com/mattbornski/tzwhere with a few improvements. The underlying timezone data is based on work done by Eric Muller

If used as a library, basic usage is as follows:

>>> from tzwhere import tzwhere
>>> tz = tzwhere.tzwhere()
>>> print tz.tzNameAt(35.29, -89.66)
America/Chicago

The polygons used for building the timezones are based on VMAP0. Sometimes points are outside a VMAP0 polygon, but are clearly within a certain timezone (see also this discussion). As a solution you can search for the closest timezone within a user defined radius.

Dependencies:

  • numpy (optional)

  • shapely

forceTZ

If the coordinates provided are outside of the currently defined timezone boundaries, the tzwhere function will return None. If you would like to match to the closest timezone, use the forceTZ parameter.

Example:

>>> from tzwhere import tzwhere
>>> tz = tzwhere.tzwhere()
>>> print(tz.tzNameAt(53.68193999999999, -6.239169999999998))
None

>>> from tzwhere import tzwhere
>>> tz = tzwhere.tzwhere(forceTZ=True)
>>> print(tz.tzNameAt(53.68193999999999, -6.239169999999998, forceTZ=True))
Europe/Dublin

pytzwhere's People

Contributors

bernardotorres avatar cstich avatar f0rk avatar famanson avatar gimbo avatar jacinda avatar kmohaia-datalicious avatar ksimka avatar linwoodc3 avatar pegler avatar scelfo avatar seahawk1986 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytzwhere's Issues

Why can't one read and write both csv's?

I see in the help that writing to csv isn't allowed if reading from one (when using as a script). What is the reason for this? This is exactly what I was hoping to do. :)

I think I could facilitate this on my own by writing a python script to read my data first, use tzwhere internally, and then writing a csv. But I'm guessing that I'm missing something here otherwise you'd have implemented this.

package_data are not installed when installing pytzwhere with pip in a venv

Hello,
when installing tzwhere in a venv with pip using Python 3.6 (or 3.5) both json files 'tz_world.json' and 'tz_world_shortcuts.json' are missing:

$ python3 -m venv test_pytzwhere
$ source test_pytzwhere/bin/activate
(test_pytzwhere) $ pip install tzwhere
(test_pytzwhere) $ tree test_pytzwhere/lib/python3.6/site-packages/tzwhere*
test_pytzwhere/lib/python3.6/site-packages/tzwhere
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-36.pyc
│   └── tzwhere.cpython-36.pyc
└── tzwhere.py
test_pytzwhere/lib/python3.6/site-packages/tzwhere-3.0.1-py3.6.egg-info
├── PKG-INFO
├── SOURCES.txt
├── dependency_links.txt
├── installed-files.txt
├── requires.txt
└── top_level.txt

Removing the Line "include_package_data=True," from the setup.py (https://github.com/pegler/pytzwhere/blob/master/setup.py#L19) seems to fix the problem - is this needed by anything else?

Polygon inaccuracy

I found out that, in some regions, the polygons are kind of inaccurate. In my case, I was testing with some points along Spain's southern coast and eventually they got outside the polygon. When I plotted the points and the polygon, I saw a 1km-wide land strip outside the polygon, where my point was located.

I don't know if its a bug or feature, but I think it's necessary to warn this package users that it may happen.

One suggestion I can make is adding actual "timezone polygons" and use them as fallback cases.

Optional dependencies

I was wondering whether it would make sense to make the optional dependencies, required dependencies and in that way clean up our code base. Should we maybe with the release of version 3 make numpy and shapely required dependencies? Any thoughts/comments/ideas?

all of China should be +0800, but some are showing up as +0600

For example if I check Sichuan, China I get Asia/Urumqi at +0600, not China at +0800

from tzwhere import tzwhere
from datetime import datetime
import pytz

TZ = tzwhere.tzwhere(shapely=True, forceTZ=True)
JAN1 = datetime(2016,1,1)
JUN1 = datetime(2016,6,1)

# Sichuan, pvsim ID #1990489
tz.tzNameAt(29.45, 97.15)
pytz.timezone(tz.tzNameAt(29.45, 97.15))
sichuan = pytz.timezone(tz.tzNameAt(29.45, 97.15))
print sichuan
# Asia/Urumqi  <- this is the timezone

# method 1: strftime('%z'), this is used in HAL_db_migration
jan1 = sichuan.localize(JAN1)
tz_str = jan1.strftime('%z')
print tz_str
# '+0600'

see also mattbornski21

forceTZ returning unexpected result

I've been using forceTZ=True to deal with coastal points. Thanks for adding this feature. Seems to be working well except for these rare points in Vancouver.

>>> from tzwhere import tzwhere
>>> tz = tzwhere.tzwhere(shapely=True, forceTZ=True)
2016-06-14 23:34:20,283 tzwhere.py _read_polygons_from_csv 288 INFO  Reading from CSV input file: /home/ec2-user/.local/lib/python2.7/site-packages/tzwhere/tz_world.csv

>>> tz.tzNameAt(49.2698, -123.1302)
>>> tz.tzNameAt(49.2698, -123.1302, forceTZ=True)
'America/Los_Angeles'

It should return 'America/Vancouver' as that's the closest polygon by far. Am I missing something?

ModuleNotFoundError: No module named 'shapely.lib'

I faced this error when initialize tzwhere:

Traceback (most recent call last):
  File "C:\Users\pizhlo21\Desktop\Folder\python\tg_bot_reminder\controller.py", line 15, in <module>
    import records
  File "C:\Users\pizhlo21\Desktop\Folder\python\tg_bot_reminder\records\__init__.py", line 3, in <module>
    from .main import get_records, delete_record, make_records_handlers, make_record_text, check_records
  File "C:\Users\pizhlo21\Desktop\Folder\python\tg_bot_reminder\records\main.py", line 10, in <module>
    from scheduler import make_cron, make_interval, make_date
  File "C:\Users\pizhlo21\Desktop\Folder\python\tg_bot_reminder\scheduler\__init__.py", line 1, in <module>
    from .main import make_cron, make_interval, make_date
  File "C:\Users\pizhlo21\Desktop\Folder\python\tg_bot_reminder\scheduler\main.py", line 8, in <module>
    from users import get_timezone
  File "C:\Users\pizhlo21\Desktop\Folder\python\tg_bot_reminder\users\__init__.py", line 3, in <module>
    from .location import set_timezone, get_timezone
  File "C:\Users\pizhlo21\Desktop\Folder\python\tg_bot_reminder\users\location.py", line 4, in <module>
    from tzwhere import tzwhere
  File "C:\Users\pizhlo21\Desktop\Folder\python\tg_bot_reminder\venv\lib\site-packages\tzwhere\tzwhere.py", line 20, in <module>
    import shapely.geometry as geometry
  File "C:\Users\pizhlo21\Desktop\Folder\python\tg_bot_reminder\venv\lib\site-packages\shapely\__init__.py", line 17, in <module>
    from .lib import GEOSException  # NOQA
ModuleNotFoundError: No module named 'shapely.lib'

My python version:

(venv) C:\Users\pizhlo21\Desktop\Folder\python\tg_bot_reminder>python 
Python 3.10.10 (tags/v3.10.10:aad5f6a, Feb  7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Does anybody know how to solve it?

Upload 2.3 to pypi?

Looks like 2.3 hasn't made it to pypi yet. Any chance that could be done so I can avoid fixing my logging configs? :)

No timezone for Dubai

Hi there,
I am trying to retrieve the timezone for coordinates in Dubai (25.193, 55.309) using these lines of code:

from tzwhere import tzwhere
tz = tzwhere.tzwhere()
tzName = tz.tzNameAt(25.193, 55.309)

Unfortunately None is being returned to me but the correct timezone would be "Asia/Dubai".

Is someone else able to reproduce this behavior?
Thanks!

Splitting database into smaller chunks

I'm trying to run pytzwhere on raspberry model A and it runs out of memory. Would it be possible to split the database file into smaller chunks?

P.S I just realised the json file is a plain text file, so even manual splitting should be possible.

Constructor fails with value error

---> 13 tz1 = tzwhere.tzwhere(forceTZ=True)

File ~\Miniconda3\envs\blah\Lib\site-packages\tzwhere\tzwhere.py:62, in tzwhere.init(self, forceTZ)
60 self.timezoneNamesToPolygons[tzname].append(poly)
61 for tzname, polys in self.timezoneNamesToPolygons.items():
---> 62 self.timezoneNamesToPolygons[tzname] = WRAP(polys)
64 if forceTZ:
65 self.unprepTimezoneNamesToPolygons[tzname] = WRAP(polys)

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (1, 2) + inhomogeneous part.

PyPI package out of date

The tzwhere PyPI package is over a year old and is not Python 3 compatible. Could a new release be made with the current updates?

VisibleDeprecationWarning

miniforge3/envs/a50-dev/lib/python3.9/site-packages/tzwhere/tzwhere.py:65: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.self.unprepTimezoneNamesToPolygons[tzname] = WRAP(polys)

Change import path

Presumably, tzNameAt is the business end of this module. In order to use it, it must be imported and invoked this way:

from tzwhere import tzwhere
tzwhere.tzwhere.tzNameAt(...)

Or:

from tzwhere.tzwhere import *
tzwhere.tzNameAt(...)

Any thoughts on removing the extraneous tzwhere parts? e.g.

import tzwhere
tzwhere.tzNameAt(...)

Potentially wrong results returned

I wrote a package using the same data and compared my results to the results of this package for aprox. 5M points. Those are a few of the points where I got mismatches:

(lng, lat), result timezonefinder, result pytzwhere
(-110.58195555806134, 35.53178795327783), 'America/Phoenix', 'America/Denver']
(-110.41118747093144, 35.76969036563554), 'America/Phoenix', 'America/Denver']
(-110.69776990691068, 35.59283347945522), 'America/Phoenix', 'America/Denver']
(-110.91327628315915, 36.11131540134208), 'America/Phoenix', 'America/Denver']
(-110.26814562443687, 35.98507252905026), 'America/Phoenix', 'America/Denver']
(-133.73396065378114, 68.38068073677294), 'America/Inuvik', 'America/Yellowknife']

These might be errors in my algorithm, but I still though you might want to check this out.

This is my package:
https://github.com/MrMinimal64/timezonefinder

What happened to tzOffsetAt?

If I write:

tz = tzwhere.tzwhere()
tzo = tz.tzOffsetAt(lat, lng)

I get:

AttributeError: 'tzwhere' object has no attribute 'tzOffsetAt'

The original tzwhere had the offset parameter. Did it not make it to this version?

Some coordinates return timezone None

I have a large cities database with coordinates (a single latitude,longitude tuple for each), and pytzwhere is not returning a timezone for almost half of them.

Some examples are:

>>> w.tzNameAt(-11.898345, 135.6838)
>>> w.tzNameAt(58.194371, -136.344993)
>>> w.tzNameAt(33.617694, 130.404308)
>>> w.tzNameAt(44.555141, 38.062928)
>>> w.tzNameAt(-28.770775, 114.59602)

can't install via pip

While I can search for tzwhere:

> pip search tzwhere
tzwhere                   - Python library to look up timezone from lat / long
                            offline

I can't install it.

> pip install tzwhere

Downloading/unpacking tzwhere
  Could not find any downloads that satisfy the requirement tzwhere

Perhaps because there is no link to the 1.0 tarball on pypi?

Is this project still maintained?

Hi,

The latest commit was 6 years ago(2018) 32d2bef and no PR got merged after that. Will this project get maintained by anyone?

I suggest use alternatives:

  1. timezonefinder
  2. tzfpy(I'm the maintainer)

Timezonefinder has a comprehensive doc for compares between packages, you may view it here.

argparse in the wrong place

argparse in pytzwhere.py is not guarded by if name == "main";, as a consequence argparse interferes when pytzwhere imported into a program that also imports and tries to use argparse.

This is easy to correct. Just wondering if this repository/project is still alive.

ValueError due to shapely update

shapely recently updated with a breaking change. I think this occured with version 2.0.0 of shapely

tz = tzwhere.tzwhere()
File "/usr/local/lib/python3.10/site-packages/tzwhere/tzwhere.py", line 62, in __init__
self.timezoneNamesToPolygons[tzname] = WRAP(polys)
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (1, 2) + inhomogeneous part.

With latest numpy update, generates an error on initialization

Using numpy 1.19.2 (I don't believe this occurred with 1.17.*), tzwhere 3.0.3, on initialization get the following error:

/usr/local/lib/python3.8/dist-packages/numpy/core/_asarray.py:83: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
return array(a, dtype, copy=False, order=order)

This obviously happens on the "WRAP(polys)" calls on lines 62 and 65.

If I comment out the "import numpy" section and declare WRAP and COLLECTION_TYPE as tuples, then quite obviously this error goes away.

tz_world.json.gz does not exits

Immediately after a fresh pip2 install I do:

from tzwhere import tzwhere
tzwhere.tzwhere()

I get the error (subset below):

/opt/conda/envs/python2/lib/python2.7/gzip.pyc in __init__(self, filename, mode, compresslevel, fileobj, mtime)
     92             mode += 'b'
     93         if fileobj is None:
---> 94             fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
     95         if filename is None:
     96             # Issue #13781: os.fdopen() creates a fileobj with a bogus name

IOError: [Errno 2] No such file or directory: '/opt/conda/envs/python2/lib/python2.7/site-packages/tzwhere/tz_world.json.gz'

It looks like gzip is trying to locate tz_world.json.gz. It does not exist inside the tzwhere folder, which includes only:

tzwhere.py                                                 
tzwhere.pyc                                                
tz_world_shortcuts.json

If I manually add the file to the directory it works. So it just seems that it is something in the pip installation that goes wrong.

Pickle the shapely polygons

If we pickle the shapely polygons, then we could speed up the import probably quite considerably. Potential downside: This might not work if you have a shapely/python mismatch somewhere.

Remove unnecessary data files

currently the package has a lot of data files that aren't necessary. The most compact one should be used and the rest deleted. It will most likely be the pickle version. If so, a utility command to parse a geojson file and write a pickle file should be added

Outdated data

Not sure which version of tz_world you are using at the moment, but I just wanted to let you now that tz_world is not being maintained any more (and there were lots of changes since may 2016!).
As stated on http://efele.net/maps/tz/world/ there now is another alternative. This new one works for timezonefinder (but data is different as in tz_world!).

Add tests to PyPI source tarball?

Could you include tests in the source tarball in PyPI? I think it's typically considered good practice:

Many significant Python packages (e.g., numpy, scipy, pandas) are including the tests in PyPI source tarball. This makes it possible to use the official PyPI release and run tests to check that everything is working.

Alternatively, I can use GitHub, but then it'd be nice if the PyPI releases were tagged on GitHub so I know which commit is which release exactly.

I'm packaging pytzwhere to a Linux distro and as part of the building process, the unit tests are run, that's why I'd like to have the tests available while using the official release from PyPI.

can't insatll pytzwhere on Win 10

Did you check installation on Win 10? Python 3.6 (32 or 64 - tried both)

use as usial "pip install tzwhere" - and get error:

C:\Users\Comp02>pip install tzwhere
Collecting tzwhere
Using cached tzwhere-3.0.3.tar.gz
Collecting shapely (from tzwhere)
Using cached Shapely-1.6.4.post1.tar.gz
Exception:
Traceback (most recent call last):
File "c:\users\comp02\appdata\local\programs\python\python36\lib\site-packages\pip\compat_init_.py", line 73, in console_to_str
return s.decode(sys.stdout.encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcd in position 24: invalid continuation byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "c:\users\comp02\appdata\local\programs\python\python36\lib\site-packages\pip\basecommand.py", line 215, in main
status = self.run(options, args)
File "c:\users\comp02\appdata\local\programs\python\python36\lib\site-packages\pip\commands\install.py", line 324, in run
requirement_set.prepare_files(finder)
File "c:\users\comp02\appdata\local\programs\python\python36\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "c:\users\comp02\appdata\local\programs\python\python36\lib\site-packages\pip\req\req_set.py", line 634, in prepare_file
abstract_dist.prep_for_dist()
File "c:\users\comp02\appdata\local\programs\python\python36\lib\site-packages\pip\req\req_set.py", line 129, in prep_for_dist
self.req_to_install.run_egg_info()
File "c:\users\comp02\appdata\local\programs\python\python36\lib\site-packages\pip\req\req_install.py", line 439, in run_egg_info
command_desc='python setup.py egg_info')
File "c:\users\comp02\appdata\local\programs\python\python36\lib\site-packages\pip\utils_init
.py", line 676, in call_subprocess
line = console_to_str(proc.stdout.readline())
File "c:\users\comp02\appdata\local\programs\python\python36\lib\site-packages\pip\compat_init_.py", line 75, in console_to_str
return s.decode('utf_8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcd in position 24: invalid continuation byte

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.