Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class HelperTestNumba(HelperTest):
# all Numba compiled functions have to receive their arguments in the proper data type (and numpy array shape)
try:
import timezonefinder.helpers_numba as helpers
fct_dict = {
"all_the_same": helpers.all_the_same,
"coord2int": helpers.coord2int,
"int2coord": helpers.int2coord,
"distance_to_point_on_equator": helpers.distance_to_point_on_equator,
"distance_to_polygon": helpers.distance_to_polygon,
"distance_to_polygon_exact": helpers.distance_to_polygon_exact,
"haversine": helpers.haversine,
"inside_polygon": helpers.inside_polygon,
"coord2shortcut": helpers.coord2shortcut,
"rectify_coordinates": helpers.rectify_coordinates,
'convert2coords': helpers.convert2coords,
'convert2coord_pairs': helpers.convert2coord_pairs,
}
print('\nNumba installation found.\ntesting helpers_numba.py functions...')
except ImportError:
fct_dict = {
"all_the_same": None,
"coord2int": None,
"int2coord": None,
"distance_to_point_on_equator": None,
"distance_to_polygon": None,
"distance_to_polygon_exact": None,
"haversine": None,
"inside_polygon": None,
def exact_routine(polygon_nr):
coords = self.coords_of(polygon_nr)
nr_points = len(coords[0])
empty_array = empty([2, nr_points], dtype=DTYPE_FORMAT_F_NUMPY)
return distance_to_polygon_exact(lng, lat, nr_points, coords, empty_array)
def normal_routine(polygon_nr):
coords = self.coords_of(polygon_nr)
nr_points = len(coords[0])
return distance_to_polygon(lng, lat, nr_points, coords)
lng, lat = rectify_coordinates(lng, lat)
# transform point X into cartesian coordinates
current_closest_id = None
central_x_shortcut, central_y_shortcut = coord2shortcut(lng, lat)
lng = radians(lng)
lat = radians(lat)
possible_polygons = []
# there are 2 shortcuts per 1 degree lat, so to cover 1 degree two shortcuts (rows) have to be checked
# the highest shortcut is 0
top = max(central_y_shortcut - NR_SHORTCUTS_PER_LAT * delta_degree, 0)
# the lowest shortcut is 359 (= 2 shortcuts per 1 degree lat)
bottom = min(central_y_shortcut + NR_SHORTCUTS_PER_LAT * delta_degree, 359)
# the most left shortcut is 0
left = max(central_x_shortcut - NR_SHORTCUTS_PER_LNG * delta_degree, 0)
# the most right shortcut is 359 (= 1 shortcuts per 1 degree lng)
right = min(central_x_shortcut + NR_SHORTCUTS_PER_LAT * delta_degree, 359)
def certain_timezone_at(self, *, lng, lat):
"""
this function looks up in which polygon the point certainly is included
this is much slower than 'timezone_at'!
:param lng: longitude of the point in degree
:param lat: latitude in degree
:return: the timezone name of the polygon the point is included in or None
"""
lng, lat = rectify_coordinates(lng, lat)
shortcut_id_x, shortcut_id_y = coord2shortcut(lng, lat)
possible_polygons = self.polygon_ids_of_shortcut(shortcut_id_x, shortcut_id_y)
# x = longitude y = latitude both converted to 8byte int
x = coord2int(lng)
y = coord2int(lat)
# check if the point is actually included in one of the polygons
for polygon_nr in possible_polygons:
# get boundaries
self.poly_max_values.seek(4 * NR_BYTES_I * polygon_nr)
boundaries = self.fromfile(self.poly_max_values, dtype=DTYPE_FORMAT_SIGNED_I_NUMPY, count=4)
if not (x > boundaries[0] or x < boundaries[1] or y > boundaries[2] or y < boundaries[3]):
outside_all_holes = True
# when the point is within a hole of the polygon this timezone doesn't need to be checked
def timezone_at(self, *, lng, lat):
"""
this function looks up in which polygons the point could be included in
to speed things up there are shortcuts being used (stored in a binary file)
especially for large polygons it is expensive to check if a point is really included,
so certain simplifications are made and even when you get a hit the point might actually
not be inside the polygon (for example when there is only one timezone nearby)
if you want to make sure a point is really inside a timezone use 'certain_timezone_at'
:param lng: longitude of the point in degree (-180 to 180)
:param lat: latitude in degree (90 to -90)
:return: the timezone name of a matching polygon or None
"""
lng, lat = rectify_coordinates(lng, lat)
shortcut_id_x, shortcut_id_y = coord2shortcut(lng, lat)
self.shortcuts_unique_id.seek(
(180 * NR_SHORTCUTS_PER_LAT * NR_BYTES_H * shortcut_id_x + NR_BYTES_H * shortcut_id_y))
try:
# if there is just one possible zone in this shortcut instantly return its name
return timezone_names[unpack(DTYPE_FORMAT_H, self.shortcuts_unique_id.read(NR_BYTES_H))[0]]
except IndexError:
possible_polygons = self.polygon_ids_of_shortcut(shortcut_id_x, shortcut_id_y)
nr_possible_polygons = len(possible_polygons)
if nr_possible_polygons == 0:
return None
if nr_possible_polygons == 1:
# there is only one polygon in that area. return its timezone name without further checks
return timezone_names[self.id_of(possible_polygons[0])]
# create a list of all the timezone ids of all possible polygons
ids = self.id_list(possible_polygons, nr_possible_polygons)