# Geohash

**Geohash** 是一种公有领域地理编码系统，由软件开发者古斯塔沃·尼迈耶（Gustavo Niemeyer）于 2008 年发明，他当时以 Go 与 MongoDB 工具方面的工作闻名。该系统将一对经纬度编码为短字符串，由字母和数字组成<sup>[1](https://github.com/phrozen/geohash)</sup><sup> • </sup><sup>[2](https://blog.cartographica.com/patents-and-advancing-art-in-geographic-coordinates.html)</sup>。字符串中的每个字符都将区域缩小为原来的 1/32，因此哈希越长，标识的矩形越小；两个哈希共享前缀则表示地理邻近<sup>[3](https://firebase.google.com/docs/firestore/solutions/geoqueries)</sup>。该系统广泛用于数据库中的空间索引与邻近搜索，因为它将二维位置转换为单个可排序字符串，普通 B 树索引即可查询<sup>[3](https://firebase.google.com/docs/firestore/solutions/geoqueries)</sup>。

| 关键事实 | 详情 |
|---|---|
| 起源 | 2008 年由古斯塔沃·尼迈耶（Gustavo Niemeyer）发明，作为 geohash.org URL 缩短服务的一部分；置于公有领域<sup>[1](https://github.com/phrozen/geohash)</sup><sup> • </sup><sup>[2](https://blog.cartographica.com/patents-and-advancing-art-in-geographic-coordinates.html)</sup> |
| 编码 | 将纬度和经度的比特交错，然后用字母表 0–9 与 b–z（排除 a、i、l、o）对结果进行 base32 编码<sup>[4](https://www.coordinately.org/learn/geohash-explained)</sup> |
| 层级 | 每个字符指定前缀哈希的 32 个细分之一；某点在较低精度下的 Geohash 是其较高精度 Geohash 的前缀<sup>[3](https://firebase.google.com/docs/firestore/solutions/geoqueries)</sup><sup> • </sup><sup>[5](https://github.com/nesterenko-kv/geohash)</sup> |
| 最大精度 | 12 个字符，即 60 比特<sup>[5](https://github.com/nesterenko-kv/geohash)</sup> |
| 示例尺度 | 在自由女神像附近，5 字符哈希 dr5r7 覆盖约 20 平方公里；12 字符的 dr5r77kkekp9 缩小到约 6 平方厘米<sup>[6](https://www.usenix.org/system/files/usenixsecurity24-guo-ruoyang.pdf)</sup> |
| 坐标基准 | WGS 84 经纬度<sup>[4](https://www.coordinately.org/learn/geohash-explained)</sup> |
| 主要缺点 | 单元格面积随纬度变化很大，且邻近点可能因跨越单元格边界而落入相距很远的哈希，因此半径查询必须覆盖相邻单元格并过滤误报<sup>[7](https://h3geo.org/docs/comparisons/geohash/)</sup><sup> • </sup><sup>[3](https://firebase.google.com/docs/firestore/solutions/geoqueries)</sup> |

## 编码原理

Geohash 通过位交错将二维坐标降为一维字符串。纬度范围为 [-90, 90]，经度范围为 [-180, 180]，二者各自量化为二进制表示；然后两个比特流交错，经度占据一种奇偶性的位位置，纬度占据另一种，从而产生单个二进制值<sup>[5](https://github.com/nesterenko-kv/geohash)</sup>。这种交错是 Z 序曲线（一种空间填充曲线，对网格单元排序，使邻近点获得邻近值）的应用，而 Z 序曲线是标准空间填充曲线之一<sup>[1](https://github.com/phrozen/geohash)</sup>。

随后该二进制值以 base32 编码。每个字符携带 5 比特，因此每增加一个字符，父单元格就被细分为 32 个更小的单元格，字符串长度直接决定精度<sup>[3](https://firebase.google.com/docs/firestore/solutions/geoqueries)</sup><sup> • </sup><sup>[5](https://github.com/nesterenko-kv/geohash)</sup>。字母表为数字 0 到 9 和小写字母 b 到 z，排除 a、i、l、o，这些字符的选择是为了避免视觉混淆<sup>[4](https://www.coordinately.org/learn/geohash-explained)</sup>。由于每个字符是在扩展前一个哈希而非替换它，某点在较低精度下的 Geohash 始终是其较高精度 Geohash 的前缀；正是这种前缀性质使邻近搜索可以归约为数据库索引中的前缀匹配<sup>[5](https://github.com/nesterenko-kv/geohash)</sup>。

该方案由尼迈耶的公有领域定义完整规定，因此各语言实现行为一致<sup>[4](https://www.coordinately.org/learn/geohash-explained)</sup>。字符串可编码任意精度的单元格，因为字符串格式原则上不固定最大分辨率，不过常见实现止于 12 个字符<sup>[7](https://h3geo.org/docs/comparisons/geohash/)</sup><sup> • </sup><sup>[5](https://github.com/nesterenko-kv/geohash)</sup>。

## 精度与层级

每增加一个字符，单元格面积除以 32<sup>[8](https://rpsonline.com.sg/proceedings/esrel-sra-e2025/pdf/ESREL-SRA-E2025-P6110.pdf)</sup>。实际范围很宽。在[自由女神像](https://www.edgepedia.org/zh/statue-of-liberty)附近，哈希 dr5r7（5 个字符）标出约 20 平方公里的子区域，而 12 个字符的 dr5r77kkekp9 将同一点缩小到约 6 平方厘米<sup>[6](https://www.usenix.org/system/files/usenixsecurity24-guo-ruoyang.pdf)</sup>。诸如 udby51 的 6 字符哈希表示约 1.22 公里 × 0.61 公里的区域<sup>[9](https://trepo.tuni.fi/bitstream/handle/10024/134662/Lempi%C3%A4inenEsa.pdf?isAllowed=y&sequence=2)</sup>。

层级是严格的：每个精细单元格完全位于其父单元格内，因此将哈希截断为更少字符会得到一个包含它的单元格，且没有近似<sup>[7](https://h3geo.org/docs/comparisons/geohash/)</sup>。这种精确嵌套是后来的网格系统为换取其他优点而放弃的一项性质。

## 局限

**面积畸变。** 由于 Geohash 单元格是经纬度上的矩形，其实际面积随位置变化。极地附近的一度经度所代表的距离远小于赤道附近的一度经度，因此固定精度的哈希在不同纬度覆盖的地面面积差异很大<sup>[7](https://h3geo.org/docs/comparisons/geohash/)</sup>。若服务以固定精度承诺“500 米内”，则低纬度需要更长的哈希，极地附近需要更短的哈希，才能获得等效覆盖<sup>[10](https://ky-gis.com/en/blog/h3-vs-geohash-vs-s2)</sup>。

**边界问题。** 共享前缀通常意味着邻近，但反过来不成立：两个点可能相距数米，却位于单元格边界两侧，哈希完全不同<sup>[3](https://firebase.google.com/docs/firestore/solutions/geoqueries)</sup>。因此半径查询不能只匹配一个哈希。标准做法是探测中心单元格及其周围一圈相邻单元格——对 Geohash 而言即该单元格及其八个邻居——再按精确距离过滤候选<sup>[11](https://www.edge-geospatial.org/local-spatial-processing-patterns/spatial-indexing-on-constrained-devices/h3-vs-geohash-cell-indexing-for-zone-lookups/)</sup>。圆形搜索区域最多可能与 9 个不同的 Geohash 单元格重叠<sup>[12](https://juliomarquez.com/blog/geospatial-nosql-geohash-implementation)</sup>，Firebase 的 Geohash 查询辅助工具每次半径搜索最多发出 9 对范围查询，通常为 4 对<sup>[3](https://firebase.google.com/docs/firestore/solutions/geoqueries)</sup>。

**误报。** 按 Geohash 查询是近似的，因此必须在客户端按真实距离过滤结果；这些额外读取会增加成本和延迟，而在经线距离估计变差的极地纬度，问题会更严重<sup>[3](https://firebase.google.com/docs/firestore/solutions/geoqueries)</sup>。

## 与 S2 和 H3 的比较

Geohash 属于离散全球网格系统家族，该家族还包括谷歌的 S2 和优步的 H3，三者常被比较<sup>[6](https://www.usenix.org/system/files/usenixsecurity24-guo-ruoyang.pdf)</sup>。结构差异源于单元格形状和细分方式：Geohash 将每个单元格分为 32 个（每字符 5 比特），S2 分为 4 个，H3 分为 7 个<sup>[13](https://note.com/pacificspatial/n/nba664736279a?hl=en)</sup>。

- **单元格形状与邻居。** Geohash 在 Z 序曲线上使用矩形单元格；S2 在[希尔伯特曲线](https://www.edgepedia.org/zh/hilbert-curve)上使用正方形单元格；H3 使用六边形<sup>[14](https://www.beyonddataconsulting.io/blog/geospatial-indexing-geohash-s2-h3)</sup>。正方形有两类邻居——共边和共角——这使移动分析复杂化；六边形只有共边邻居，且距离都相等，这简化了邻域和流动计算<sup>[15](https://h3geo.org/docs/comparisons/s2/)</sup>。希尔伯特曲线还使 S2 比 Geohash 的 Z 序曲线具有更好的局部性，即邻近点更一致地获得邻近的单元格标识符<sup>[16](https://www.systemoverflow.com/learn/geospatial/geohashing/trade-offs-geohash-vs-h3-vs-s2-in-production)</sup>。
- **标识符格式。** Geohash 使用字符串，可编码任意精度的单元格；S2 和 H3 使用 64 位整数，在大数据系统中更快，但限制了最大分辨率<sup>[7](https://h3geo.org/docs/comparisons/geohash/)</sup>。Geohash 仅通过长度携带精度<sup>[17](https://spatialworkflow.io/discrete-global-grids-explained/)</sup>。
- **实际定位。** 一项比较评估得出结论：S2 适合键必须能排序进数据库索引并高效覆盖某一范围的情形；H3 适合从点向外扩展、需要各向同性邻居的分析；Geohash 适合需要人直接读取键的情形<sup>[18](https://www.3d-geospatial.com/3d-geospatial-fundamentals-for-digital-twins/spatial-indexing-and-tiling-schemes/choosing-between-s2-h3-and-geohash-for-3d-data/)</sup>。Geohash 的简单性和库的普及性仍是其主要优势<sup>[14](https://www.beyonddataconsulting.io/blog/geospatial-indexing-geohash-s2-h3)</sup>。

## 用法

前缀性质使 Geohash 天然适合普通数据库索引。邻近服务将坐标映射为 Geohash 字符串，运行 B 树前缀或范围查询以缩小候选集，然后在应用代码中应用精确距离过滤<sup>[19](https://www.abstractalgorithms.dev/system-design-hld-proximity-service-example)</sup>。Firebase 的 Firestore 文档介绍的正是这种使用单个带索引 Geohash 字段进行基于位置查询的模式<sup>[3](https://firebase.google.com/docs/firestore/solutions/geoqueries)</sup>，而 OpenSearch 等搜索引擎提供 Geohash 网格聚合，按 Geohash 前缀将文档分桶<sup>[20](https://docs.opensearch.org/latest/aggregations/bucket/geohash-grid/)</sup>。Transitland 将 Geohash 用作其分配给公共交通运营商、线路和站点的稳定 Onestop ID 的组成部分<sup>[21](https://www.transit.land/news/2015/06/05/geohashes-and-you.html)</sup>。

研究人员为特定工作负载提出了变体。统计学家泰勒·阿诺德（Taylor Arnold）于 2015 年提出一种熵最大化 Geohash，对于给定哈希前缀长度，数据量近似恒定，从而在数据密度不同的区域之间平衡分布式时空数据库的负载<sup>[22](https://arxiv.org/abs/1506.05158)</sup>。

## References

1. [phrozen/geohash: Simple implementation of Gustavo Niemeyer's algorithm, GitHub](https://github.com/phrozen/geohash)
2. [Patents and advancing art in geographic coordinates, Cartographica Blog](https://blog.cartographica.com/patents-and-advancing-art-in-geographic-coordinates.html)
3. [Geo queries, Firebase Firestore documentation](https://firebase.google.com/docs/firestore/solutions/geoqueries)
4. [Geohash Explained, Coordinately](https://www.coordinately.org/learn/geohash-explained)
5. [nesterenko-kv/geohash: Geohash.NET library, GitHub](https://github.com/nesterenko-kv/geohash)
6. [GridSE: Towards Practical Secure Geographic Search via Prefix Symmetric Searchable Encryption, USENIX Security 2024](https://www.usenix.org/system/files/usenixsecurity24-guo-ruoyang.pdf)
7. [Geohash, H3 documentation comparisons](https://h3geo.org/docs/comparisons/geohash/)
8. [ESREL-SRA-E2025 proceedings paper on geohash precision](https://rpsonline.com.sg/proceedings/esrel-sra-e2025/pdf/ESREL-SRA-E2025-P6110.pdf)
9. [Esa Lempiäinen, thesis, Tampere University](https://trepo.tuni.fi/bitstream/handle/10024/134662/Lempi%C3%A4inenEsa.pdf?isAllowed=y&sequence=2)
10. [H3 vs Geohash vs S2: Choosing the Right Spatial Index, KunYu](https://ky-gis.com/en/blog/h3-vs-geohash-vs-s2)
11. [H3 vs geohash cell indexing for zone lookups, Edge Geospatial](https://www.edge-geospatial.org/local-spatial-processing-patterns/spatial-indexing-on-constrained-devices/h3-vs-geohash-cell-indexing-for-zone-lookups/)
12. [Geospatial Queries in NoSQL: Precise Location Search, Julio Marquez](https://juliomarquez.com/blog/geospatial-nosql-geohash-implementation)
13. [Introduction to GIS and Spatial Indexing, Part 6: Comparing H3, S2, and Geohash, Pacific Spatial Solutions](https://note.com/pacificspatial/n/nba664736279a?hl=en)
14. [Geospatial Indexing Explained: A Comparison of Geohash, S2, & H3, Beyond Data Consulting](https://www.beyonddataconsulting.io/blog/geospatial-indexing-geohash-s2-h3)
15. [S2, H3 documentation comparisons](https://h3geo.org/docs/comparisons/s2/)
16. [Trade Offs: Geohash vs H3 vs S2 in Production, System Overflow](https://www.systemoverflow.com/learn/geospatial/geohashing/trade-offs-geohash-vs-h3-vs-s2-in-production)
17. [Discrete Global Grids Explained, Spatial Workflow](https://spatialworkflow.io/discrete-global-grids-explained/)
18. [Choosing Between S2, H3 and Geohash for 3D Data, 3D Geospatial](https://www.3d-geospatial.com/3d-geospatial-fundamentals-for-digital-twins/spatial-indexing-and-tiling-schemes/choosing-between-s2-h3-and-geohash-for-3d-data/)
19. [System Design HLD Example: Proximity Service, Abstract Algorithms](https://www.abstractalgorithms.dev/system-design-hld-proximity-service-example)
20. [Geohash grid aggregation, OpenSearch Documentation](https://docs.opensearch.org/latest/aggregations/bucket/geohash-grid/)
21. [Geohashes and You, Transitland](https://www.transit.land/news/2015/06/05/geohashes-and-you.html)
22. [Taylor Arnold (2015). An Entropy Maximizing Geohash for Distributed Spatiotemporal Database Indexing. arXiv.](https://arxiv.org/abs/1506.05158)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 21, 2026 · Edited: Oct 15, 2026; Oct 21, 2026 · Last review: Oct 21, 2026*

Topic: 地点与地理 › 地理学总论与地理参考资料 › 坐标与地理编码

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/geohash

English original: https://www.edgepedia.org/geohash
