Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
np.array(trans_points, dtype=DTYPE_FORMAT_F_NUMPY))
# print(km2deg(distance_exact))
assert km2deg(distance_exact) == 0.5
# print('=====')
distance = distance_to_polygon(x_rad, y_rad, len(x_coords), np.array(points, dtype=DTYPE_FORMAT_SIGNED_I_NUMPY))
# print(km2deg(distance))
assert abs(km2deg(distance) - sqrt(2) / 2) < 0.00001
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,
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
for hole_coordinates in self._holes_of_line(polygon_nr):
if inside_polygon(x, y, hole_coordinates):
outside_all_holes = False
break
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)
# x = longitude y = latitude both converted to 8byte int
x = coord2int(lng)
y = coord2int(lat)
# check until the point is included in one of the possible polygons
for i in range(nr_possible_polygons):
# when including the current polygon only polygons from the same zone remain,
same_element = all_the_same(pointer=i, length=nr_possible_polygons, id_list=ids)
if same_element != -1:
# return the name of that zone
return timezone_names[same_element]
polygon_nr = possible_polygons[i]
# get the boundaries of the polygon = (lng_max, lng_min, lat_max, lat_min)
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 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)
# x = longitude y = latitude both converted to 8byte int
x = coord2int(lng)
y = coord2int(lat)
# check until the point is included in one of the possible polygons
for i in range(nr_possible_polygons):
# when including the current polygon only polygons from the same zone remain,
same_element = all_the_same(pointer=i, length=nr_possible_polygons, id_list=ids)
if same_element != -1:
# return the name of that zone
return timezone_names[same_element]
polygon_nr = possible_polygons[i]
# get the boundaries of the polygon = (lng_max, lng_min, lat_max, lat_min)
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)
# only run the expensive algorithm if the point is withing the boundaries
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
for hole_coordinates in self._holes_of_line(polygon_nr):
if inside_polygon(x, y, hole_coordinates):
outside_all_holes = False
break