02 s2
Working with S2 in Vgrid DGGS¶
Full Vgrid DGGS documentation is available at vgrid document.
To work with Vgrid DGGS directly in GeoPandas and Pandas, please use vgridpandas. Full Vgridpandas DGGS documentation is available at vgridpandas document.
To work with Vgrid DGGS in QGIS, install the Vgrid Plugin.
To visualize DGGS in Maplibre GL JS, try the vgrid-maplibre library.
For an interactive demo, visit the Vgrid Homepage.
Install vgrid¶
In [1]:
Copied!
# %pip install vgrid --upgrade
# %pip install vgrid --upgrade
latlon2s2¶
In [2]:
Copied!
from vgrid.conversion.latlon2dggs import latlon2s2
lat = 10.775276
lon = 106.706797
res = 12
s2_token = latlon2s2(lat, lon, res)
s2_token
from vgrid.conversion.latlon2dggs import latlon2s2
lat = 10.775276
lon = 106.706797
res = 12
s2_token = latlon2s2(lat, lon, res)
s2_token
Out[2]:
'31752f5'
S2 to Shapely Polygon¶
In [3]:
Copied!
from vgrid.conversion.dggs2geo.s22geo import s22geo
s2_geo = s22geo(s2_token)
s2_geo
from vgrid.conversion.dggs2geo.s22geo import s22geo
s2_geo = s22geo(s2_token)
s2_geo
Out[3]:
S2 to GeoJSON¶
In [4]:
Copied!
from vgrid.conversion.dggs2geo.s22geo import s22geojson
import json
s2_geojson = s22geojson(s2_token)
s2_geojson
from vgrid.conversion.dggs2geo.s22geo import s22geojson
import json
s2_geojson = s22geojson(s2_token)
s2_geojson
Out[4]:
{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'geometry': {'type': 'Polygon', 'coordinates': (((106.69987670323877, 10.767383458965485), (106.72346389403498, 10.766083802809371), (106.72346389403498, 10.787862253881839), (106.69987670323877, 10.789164413734015), (106.69987670323877, 10.767383458965485)),)}, 'properties': {'s2': '31752f5', 'resolution': 12, 'center_lat': 10.7776235, 'center_lon': 106.7116701, 'avg_edge_len': 2496.415, 'cell_area': 6214807.33, 'cell_perimeter': 9985.661}}]}
Vector to S2¶
In [5]:
Copied!
from vgrid.conversion.vector2dggs.vector2s2 import vector2s2
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/polygon.geojson"
)
vector_to_s2 = vector2s2(file_path, resolution=17, compact=False,
predicate = "intersects", output_format="gpd")
# Visualize vector_to_s2
vector_to_s2.plot(edgecolor="white")
from vgrid.conversion.vector2dggs.vector2s2 import vector2s2
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/polygon.geojson"
)
vector_to_s2 = vector2s2(file_path, resolution=17, compact=False,
predicate = "intersects", output_format="gpd")
# Visualize vector_to_s2
vector_to_s2.plot(edgecolor="white")
Processing features: 100%|██████████| 4/4 [00:00<00:00, 11.68it/s]
Out[5]:
<Axes: >
S2 Compact¶
In [6]:
Copied!
from vgrid.conversion.dggscompact.s2compact import s2compact
s2_compacted = s2compact(vector_to_s2, s2_token="s2", output_format="gpd")
s2_compacted.plot(edgecolor="white")
from vgrid.conversion.dggscompact.s2compact import s2compact
s2_compacted = s2compact(vector_to_s2, s2_token="s2", output_format="gpd")
s2_compacted.plot(edgecolor="white")
Out[6]:
<Axes: >
S2 Expand¶
In [7]:
Copied!
from vgrid.conversion.dggscompact.s2compact import s2expand
s2_expanded = s2expand(vector_to_s2, resolution=18, output_format="gpd")
s2_expanded.plot(edgecolor="white")
from vgrid.conversion.dggscompact.s2compact import s2expand
s2_expanded = s2expand(vector_to_s2, resolution=18, output_format="gpd")
s2_expanded.plot(edgecolor="white")
Out[7]:
<Axes: >
S2 Binning¶
In [3]:
Copied!
from vgrid.binning.s2bin import s2bin
import geopandas as gpd
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/csv/dist1_pois.csv"
)
stats = "count"
s2_bin = s2bin(file_path, resolution=16, stats=stats,
# numeric_field="confidence",
# category="category",
output_format="gpd")
s2_bin.plot(
column=stats, # numeric column to base the colors on
cmap='Spectral_r', # color scheme (matplotlib colormap)
legend=True,
linewidth=0.2 # boundary width (optional)
)
from vgrid.binning.s2bin import s2bin
import geopandas as gpd
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/csv/dist1_pois.csv"
)
stats = "count"
s2_bin = s2bin(file_path, resolution=16, stats=stats,
# numeric_field="confidence",
# category="category",
output_format="gpd")
s2_bin.plot(
column=stats, # numeric column to base the colors on
cmap='Spectral_r', # color scheme (matplotlib colormap)
legend=True,
linewidth=0.2 # boundary width (optional)
)
Generating S2 DGGS: 100%|██████████| 737/737 [00:00<00:00, 1236.72 cells/s]
Out[3]:
<Axes: >
Raster to S2¶
In [9]:
Copied!
# %pip install folium
# %pip install folium
In [10]:
Copied!
from vgrid.conversion.raster2dggs.raster2s2 import raster2s2
from vgrid.utils.io import download_file
raster_url = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/raster/rgb.tif"
)
raster_file = download_file(raster_url)
raster_to_s2 = raster2s2(raster_file,output_format="gpd")
# Visualize raster_to_s2
import folium
m = folium.Map(tiles="CartoDB positron", max_zoom=28)
s2_layer = folium.GeoJson(
raster_to_s2,
style_function=lambda x: {
"fillColor": f"rgb({x['properties']['band_1']}, {x['properties']['band_2']}, {x['properties']['band_3']})",
"fillOpacity": 1,
"color": "black",
"weight": 1,
},
popup=folium.GeoJsonPopup(
fields=["s2", "resolution", "band_1", "band_2", "band_3", "cell_area"],
aliases=["S2 Token", "Resolution", "Band 1", "Band 2", "Band 3", "Area (m²)"],
style="""
background-color: white;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
),
).add_to(m)
m.fit_bounds(s2_layer.get_bounds())
# Display the map
m
from vgrid.conversion.raster2dggs.raster2s2 import raster2s2
from vgrid.utils.io import download_file
raster_url = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/raster/rgb.tif"
)
raster_file = download_file(raster_url)
raster_to_s2 = raster2s2(raster_file,output_format="gpd")
# Visualize raster_to_s2
import folium
m = folium.Map(tiles="CartoDB positron", max_zoom=28)
s2_layer = folium.GeoJson(
raster_to_s2,
style_function=lambda x: {
"fillColor": f"rgb({x['properties']['band_1']}, {x['properties']['band_2']}, {x['properties']['band_3']})",
"fillOpacity": 1,
"color": "black",
"weight": 1,
},
popup=folium.GeoJsonPopup(
fields=["s2", "resolution", "band_1", "band_2", "band_3", "cell_area"],
aliases=["S2 Token", "Resolution", "Band 1", "Band 2", "Band 3", "Area (m²)"],
style="""
background-color: white;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
),
).add_to(m)
m.fit_bounds(s2_layer.get_bounds())
# Display the map
m
WARNING [rasterio._env:368 open()] CPLE_AppDefined in PROJ: proj_create_from_database: Cannot find proj.db WARNING [rasterio._env:368 open()] CPLE_AppDefined in PROJ: proj_create_from_database: Cannot find proj.db
rgb.tif already exists. Skip downloading. Set overwrite=True to overwrite. Nearest s2 resolution determined: 24
Converting raster to S2: 100%|██████████| 8231/8231 [00:03<00:00, 2724.89 cells/s]
Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook
S2 Generator¶
In [11]:
Copied!
from vgrid.generator.s2grid import s2grid
s2_grid = s2grid(resolution=0,output_format="gpd")
# s2_grid = s2grid(resolution=16,bbox=[106.699007, 10.762811, 106.717674, 10.778649],output_format="gpd")
s2_grid.plot(edgecolor='white')
from vgrid.generator.s2grid import s2grid
s2_grid = s2grid(resolution=0,output_format="gpd")
# s2_grid = s2grid(resolution=16,bbox=[106.699007, 10.762811, 106.717674, 10.778649],output_format="gpd")
s2_grid.plot(edgecolor='white')
Generating S2 DGGS: 100%|██████████| 6/6 [00:00<00:00, 730.91 cells/s]
Out[11]:
<Axes: >
S2 Inspect¶
In [12]:
Copied!
from vgrid.stats.s2stats import s2inspect
resolution = 6
s2_inspect = s2inspect(resolution)
s2_inspect.head()
from vgrid.stats.s2stats import s2inspect
resolution = 6
s2_inspect = s2inspect(resolution)
s2_inspect.head()
Generating S2 DGGS: 100%|██████████| 24576/24576 [00:12<00:00, 1957.86 cells/s]
Out[12]:
s2 | resolution | center_lat | center_lon | avg_edge_len | cell_area | cell_perimeter | geometry | crossed | norm_area | ipq | zsc | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0001 | 6 | -34.976691 | -44.394709 | 126254.075 | 1.391628e+10 | 505016.299 | POLYGON ((-45 -35.26439, -43.79085 -35.82442, ... | False | 0.670514 | 0.685681 | 0.828047 |
1 | 0003 | 6 | -35.529669 | -43.170222 | 127426.102 | 1.427186e+10 | 509704.406 | POLYGON ((-43.79085 -35.82442, -42.55096 -36.3... | False | 0.687647 | 0.690325 | 0.830846 |
2 | 0005 | 6 | -34.378795 | -43.170143 | 128679.032 | 1.464789e+10 | 514716.129 | POLYGON ((-43.79085 -34.68431, -42.55096 -35.2... | False | 0.705765 | 0.694783 | 0.833525 |
3 | 0007 | 6 | -33.834099 | -44.394632 | 127398.731 | 1.426656e+10 | 509594.924 | POLYGON ((-45 -34.13233, -43.79085 -34.68431, ... | False | 0.687392 | 0.690365 | 0.830871 |
4 | 0009 | 6 | -32.679004 | -44.394554 | 128450.519 | 1.460280e+10 | 513802.077 | POLYGON ((-45 -32.98768, -43.79085 -33.53064, ... | False | 0.703593 | 0.695111 | 0.833721 |
Distribution of S2 Area Distortions¶
In [13]:
Copied!
from vgrid.stats.s2stats import s2_norm_area
s2_norm_area(s2_inspect)
from vgrid.stats.s2stats import s2_norm_area
s2_norm_area(s2_inspect)
Distribution of S2 IPQ Compactness¶
In [14]:
Copied!
from vgrid.stats.s2stats import s2_compactness
s2_compactness(s2_inspect)
from vgrid.stats.s2stats import s2_compactness
s2_compactness(s2_inspect)
S2 Statistics¶
In [15]:
Copied!
from vgrid.stats.s2stats import s2stats
s2stats('km')
from vgrid.stats.s2stats import s2stats
s2stats('km')
Out[15]:
resolution | number_of_cells | avg_edge_len_km | avg_cell_area_km2 | |
---|---|---|---|---|
0 | 0 | 6 | 9220.137605 | 8.501094e+07 |
1 | 1 | 24 | 4610.068803 | 2.125273e+07 |
2 | 2 | 96 | 2305.034401 | 5.313184e+06 |
3 | 3 | 384 | 1152.517201 | 1.328296e+06 |
4 | 4 | 1536 | 576.258600 | 3.320740e+05 |
5 | 5 | 6144 | 288.129300 | 8.301849e+04 |
6 | 6 | 24576 | 144.064650 | 2.075462e+04 |
7 | 7 | 98304 | 72.032325 | 5.188656e+03 |
8 | 8 | 393216 | 36.016163 | 1.297164e+03 |
9 | 9 | 1572864 | 18.008081 | 3.242910e+02 |
10 | 10 | 6291456 | 9.004041 | 8.107275e+01 |
11 | 11 | 25165824 | 4.502020 | 2.026819e+01 |
12 | 12 | 100663296 | 2.251010 | 5.067047e+00 |
13 | 13 | 402653184 | 1.125505 | 1.266762e+00 |
14 | 14 | 1610612736 | 0.562753 | 3.166904e-01 |
15 | 15 | 6442450944 | 0.281376 | 7.917261e-02 |
16 | 16 | 25769803776 | 0.140688 | 1.979315e-02 |
17 | 17 | 103079215104 | 0.070344 | 4.948288e-03 |
18 | 18 | 412316860416 | 0.035172 | 1.237072e-03 |
19 | 19 | 1649267441664 | 0.017586 | 3.092680e-04 |
20 | 20 | 6597069766656 | 0.008793 | 7.731700e-05 |
21 | 21 | 26388279066624 | 0.004397 | 1.932925e-05 |
22 | 22 | 105553116266496 | 0.002198 | 4.832312e-06 |
23 | 23 | 422212465065984 | 0.001099 | 1.208078e-06 |
24 | 24 | 1688849860263936 | 0.000550 | 3.020195e-07 |
25 | 25 | 6755399441055744 | 0.000275 | 7.550488e-08 |
26 | 26 | 27021597764222976 | 0.000137 | 1.887622e-08 |
27 | 27 | 108086391056891904 | 0.000069 | 4.719055e-09 |
28 | 28 | 432345564227567616 | 0.000034 | 1.179764e-09 |
29 | 29 | 1729382256910270464 | 0.000017 | 2.949409e-10 |
30 | 30 | 6917529027641081856 | 0.000009 | 7.373523e-11 |