<jupyter_start><jupyter_text>An attempt to generate a seamless H3 hex grid from a geographic extent coordinate pairs.Adapted from https://github.com/uber/h3-py-notebooks/blob/master/notebooks/usage.ipynb<jupyter_code>from h3 import h3
import folium

def visualize_hexagons(hexagons, color="red", folium_map=None):
    """
    hexagons is a list of hexcluster. Each hexcluster is a list of hexagons. 
    eg. [[hex1, hex2], [hex3, hex4]]
    """
    polylines = []
    lat = []
    lng = []
    for hex in hexagons:
        polygons = h3.h3_set_to_multi_polygon([hex], geo_json=False)
        # flatten polygons into loops.
        outlines = [loop for polygon in polygons for loop in polygon]
        polyline = [outline + [outline[0]] for outline in outlines][0]
        lat.extend(map(lambda v:v[0],polyline))
        lng.extend(map(lambda v:v[1],polyline))
        polylines.append(polyline)
    
    if folium_map is None:
        m = folium.Map(location=[sum(lat)/len(lat), sum(lng)/len(lng)], zoom_start=13, tiles='cartodbpositron')
    else:
        m = folium_map
    for polyline in polylines:
        my_PolyLine=folium.PolyLine(locations=polyline,weight=8,color=color)
        m.add_child(my_PolyLine)
    return m

def visualize_polygon(polyline, color):
    polyline.append(polyline[0])
    lat = [p[0] for p in polyline]
    lng = [p[1] for p in polyline]
    m = folium.Map(location=[sum(lat)/len(lat), sum(lng)/len(lng)], zoom_start=11, tiles='cartodbpositron')
    my_PolyLine=folium.PolyLine(locations=polyline,weight=8,color=color)
    m.add_child(my_PolyLine)
    return m
h3_address = h3.geo_to_h3(60.15, -133.5, 6) # lat, lng, hex resolution
m = visualize_hexagons([h3_address])
display(m)
extent = [(-142.0, 54.999999999999986), (-115.0, 74.99999999999991)]
h3_out = h3.polyfill_polygon(extent, res=4, lnglat_order=True)
m = visualize_hexagons(h3_out)
display(m)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12950391644908615, 'fraction_numerical': 0.0422976501305483, 'mean_word_length': 3.950904392764858, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '404487', 'license': 'Apache-2.0', 'n_tokens_mistral': 770, 'n_tokens_neox': 705, 'n_words': 148, 'path': 'notebooks/h3-grid-from-extent-test-01.ipynb', 'repo_name': 'maphew/h3-py-notebooks', 'size': '1915'}