logo
Reliability & Operations

Geohashing and Quadtrees

Geohash 与 Quadtree 基础

Geohashing

Geohashing 是一种 geocoding 方法,用于把经纬度编码成短的字母数字字符串。由 Gustavo Niemeyer 在 2008 年提出。

例如,San Francisco 的坐标 37.7564, -122.4016 可以编码成 geohash 9q8yy9mf

Geohashing 如何工作?

Geohash 是一种层级化的空间索引,使用 Base-32 编码。第一个字符确定 32 个初始网格之一,该网格再细分成 32 个子网格,如此递归细分,直到达到所需精度。精度越高,cell 越小。

geohashing

Geohash 保证:前缀越长,空间距离越近。例如 9q8yy9mf9q8yy9vx 共享前缀 9q8yy9,因此地理位置更接近。

Geohash 还能提供一定匿名性:通过调整长度,只暴露用户所在区域,而非精确位置。

不同长度 geohash 的 cell 大小:

Geohash lengthCell widthCell height
15000 km5000 km
21250 km1250 km
3156 km156 km
439.1 km19.5 km
54.89 km4.89 km
61.22 km0.61 km
7153 m153 m
838.2 m19.1 m
94.77 m4.77 m
101.19 m0.596 m
11149 mm149 mm
1237.2 mm18.6 mm

Use cases

  • 用简单字符串存储位置
  • 作为 URL 分享,便于记忆
  • 用前缀匹配 + index 快速查邻近点

Examples

Quadtrees

Quadtree 是一种树结构,每个内部节点有 4 个子节点。它用递归四分法来划分二维空间。每个 leaf 保存空间信息。它是二维的 Octree 类比。

quadtree

Types of Quadtrees

  • Point quadtrees
  • Point-region (PR) quadtrees
  • Polygonal map (PM) quadtrees
  • Compressed quadtrees
  • Edge quadtrees

为什么需要 Quadtrees?

经纬度可以计算距离,但对大规模数据来说基于 euclidean distance 的全量计算不现实。

quadtree-subdivision

Quadtree 能高效查询二维范围内的点(经纬度或坐标)。还可根据阈值决定是否继续细分,配合 Hilbert curve 等映射算法提升 range query performance。

Use cases

  • 图像表示、处理与压缩
  • Spatial indexing 与 range queries
  • LBS(Google Maps / Uber)
  • Mesh 生成与图形学
  • Sparse data 存储

相关练习题

Geohashing and Quadtrees

暂无相关练习题