def test_many_hex_ranges(self): hex_ranges = h3.hex_ranges(list(h3.k_ring('8928308280fffff', 2)), 2) self.assertEqual(19, len(list(hex_ranges.keys()))) hexagons = hex_ranges['8928308280fffff'] self.assertEqual(3, len(hexagons)) self.assertEqual(1, len(hexagons[0])) self.assertEqual(6, len(hexagons[1])) self.assertEqual(12, len(hexagons[2])) hex_ranges_even_larger = h3.hex_ranges( list(h3.k_ring('8928308280fffff', 5)), 5) self.assertEqual(91, len(list(hex_ranges_even_larger.keys()))) hexagons_larger = hex_ranges_even_larger['8928308280fffff'] self.assertEqual(6, len(hexagons_larger)) self.assertEqual(1, len(hexagons_larger[0])) self.assertEqual(6, len(hexagons_larger[1])) self.assertEqual(12, len(hexagons_larger[2])) self.assertEqual(18, len(hexagons_larger[3])) self.assertEqual(24, len(hexagons_larger[4])) self.assertEqual(30, len(hexagons_larger[5]))
def test_hex_ring2(self): hexagons = h3.hex_ring('8928308280fffff', 2) self.assertEqual( 12, len(hexagons), 'got the expected number of hexagons for the second ring') expected_hexagons = [ '89283082813ffff', '89283082817ffff', '8928308281bffff', '89283082863ffff', '89283082823ffff', '8928308287bffff', '89283082833ffff', '8928308282bffff', '89283082857ffff', '892830828abffff', '89283082847ffff', '89283082867ffff', ] for hexagon in expected_hexagons: self.assertIn(hexagon, hexagons, 'found an expected hexagon') self.assertEqual( hexagons, h3.k_ring('8928308280fffff', 2) - h3.k_ring('8928308280fffff', 1), 'the fast and slow hex ring paths match')
def test_k_ring2(self): hexagons = h3.k_ring('8928308280fffff', 2) self.assertEqual(1 + 6 + 12, len(hexagons), 'got the expected number of hexagons for two rings') expected_hexagons = [ '89283082813ffff', '89283082817ffff', '8928308281bffff', '89283082863ffff', '89283082823ffff', '89283082873ffff', '89283082877ffff', '8928308287bffff', '89283082833ffff', '8928308282bffff', '8928308283bffff', '89283082857ffff', '892830828abffff', '89283082847ffff', '89283082867ffff', '89283082803ffff', '89283082807ffff', '8928308280bffff', '8928308280fffff', ] for hexagon in expected_hexagons: self.assertIn(hexagon, hexagons, 'found an expected hexagon')
def test_h3_set_to_multi_polygon_2k_ring(self): # 2-ring in order returned by algo h3_addresses = h3.k_ring('8930062838bffff', 2) multi_polygon = h3.h3_set_to_multi_polygon(h3_addresses) self.assertEqual( len(multi_polygon), 1, 'polygon count matches expected' ) self.assertEqual( len(multi_polygon[0]), 1, 'loop count matches expected' ) self.assertEqual( len(multi_polygon[0][0]), 6 * (2 * 2 + 1), 'coord count matches expected' ) # Same k-ring in random order h3_addresses = [ '89300628393ffff', '89300628383ffff', '89300628397ffff', '89300628067ffff', '89300628387ffff', '893006283bbffff', '89300628313ffff', '893006283cfffff', '89300628303ffff', '89300628317ffff', '8930062839bffff', '8930062838bffff', '8930062806fffff', '8930062838fffff', '893006283d3ffff', '893006283c3ffff', '8930062831bffff', '893006283d7ffff', '893006283c7ffff' ] multi_polygon = h3.h3_set_to_multi_polygon(h3_addresses) self.assertEqual( len(multi_polygon), 1, 'polygon count matches expected' ) self.assertEqual( len(multi_polygon[0]), 1, 'loop count matches expected' ) self.assertEqual( len(multi_polygon[0][0]), 6 * (2 * 2 + 1), 'coord count matches expected' ) h3_addresses = list(h3.k_ring('8930062838bffff', 6)) h3_addresses.sort() multi_polygon = h3.h3_set_to_multi_polygon(h3_addresses) self.assertEqual( len(multi_polygon[0]), 1, 'loop count matches expected' )
def test_hex_ring(self): hexagons = h3.hex_ring('8928308280fffff', 1) self.assertEqual( 6, len(hexagons), 'got the expected number of hexagons for a single ring') expected_hexagons = [ '8928308280bffff', '89283082807ffff', '89283082877ffff', '89283082803ffff', '89283082873ffff', '8928308283bffff', ] for hexagon in expected_hexagons: self.assertIn(hexagon, hexagons, 'found an expected hexagon') self.assertEqual( hexagons, h3.k_ring('8928308280fffff', 1) - h3.k_ring('8928308280fffff', 0), 'the fast and slow hex ring paths match')
def kring_smoothing(df, hex_col, metric_col, k): dfk = df[[hex_col]] dfk.index = dfk[hex_col] dfs = (dfk[hex_col].apply(lambda x: pd.Series(list(h3.k_ring(x, k)))). stack().to_frame('hexk').reset_index( 1, drop=True).reset_index().merge(df[[ hex_col, metric_col ]]).fillna(0).groupby(['hexk'])[[metric_col]].sum().divide( (1 + 3 * k * (k + 1))).reset_index().rename( index=str, columns={"hexk": hex_col})) dfs['lat'] = dfs[hex_col].apply(lambda x: h3.h3_to_geo(x)[0]) dfs['lng'] = dfs[hex_col].apply(lambda x: h3.h3_to_geo(x)[1]) return dfs
def test_k_ring_pentagon(self): hexagons = h3.k_ring('821c07fffffffff', 1) self.assertEqual( 1 + 5, len(hexagons), 'got the expected number for a single ring around a pentagon') expected_hexagons = [ '821c2ffffffffff', '821c27fffffffff', '821c07fffffffff', '821c17fffffffff', '821c1ffffffffff', '821c37fffffffff', ] for hexagon in expected_hexagons: self.assertIn(hexagon, hexagons, 'found an expected hexagon')
def test_k_ring(self): hexagons = h3.k_ring('8928308280fffff', 1) self.assertEqual( 1 + 6, len(hexagons), 'got the expected number of hexagons for a single ring') expected_hexagons = [ '8928308280fffff', '8928308280bffff', '89283082807ffff', '89283082877ffff', '89283082803ffff', '89283082873ffff', '8928308283bffff', ] for hexagon in expected_hexagons: self.assertIn(hexagon, hexagons, 'found an expected hexagon')
def k_ring(self, hex_id: str, ring_size: int) -> set: """return indices for all hexagons within the range of `ring_size` hexagon from hex_id """ return h3.k_ring(hex_id, ring_size)