-
-
Save akhenakh/142ddeec4cd7138b517579b4f4b1063f to your computer and use it in GitHub Desktop.
s2 python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# 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]) | |
# 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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment