Skip to content

Commit

Permalink
Merge branch 'master' of github.com:esplorio/pytzwhere
Browse files Browse the repository at this point in the history
* 'master' of github.com:esplorio/pytzwhere:
  Fix tests
  Make numpy optional again
  Add delta_degrees
  Add delta_degrees to tests
  • Loading branch information
Son Pham committed Mar 5, 2019
2 parents 67e228a + 5abbbeb commit e9a39ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
24 changes: 19 additions & 5 deletions tests/test_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,37 @@ class LocationTestCase(unittest.TestCase):
( 50.26, -9.051, 'Far off Cornwall', None)
)

def _test_tzwhere(self, locations, forceTZ):
TEST_LOCATIONS_FORCETZ_DELTA_DEGREES = (
( 35.295953, -89.662186, 'Arlington, TN', 'America/Chicago'),
( 33.58, -85.85, 'Memphis, TN', 'America/Chicago'),
( 61.17, -150.02, 'Anchorage, AK', 'America/Anchorage'),
( 40.7271, -73.98, 'Shore Lake Michigan', 'America/New_York'),
( 50.1536, -8.051, 'Off Cornwall', 'Europe/London'),
( 49.2698, -123.1302, 'Vancouver', None),
)

def _test_tzwhere(self, locations, forceTZ, tz_service=None, delta_degrees=None):
start = datetime.datetime.now()
w = tzwhere.tzwhere(forceTZ=forceTZ)
if not tz_service:
tz_service = tzwhere.tzwhere(forceTZ=forceTZ)
end = datetime.datetime.now()
print('Initialized in: '),
print(end - start)

template = '{0:20s} | {1:20s} | {2:20s} | {3:2s}'
print(template.format('LOCATION', 'EXPECTED', 'COMPUTED', '=='))
for (lat, lon, loc, expected) in locations:
computed = w.tzNameAt(float(lat), float(lon), forceTZ=forceTZ)
computed = tz_service.tzNameAt(float(lat), float(lon), forceTZ=forceTZ, delta_degrees=delta_degrees)
ok = 'OK' if computed == expected else 'XX'
print(template.format(loc, str(expected), str(computed), ok))
assert computed == expected

def test_lookup(self):
self._test_tzwhere(self.TEST_LOCATIONS,forceTZ=False)
self._test_tzwhere(self.TEST_LOCATIONS, forceTZ=False)

def test_forceTZ(self):
self._test_tzwhere(self.TEST_LOCATIONS_FORCETZ,forceTZ=True)
tz_service = tzwhere.tzwhere(forceTZ=True)
self._test_tzwhere(self.TEST_LOCATIONS, forceTZ=True, tz_service=tz_service)
self._test_tzwhere(self.TEST_LOCATIONS_FORCETZ, forceTZ=True, tz_service=tz_service)
self._test_tzwhere(self.TEST_LOCATIONS_FORCETZ, forceTZ=True, tz_service=tz_service, delta_degrees=1)
self._test_tzwhere(self.TEST_LOCATIONS_FORCETZ_DELTA_DEGREES, forceTZ=True, tz_service=tz_service, delta_degrees=0.001)
11 changes: 7 additions & 4 deletions tzwhere/tzwhere.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self, forceTZ=False):
self.timezoneLongitudeShortcuts[degree][tzname] = \
tuple(self.timezoneLongitudeShortcuts[degree][tzname])

def tzNameAt(self, latitude, longitude, forceTZ=False):
def tzNameAt(self, latitude, longitude, forceTZ=False, delta_degrees=None):
'''
Let's you lookup for a given latitude and longitude the appropriate
timezone.
Expand All @@ -82,6 +82,8 @@ def tzNameAt(self, latitude, longitude, forceTZ=False):
@forceTZ: If forceTZ is true and you can't find a valid timezone return
the closest timezone you can find instead. Only works if the point has
the same integer value for its degree than the timezeone
@delta_degrees: If forceTZ is true, this value is used to limit the
search radius to a preset value in degrees
'''

if forceTZ:
Expand Down Expand Up @@ -120,10 +122,10 @@ def tzNameAt(self, latitude, longitude, forceTZ=False):

if forceTZ:
return self.__forceTZ__(possibleTimezones, latTzOptions,
lngTzOptions, queryPoint)
lngTzOptions, queryPoint, delta_degrees)

def __forceTZ__(self, possibleTimezones, latTzOptions,
lngTzOptions, queryPoint):
lngTzOptions, queryPoint, delta_degrees):
distances = []
if possibleTimezones:
if len(possibleTimezones) == 1:
Expand All @@ -141,7 +143,8 @@ def __forceTZ__(self, possibleTimezones, latTzOptions,
poly = self.unprepTimezoneNamesToPolygons[
tzname][polyIndex]
d = poly.distance(queryPoint)
distances.append((d, tzname))
if not delta_degrees or d <= delta_degrees:
distances.append((d, tzname))
if len(distances) > 0:
return sorted(distances, key=lambda x: x[0])[0][1]

Expand Down

0 comments on commit e9a39ea

Please sign in to comment.