15 tilecode
Working with Tilecode 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.
In [1]:
Copied!
# %pip install vgrid --upgrade
# %pip install vgrid --upgrade
latlon2tilecode¶
In [2]:
Copied!
from vgrid.conversion.latlon2dggs import latlon2tilecode
lat = 10.775276
lon = 106.706797
res = 14
tilecode_id = latlon2tilecode(lat, lon, res)
tilecode_id
from vgrid.conversion.latlon2dggs import latlon2tilecode
lat = 10.775276
lon = 106.706797
res = 14
tilecode_id = latlon2tilecode(lat, lon, res)
tilecode_id
Out[2]:
'z14x13048y7698'
Tilecode to Shapely Polygon¶
In [3]:
Copied!
from vgrid.conversion.dggs2geo.tilecode2geo import tilecode2geo
tilecode_geo = tilecode2geo(tilecode_id)
tilecode_geo
from vgrid.conversion.dggs2geo.tilecode2geo import tilecode2geo
tilecode_geo = tilecode2geo(tilecode_id)
tilecode_geo
Out[3]:
Tilecode to GeoJSON¶
In [4]:
Copied!
from vgrid.conversion.dggs2geo.tilecode2geo import tilecode2geojson
tilecode_geojson = tilecode2geojson(tilecode_id)
tilecode_geojson
from vgrid.conversion.dggs2geo.tilecode2geo import tilecode2geojson
tilecode_geojson = tilecode2geojson(tilecode_id)
tilecode_geojson
Out[4]:
{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'geometry': {'type': 'Polygon', 'coordinates': (((106.69921875, 10.768555807732435), (106.72119140625, 10.768555807732435), (106.72119140625, 10.790140750321735), (106.69921875, 10.790140750321735), (106.69921875, 10.768555807732435)),)}, 'properties': {'tilecode_id': 'z14x13048y7698', 'resolution': 14, 'center_lat': 10.7793483, 'center_lon': 106.7102051, 'cell_width': 2403.192, 'cell_height': 2387.578, 'cell_area': 5737603.485, 'cell_perimeter': 9581.368}}]}
Vector to Tilecode¶
In [5]:
Copied!
from vgrid.conversion.vector2dggs.vector2tilecode import vector2tilecode
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/polygon.geojson"
)
vector_to_tilecode = vector2tilecode(file_path, resolution=18, compact=False,
predicate = "intersects",output_format="gpd")
# Visualize the output
vector_to_tilecode.plot(edgecolor="white")
from vgrid.conversion.vector2dggs.vector2tilecode import vector2tilecode
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/polygon.geojson"
)
vector_to_tilecode = vector2tilecode(file_path, resolution=18, compact=False,
predicate = "intersects",output_format="gpd")
# Visualize the output
vector_to_tilecode.plot(edgecolor="white")
Processing features: 100%|██████████| 4/4 [00:00<00:00, 194.15it/s]
Out[5]:
<Axes: >
Tilecode Compact¶
In [6]:
Copied!
from vgrid.conversion.dggscompact.tilecodecompact import tilecodecompact
tilecode_compacted = tilecodecompact(vector_to_tilecode, tilecode_id="tilecode", output_format="gpd")
tilecode_compacted.plot(edgecolor="white")
from vgrid.conversion.dggscompact.tilecodecompact import tilecodecompact
tilecode_compacted = tilecodecompact(vector_to_tilecode, tilecode_id="tilecode", output_format="gpd")
tilecode_compacted.plot(edgecolor="white")
Out[6]:
<Axes: >
Tilecode Expand¶
In [7]:
Copied!
from vgrid.conversion.dggscompact.tilecodecompact import tilecodeexpand
tilecode_expanded = tilecodeexpand(vector_to_tilecode, resolution=19, output_format="gpd")
tilecode_expanded.plot(edgecolor="white")
from vgrid.conversion.dggscompact.tilecodecompact import tilecodeexpand
tilecode_expanded = tilecodeexpand(vector_to_tilecode, resolution=19, output_format="gpd")
tilecode_expanded.plot(edgecolor="white")
Out[7]:
<Axes: >
Tilecode Binning¶
In [16]:
Copied!
from vgrid.binning.tilecodebin import tilecodebin
import geopandas as gpd
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/csv/dist1_pois.csv"
)
stats = "count"
tilecode_bin = tilecodebin(file_path, resolution=18, stats=stats,
# numeric_field="confidence",
# category="category",
output_format="gpd")
tilecode_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.tilecodebin import tilecodebin
import geopandas as gpd
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/csv/dist1_pois.csv"
)
stats = "count"
tilecode_bin = tilecodebin(file_path, resolution=18, stats=stats,
# numeric_field="confidence",
# category="category",
output_format="gpd")
tilecode_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 Tilecode DGGS: 768 cells [00:00, 8895.22 cells/s]
Out[16]:
<Axes: >
Raster to Tilecode¶
In [9]:
Copied!
# %pip install folium
# %pip install folium
In [10]:
Copied!
from vgrid.conversion.raster2dggs.raster2tilecode import raster2tilecode
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_tilecode = raster2tilecode(raster_file,output_format="gpd")
# Visualize the output
import folium
m = folium.Map(tiles="CartoDB positron", max_zoom=28)
tilecode_layer = folium.GeoJson(
raster_to_tilecode,
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=["tilecode", "band_1", "band_2", "band_3"],
aliases=["Tilecode ID", "Band 1", "Band 2", "Band 3"],
style="""
background-color: white;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
),
).add_to(m)
m.fit_bounds(tilecode_layer.get_bounds())
# Display the map
m
from vgrid.conversion.raster2dggs.raster2tilecode import raster2tilecode
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_tilecode = raster2tilecode(raster_file,output_format="gpd")
# Visualize the output
import folium
m = folium.Map(tiles="CartoDB positron", max_zoom=28)
tilecode_layer = folium.GeoJson(
raster_to_tilecode,
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=["tilecode", "band_1", "band_2", "band_3"],
aliases=["Tilecode ID", "Band 1", "Band 2", "Band 3"],
style="""
background-color: white;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
),
).add_to(m)
m.fit_bounds(tilecode_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 tilecode resolution determined: 26
Resampling: 100%|██████████| 8925/8925 [00:00<00:00, 163480.10 cells/s] Converting raster to Tilecode: 100%|██████████| 8806/8806 [00:00<00:00, 38007.93 cells/s]
Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Tilecode Generator¶
In [11]:
Copied!
from vgrid.generator.tilecodegrid import tilecodegrid
# tilecode_grid = tilecodegrid(resolution=2,output_format="gpd")
tilecode_grid = tilecodegrid(resolution=18,bbox=[106.699007, 10.762811, 106.717674, 10.778649],output_format="gpd")
tilecode_grid.plot(edgecolor="white")
from vgrid.generator.tilecodegrid import tilecodegrid
# tilecode_grid = tilecodegrid(resolution=2,output_format="gpd")
tilecode_grid = tilecodegrid(resolution=18,bbox=[106.699007, 10.762811, 106.717674, 10.778649],output_format="gpd")
tilecode_grid.plot(edgecolor="white")
Generating Tilecode DGGS: 195 cells [00:00, 4683.80 cells/s]
Out[11]:
<Axes: >
Tilecode Inspect¶
In [12]:
Copied!
from vgrid.stats.tilecodestats import tilecodeinspect
resolution = 7
tilecode_inspect = tilecodeinspect(resolution)
tilecode_inspect.head()
from vgrid.stats.tilecodestats import tilecodeinspect
resolution = 7
tilecode_inspect = tilecodeinspect(resolution)
tilecode_inspect.head()
Generating Tilecode DGGS: 16384 cells [00:01, 8448.16 cells/s]
Out[12]:
tilecode | resolution | center_lat | center_lon | cell_width | cell_height | cell_area | cell_perimeter | geometry | crossed | norm_area | ipq | zsc | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | z7x0y0 | 7 | 84.926801 | -178.59375 | 28454.129 | 27771.077 | 7.711209e+08 | 111092.678 | POLYGON ((-180 84.80247, -177.1875 84.80247, -... | False | 0.024863 | 0.785166 | 0.886095 |
1 | z7x0y1 | 7 | 84.671917 | -178.59375 | 29879.298 | 29162.169 | 8.503136e+08 | 116657.765 | POLYGON ((-180 84.54136, -177.1875 84.54136, -... | False | 0.027416 | 0.785166 | 0.886095 |
2 | z7x0y2 | 7 | 84.404267 | -178.59375 | 31375.156 | 30622.280 | 9.375993e+08 | 122499.013 | POLYGON ((-180 84.26717, -177.1875 84.26717, -... | False | 0.030231 | 0.785167 | 0.886096 |
3 | z7x0y3 | 7 | 84.123216 | -178.59375 | 32945.097 | 32154.730 | 1.033796e+09 | 128629.712 | POLYGON ((-180 83.97926, -177.1875 83.97926, -... | False | 0.033332 | 0.785167 | 0.886096 |
4 | z7x0y4 | 7 | 83.828101 | -178.59375 | 34592.666 | 33762.981 | 1.139804e+09 | 135063.726 | POLYGON ((-180 83.67694, -177.1875 83.67694, -... | False | 0.036750 | 0.785168 | 0.886096 |
Distribution of Tilecode Area Distortions¶
In [13]:
Copied!
from vgrid.stats.tilecodestats import tilecode_norm_area
tilecode_norm_area(tilecode_inspect)
from vgrid.stats.tilecodestats import tilecode_norm_area
tilecode_norm_area(tilecode_inspect)
Distribution of Tilecode IPQ Compactness¶
In [14]:
Copied!
from vgrid.stats.tilecodestats import tilecode_compactness
tilecode_compactness(tilecode_inspect)
from vgrid.stats.tilecodestats import tilecode_compactness
tilecode_compactness(tilecode_inspect)
Tilecode Statistics¶
In [15]:
Copied!
from vgrid.stats import tilecodestats
tilecodestats('km')
from vgrid.stats import tilecodestats
tilecodestats('km')
Out[15]:
resolution | number_of_cells | avg_edge_len_km | avg_cell_area_km2 | |
---|---|---|---|---|
0 | 0 | 1 | 22584.632492 | 5.100656e+08 |
1 | 1 | 4 | 11292.316246 | 1.275164e+08 |
2 | 2 | 16 | 5646.158123 | 3.187910e+07 |
3 | 3 | 64 | 2823.079061 | 7.969775e+06 |
4 | 4 | 256 | 1411.539531 | 1.992444e+06 |
5 | 5 | 1024 | 705.769765 | 4.981110e+05 |
6 | 6 | 4096 | 352.884883 | 1.245277e+05 |
7 | 7 | 16384 | 176.442441 | 3.113194e+04 |
8 | 8 | 65536 | 88.221221 | 7.782984e+03 |
9 | 9 | 262144 | 44.110610 | 1.945746e+03 |
10 | 10 | 1048576 | 22.055305 | 4.864365e+02 |
11 | 11 | 4194304 | 11.027653 | 1.216091e+02 |
12 | 12 | 16777216 | 5.513826 | 3.040228e+01 |
13 | 13 | 67108864 | 2.756913 | 7.600570e+00 |
14 | 14 | 268435456 | 1.378457 | 1.900143e+00 |
15 | 15 | 1073741824 | 0.689228 | 4.750356e-01 |
16 | 16 | 4294967296 | 0.344614 | 1.187589e-01 |
17 | 17 | 17179869184 | 0.172307 | 2.968973e-02 |
18 | 18 | 68719476736 | 0.086154 | 7.422432e-03 |
19 | 19 | 274877906944 | 0.043077 | 1.855608e-03 |
20 | 20 | 1099511627776 | 0.021538 | 4.639020e-04 |
21 | 21 | 4398046511104 | 0.010769 | 1.159755e-04 |
22 | 22 | 17592186044416 | 0.005385 | 2.899387e-05 |
23 | 23 | 70368744177664 | 0.002692 | 7.248468e-06 |
24 | 24 | 281474976710656 | 0.001346 | 1.812117e-06 |
25 | 25 | 1125899906842624 | 0.000673 | 4.530293e-07 |
26 | 26 | 4503599627370496 | 0.000337 | 1.132573e-07 |
27 | 27 | 18014398509481984 | 0.000168 | 2.831433e-08 |
28 | 28 | 72057594037927936 | 0.000084 | 7.078583e-09 |
29 | 29 | 288230376151711744 | 0.000042 | 1.769646e-09 |