Created
March 19, 2025 03:43
-
-
Save ajfriend/93e75cd48f570ca0225fa334955870fb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import h3 | |
import geopandas | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import contextily as cx | |
h = '8c196938064c5ff' | |
# convert to h3.LatLngPoly, which is compatible with | |
# geopandas via __geo_interface__ | |
shape = h3.cells_to_h3shape([h]) | |
df = geopandas.GeoDataFrame( | |
{'geometry': [shape]}, | |
crs='EPSG:4326', # H3 provides coordinates in WGS84 | |
) | |
df = df.to_crs('EPSG:3857') # convert to web mercator for plotting | |
fig, ax = plt.subplots(figsize=(4,4)) | |
df.plot( | |
ax = ax, | |
alpha = 0.2, | |
edgecolor = 'k', | |
) | |
ax.get_xaxis().set_visible(False) | |
ax.get_yaxis().set_visible(False) | |
buffer = .8 | |
x1, y1, x2, y2 = df.total_bounds | |
dx = buffer*(x2 - x1) | |
dy = buffer*(x2 - x1) | |
ax.set_xlim(x1 - dx, x2 + dx) | |
ax.set_ylim(y1 - dy, y2 + dy) | |
cx.add_basemap( | |
ax, | |
crs = df.crs, | |
source = cx.providers.CartoDB.PositronNoLabels, | |
zoom = 20, | |
) | |
fig.tight_layout() | |
fig.savefig('temp.png') |
Author
ajfriend
commented
Mar 19, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment