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_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 tag(row, res): h3 = GeoCoord.from_degrees(row['lat'], row['lon']).to_h3(res) row['h3'] = int(h3) return row