You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何展示H3网格的索引顺序并生成带索引曲线的可视化?

实现带标签与索引曲线的H3网格可视化

核心思路

通过H3库生成网格数据,用Matplotlib绘制六边形、添加标签,并基于网格中心坐标生成平滑曲线连接索引序列。


1. 安装依赖库

需要用到H3网格计算、绘图及数值处理工具:

pip install h3 matplotlib numpy

2. 完整实现代码

import h3
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
import numpy as np

# 生成H3网格:自定义中心坐标与分辨率
center_lat, center_lng = 30.0, 120.0
resolution = 6  # 分辨率越小,六边形面积越大
center_h3 = h3.geo_to_h3(center_lat, center_lng, resolution)
# 获取中心单元格及2环范围内的所有H3索引
h3_indexes = list(h3.k_ring(center_h3, 2))

# 经纬度转平面坐标(小范围近似投影,适合局部区域可视化)
def geo_to_plane(lat, lng, origin_lat, origin_lng):
    lat_diff = (lat - origin_lat) * 111.32  # 纬度每度约111.32km
    lng_diff = (lng - origin_lng) * 111.32 * np.cos(np.radians(origin_lat))  # 经度距离随纬度修正
    return lng_diff, lat_diff

origin_lat, origin_lng = center_lat, center_lng
h3_coords = []
for idx in h3_indexes:
    lat, lng = h3.h3_to_geo(idx)
    x, y = geo_to_plane(lat, lng, origin_lat, origin_lng)
    h3_coords.append((x, y))

# 初始化绘图
fig, ax = plt.subplots(figsize=(10, 10))
ax.set_aspect('equal')

# 绘制六边形网格并添加标签
hex_radius = h3.edge_length(resolution, unit='km') * 0.9  # 适配投影的六边形半径
for idx, (x, y) in zip(h3_indexes, h3_coords):
    # 绘制六边形
    hexagon = RegularPolygon((x, y), numVertices=6, radius=hex_radius, 
                             edgecolor='black', facecolor='#f0f8ff', alpha=0.7)
    ax.add_patch(hexagon)
    # 添加短标签(取H3索引后6位,避免文字拥挤)
    ax.text(x, y, idx[-6:], ha='center', va='center', fontsize=8)

# 绘制平滑索引曲线
x_coords = [p[0] for p in h3_coords]
y_coords = [p[1] for p in h3_coords]
# 用线性插值生成平滑曲线
t = np.linspace(0, 1, len(x_coords)*10)
x_smooth = np.interp(t, np.linspace(0,1,len(x_coords)), x_coords)
y_smooth = np.interp(t, np.linspace(0,1,len(y_coords)), y_coords)
ax.plot(x_smooth, y_smooth, color='#ff6b6b', linewidth=2, linestyle='-', label='索引曲线')

# 调整绘图范围与样式
ax.set_xlim(min(x_coords)-hex_radius, max(x_coords)+hex_radius)
ax.set_ylim(min(y_coords)-hex_radius, max(y_coords)+hex_radius)
plt.legend()
plt.title('带标签与索引曲线的H3网格')
plt.show()

3. 关键细节说明

  • 网格生成:h3.k_ring函数用于获取中心单元格周围N环内的所有网格索引,可通过调整第二个参数控制网格数量。
  • 坐标投影:小范围区域用近似平面投影即可满足需求;如果是全球或大范围可视化,可结合专业球面投影工具优化。
  • 标签优化:直接显示完整H3索引会导致文字重叠,因此截取索引后6位作为简化标签。
  • 平滑曲线:通过np.interp对原始坐标点进行插值,生成连续平滑的曲线,避免折线生硬。

4. 大范围投影扩展(可选)

若需要处理跨区域的H3网格,安装cartopy后可替换为球面投影逻辑:

pip install cartopy

修改绘图部分:

import cartopy.crs as ccrs

fig, ax = plt.subplots(figsize=(10, 10), subplot_kw={'projection': ccrs.PlateCarree()})
ax.coastlines()
# 直接用经纬度绘制六边形
for idx in h3_indexes:
    lat, lng = h3.h3_to_geo(idx)
    hex_poly = h3.h3_to_geo_boundary(idx, geo_json=True)
    poly_coords = [(lng, lat) for lat, lng in hex_poly]
    ax.fill(*zip(*poly_coords), edgecolor='black', facecolor='#f0f8ff', alpha=0.7)
    ax.text(lng, lat, idx[-6:], ha='center', va='center', fontsize=8, transform=ccrs.PlateCarree())

内容的提问来源于stack exchange,提问作者Peter Krauss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 01:40:21