In [1]:
import folium
import pywraps2 as s2
In [2]:
# create a rect in s2
region_rect = s2.S2LatLngRect(
        s2.S2LatLng.FromDegrees(48.831776, 2.222639),
        s2.S2LatLng.FromDegrees(48.902839, 2.406))
In [3]:
# ask s2 to create a cover of this rect
coverer = s2.S2RegionCoverer()
coverer.set_min_level(10)
coverer.set_max_level(30)
coverer.set_max_cells(60)
covering = coverer.GetCovering(region_rect)
print([c.ToToken() for c in covering])
['47e66454', '47e66459', '47e664c2c', '47e664d4', '47e664dc', '47e664f', '47e6654', '47e66584', '47e6659c', '47e665a4', '47e665ac', '47e66c2c', '47e66c34', '47e66c4c', '47e66c51', '47e66c53', '47e66dc', '47e66e4', '47e66e84', '47e66e8c', '47e66ef4', '47e66efc', '47e66f04', '47e66f0c', '47e66f74', '47e66f7c', '47e66fc', '47e6701', '47e6703', '47e67044', '47e6704c', '47e6706c', '47e67074', '47e6718c', '47e67194', '47e671b4', '47e671bc', '47e671d', '47e671f', '47e6721', '47e67224', '47e6723c', '47e67244', '47e6725c', '47e6727', '47e67a94', '47e67a9c', '47e67ab', '47e67ad', '47e67ae4', '47e67aec', '47e67b1c', '47e67b24', '47e67b2c']
In [4]:
# create a map
map_osm = folium.Map(location=[48.86, 2.3],zoom_start=12, tiles='Stamen Toner')

# get vertices from rect to draw them on map
rect_vertices = []
for i in [0, 1, 2, 3, 0]:
    vertex = region_rect.GetVertex(i)
    rect_vertices.append([vertex.lat().degrees(), vertex.lng().degrees()])
    
# draw the cells
style_function = lambda x: {'weight': 1, 'fillColor':'#eea500'}
for cellid in covering:
    cell = s2.S2Cell(cellid)
    vertices = []
    for i in range(0, 4):
        vertex = cell.GetVertex(i)
        latlng = s2.S2LatLng(vertex)
        vertices.append([latlng.lng().degrees(),
                         latlng.lat().degrees()])
        
    gj = folium.GeoJson({ "type": "Polygon", "coordinates": [vertices]}, style_function=style_function)
    gj.add_children(folium.Popup(cellid.ToToken()))
    gj.add_to(map_osm)
    
# warning PolyLine is lat,lng based while GeoJSON is not
ls = folium.PolyLine(rect_vertices, color='red', weight=2)
ls.add_children(folium.Popup("shape"))
ls.add_to(map_osm)
    
map_osm
/home/akh/dev/venv3/lib/python3.6/site-packages/ipykernel_launcher.py:22: FutureWarning: Method `add_children` is deprecated. Please use `add_child` instead.
/home/akh/dev/venv3/lib/python3.6/site-packages/ipykernel_launcher.py:27: FutureWarning: Method `add_children` is deprecated. Please use `add_child` instead.
Out[4]: