<jupyter_start><jupyter_text>google/s2geometry SWIG Python-C++ bindings<jupyter_code>%matplotlib notebook
from datetime import datetime
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
from ipywidgets import Checkbox, IntSlider, ToggleButtons, interactive
import matplotlib.pyplot as plt
from s2 import S2Cell, S2LatLng, S2LatLngRect, S2RegionCoverer
from shapely.geometry import Polygon
class Plot:
    def __init__(self):
        self.fig = plt.figure(figsize=(4, 3), dpi=200)

        proj = cimgt.OSM()
        self.ax = plt.axes(projection=proj.crs)
        self.ax.add_image(proj, 17, interpolation='bilinear')
    
        # cartopy extent order is (x0, x1, y0, y1) or (lon0, lon1, lat0, lat1)
        self.ax.set_extent([-3.478665, -3.471724, 50.726045, 50.728911])
        
    def update(self, min_level, max_level, max_cells, cover_type=None, show_bbox=True):
        start = datetime.now()
        
        self.ax.artists = []
    
        # our bounding box of interest
        lat0, lon0 = 50.726771, -3.476471
        lat1, lon1 = 50.728089, -3.473832

        # shapely order is (x, y) or (lon, lat)
        bbox = Polygon([(lon0, lat0), (lon0, lat1),
                        (lon1, lat1), (lon1, lat0)])

        # s2 order is (y, x) or (lat, lon)
        region_rect = S2LatLngRect(
            S2LatLng.FromDegrees(lat0, lon0),
            S2LatLng.FromDegrees(lat1, lon1))

        coverer = S2RegionCoverer()
        coverer.set_min_level(min_level)
        coverer.set_max_level(max_level)
        coverer.set_max_cells(max_cells)
        region = region_rect.GetCapBound() if cover_type == "Cap" else region_rect
        covering = coverer.GetCovering(region)
        
        geoms = []
        for cellid in covering:
            new_cell = S2Cell(cellid)
            vertices = []
            for i in range(0, 4):
                vertex = new_cell.GetVertex(i)
                latlng = S2LatLng(vertex)
                # shapely vertex order is (x, y) or (lon, lat)
                vertices.append((latlng.lng().degrees(),
                                 latlng.lat().degrees()))
            geo = Polygon(vertices)
            geoms.append(geo)
            
        print("S2 geometries = {}".format(len(geoms)))

        pc = ccrs.PlateCarree()
        show_bbox = show_bbox.value if hasattr(show_bbox, "value") else show_bbox
        if show_bbox:
            self.ax.add_geometries([bbox], pc, facecolor=(0, 1, 0, .2), edgecolor=(0, 0, 0, 1), zorder=10)
        self.ax.add_geometries(geoms, pc, facecolor='coral', edgecolor='black', alpha=0.4, zorder=5)
    
        end = datetime.now()
    
        print("Update time   = {}s".format(end - start))
    
        plt.draw()
def validate_min_level(*args):
    if min_level.value > max_level.value:
        min_level.value = args[0]['old']


def validate_max_level(*args):
    if max_level.value < min_level.value:
        max_level.value = args[0]['old']


min_level = IntSlider(min=0, max=30, step=1, value=4, continuous_update=False)
max_level = IntSlider(min=0, max=30, step=1, value=18, continuous_update=False)
max_cells = IntSlider(min=1, max=250, step=1, value=10, continuous_update=False)
bbox = Checkbox(value=True, description="Show bbox", disable=False, indent=False)
cover = ToggleButtons(
    options=['Cap', 'Rectangle'],
    description='Cover:',
    disabled=False,
    button_style='', # 'success', 'info', 'warning', 'danger' or ''
    tooltips=['Cover region with a spherical cap', 'Cover region with a bounding box'],
    value="Rectangle"
)

min_level.observe(validate_min_level, "value")
max_level.observe(validate_max_level, "value")

plot = Plot()

interactive(plot.update, min_level=min_level, max_level=max_level,
            max_cells=max_cells, cover_type=cover, show_bbox=bbox)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12571726656233698, 'fraction_numerical': 0.036515388628064686, 'mean_word_length': 2.800792864222002, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '247126', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1338, 'n_words': 283, 'path': 'context/notebooks/example.ipynb', 'repo_name': 'bjlittle/docker-s2geometry', 'size': '3834'}