[H3](https://h3geo.org/) is a hexagonal hierarchical geospatial indexing system, developed by [Uber](https://www.uber.com/blog/h3/).
H3 has become popular in the geospatial community for indexing and aggregating spatial data at multiple spatial scales. H3 partitions the world into hierarchical hexagonal grids. The H3 Core Library is written in C, however bindings for many other languages are available, including [[JavaScript]], [[Python]], [[R]] and [[Postgres]].
Use this [web app](https://wolf-h3-viewer.glitch.me/) to explore the H3 grid.
## H3 Bindings
### For JavaScript
[https://github.com/uber/h3-js](https://github.com/uber/h3-js)
### For Postgres
Install the H3 extension from [https://github.com/zachasme/h3-pg](https://github.com/zachasme/h3-pg).
- [https://tech.marksblogg.com/faster-geospatial-enrichment.html](https://tech.marksblogg.com/faster-geospatial-enrichment.html)
- [https://blog.rustprooflabs.com/2022/04/postgis-h3-intro](https://blog.rustprooflabs.com/2022/04/postgis-h3-intro)
- [https://pgxn.org/dist/h3/0.3.0/](https://pgxn.org/dist/h3/0.3.0/)
- https://colab.research.google.com/drive/1794pBBGK_8l1wBG2Zj_BaE0vUxW3tM5K (Notebook for converting Guatemala's data)
The code below provides an example of using H3 to create a hex grid for the country boundary of Guatemala.
```python
import contextily as ctx
import pandas as pd
import geopandas as gpd
import h3pandas
from shapely.geometry import Polygon
# Set parameter names
country_name = "Guatemala"
resolution = 6
# Get country boundary
gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
gdf = gdf.loc[gdf['name'].eq(country_name)]
# Resample to H3 cells
hex_df = gdf.h3.polyfill_resample(resolution)
ax = hex_df.plot();
ctx.add_basemap(ax, crs=gdf.crs.to_string())
# Read shapefile
munis = gpd.read_file(
"https://raw.githubusercontent.com/eanderson-ei/gtm-apps/main/"
"data/spatial/municipalities.json")
# Join with h3 grid
hex_munis = {}
for idx, row in hex_df.iterrows():
muni_names = row.geometry.intersects(munis.set_index('Municipali').geometry)
muni_names = muni_names[muni_names]
hex_munis[idx] = ', '.join(muni_names.index.tolist())
hex_df['muni'] = pd.Series(hex_munis)
# Save as GeoJSON
hex_df.geometry.to_file("gtm-hex-6.geojson", driver='GeoJSON')
```