def test_get_set_lat_lon(): """Verify the getters and setters from the c end are working """ ray = zip(np.random.random(100) * 2 * np.pi, np.random.random(100) * 2 * np.pi) for lat, lon in ray: coordinates = GeoCoord(lat, lon) coordinates.lat = lat - 5 coordinates.lon = lon - 5 assert coordinates.lat == lat - 5 assert coordinates.lon == lon - 5
def test_GeoBoundary_init_error_overflow(): """verify GeoBoundary raises when exceeding the maximum boundaries (10)""" coord_values = zip(np.random.random(11) * np.pi, np.random.random(11) * np.pi) geo_coords = [GeoCoord(lat, lon) for lat, lon in coord_values] with pytest.raises(ValueError): GeoBoundary(geo_coords)
def test_k_ring0(): lat = 0.659966917655 lon = 2 * 3.14159 - 2.1364398519396 sf = GeoCoord(lat, lon) sfhex0 = geo_to_h3(sf, 0) expectedk1 = [ H3Index(0x8029fffffffffff), H3Index(0x801dfffffffffff), H3Index(0x8013fffffffffff), H3Index(0x8027fffffffffff), H3Index(0x8049fffffffffff), H3Index(0x8051fffffffffff), H3Index(0x8037fffffffffff) ] k1, k1dist = k_ring_distances(sfhex0, 1) assert all(i.is_valid() for i in k1), "Indexes not populated" for i in range(7): in_list = 0 for j in range(7): if k1[i] == expectedk1[j]: assert k1dist[i] == (0 if k1[i] == sfhex0 else 1) in_list += 1 assert in_list == 1, "index not found in expected set"
def test_GeoBoundary_init_error_on_str(coord_values): """test GeoBoundary raises when getting input type 'str'""" geo_coords = [GeoCoord(lat, lon) for lat, lon in coord_values] geo_coords.insert(4, 'mittens') with pytest.raises(TypeError): GeoBoundary(geo_coords)
def test_to_geo(): """bc*r*centers.txt""" test_files = glob(os.path.join('tests', 'inputfiles', 'bc*centers.txt')) for file in test_files: with open(file, 'rt') as test: for line in test: h3str, latdegs, londegs = line.split() h3 = H3Index.from_string(h3str) coord = GeoCoord.from_degrees(float(latdegs), float(londegs)) assert_expected(h3, coord)
def test_k_ring_counts(): lat = 0.659966917655 lon = 2 * 3.14159 - 2.1364398519396 sf = GeoCoord(lat, lon) sfhex0 = geo_to_h3(sf, 9) for i in range(16): k, dist = k_ring_distances(sfhex0, i) assert all(h3.is_valid() for h3 in k) length = 1 for j in range(i, -1, -1): length += 6 * j assert len(k) == length
def test_to_h3(): """convert lat/lon to H3 and verify""" test_path = 'h3/tests/inputfiles' if not exists(test_path): assert exists(test_path), "Missing test files" target = join(test_path, 'rand*centers.txt') def assert_expected(h1: H3Index, g1: GeoCoord): assert isinstance(h1, H3Index) assert isinstance(g1, GeoCoord) h2 = g1.to_h3(h1.get_resolution()) assert h1 == h2, "got unexpected GeoCoord.to_h3 output" for filename in glob(target): with open(filename, 'rt') as test_file: test_values = [line.split() for line in test_file] for h3_str, lat_degrees, lon_degrees in test_values: h3 = H3Index.from_string(h3_str) lat = float(lat_degrees) lon = float(lon_degrees) coord = GeoCoord.from_degrees(lat, lon) assert_expected(h3, coord)
def test_to_boundary(): "bc*cells.txt" test_files = glob(os.path.join('tests', 'inputfiles', 'bc*cells.txt')) for file in test_files: test = open(file, 'rt') it = iter(test) while True: try: h3str = next(it) except StopIteration: test.close() break h3 = H3Index.from_string(h3str) assert next(it).startswith('{'), file coordinates = [] while True: line = next(it) if line.startswith('}'): break lat, lon = map(float, line.split()) coordinates.append(GeoCoord.from_degrees(lat, lon)) b1 = h3.to_boundary() b2 = GeoBoundary(coordinates) assert b1 == b2
def assert_expected(h1: H3Index, g1: GeoCoord): assert isinstance(h1, H3Index) assert isinstance(g1, GeoCoord) h2 = g1.to_h3(h1.get_resolution()) assert h1 == h2, "got unexpected GeoCoord.to_h3 output"
def test_GeoBoundary_raises_generator(coord_values): """verify GeoBoundary won't accept a generator""" with pytest.raises(TypeError): GeoBoundary(GeoCoord(lat, lon) for lat, lon in coord_values)
def test_GeoBoundary_raises_tuple(coord_values): """verify GeoBoundary won't accept a tuple""" geo_coords = tuple(GeoCoord(lat, lon) for lat, lon in coord_values) with pytest.raises(TypeError): GeoBoundary(geo_coords)
def test_GeoBoundary_init_error_on_float(coord_values): """test GeoBoundary raises when getting input type 'float'""" geo_coords = [5.4] + [GeoCoord(lat, lon) for lat, lon in coord_values] with pytest.raises(TypeError): GeoBoundary(geo_coords)
from h3py import GeoCoord, geo_to_h3, hex_ranges, H3Index, hex_range import pytest lat = 0.65996691765 lon = 2 * 3.14159 - 2.1364398519396 sf = GeoCoord(lat, lon) sfhex = geo_to_h3(sf, 9) k1 = [ H3Index(0x89283080ddbffff), H3Index(0x89283080c37ffff), H3Index(0x89283080c27ffff), H3Index(0x89283080d53ffff), H3Index(0x89283080dcfffff), H3Index(0x89283080dc3ffff) ] def test_identity_kring(): k0 = hex_ranges([sfhex], 0) assert k0[0] == sfhex, "failed to generate identity k-ring" k0 = hex_range(sfhex, 0) assert k0[0] == sfhex def test_ring1of1(): allkrings = hex_ranges(k1, 1) for i, kring in enumerate(allkrings): assert kring, "failed to create Kring"
import pytest from h3py import GeoCoord, H3Index, hex_ring, set_h3_index lat_rad, lon_rad = 0.659966917655, 2 * 3.14159 - 2.1364398519396 sf = GeoCoord(lat_rad, lon_rad) sfhex = sf.to_h3(9) def test_ring1(): expectedk1 = [ H3Index(0x89283080ddbffff), H3Index(0x89283080c37ffff), H3Index(0x89283080c27ffff), H3Index(0x89283080d53ffff), H3Index(0x89283080dcfffff), H3Index(0x89283080dc3ffff) ] k1 = hex_ring(sfhex, 1) for i in range(6): assert k1[i] != 0, "index is not populated" in_list = 0 for j in range(6): if k1[i] == expectedk1[j]: in_list += 1 assert in_list == 1, "index not found in expected set"
def tag(row, res): h3 = GeoCoord.from_degrees(row['lat'], row['lon']).to_h3(res) row['h3'] = int(h3) return row