ClickHouse/docs/en/sql-reference/functions/geo/geohash.md
olgarev e2287959a8
DOCSUP-1585: Edit and sync geo functions (#12584)
* Geo functions - en/ru sync.

* Fixed bug with levels.

* Apply suggestions from code review

Co-authored-by: Ivan Blinkov <github@blinkov.ru>
Co-authored-by: BayoNet <da-daos@yandex.ru>

* Geo functions splitted into 3 files.

* Links fixed.

* Link fixed.

* Anchor fixed.

* Add into TOC.

* Temporarily removed anchors.

* Working on TOC.

* Links to original article.

* TOC and links in English and Russian.

* TOC in English, content in index.

* Link fixed.

* TOC in Russian, with content in index file.

* Added index file to geo functions

* fixed links in ru

Co-authored-by: Olga Revyakina <revolg@yandex-team.ru>
Co-authored-by: Ivan Blinkov <github@blinkov.ru>
Co-authored-by: BayoNet <da-daos@yandex.ru>
Co-authored-by: Sergei Shtykov <bayonet@yandex-team.ru>
2020-07-28 10:57:28 +03:00

4.0 KiB
Raw Blame History

toc_title
Geohash

Functions for Working with Geohash

Geohash is the geocode system, which subdivides Earths surface into buckets of grid shape and encodes each cell into a short string of letters and digits. It is a hierarchical data structure, so the longer is the geohash string, the more precise is the geographic location.

If you need to manually convert geographic coordinates to geohash strings, you can use geohash.org.

geohashEncode

Encodes latitude and longitude as a geohash-string.

geohashEncode(longitude, latitude, [precision])

Input values

  • longitude - longitude part of the coordinate you want to encode. Floating in range[-180°, 180°]
  • latitude - latitude part of the coordinate you want to encode. Floating in range [-90°, 90°]
  • precision - Optional, length of the resulting encoded string, defaults to 12. Integer in range [1, 12]. Any value less than 1 or greater than 12 is silently converted to 12.

Returned values

  • alphanumeric String of encoded coordinate (modified version of the base32-encoding alphabet is used).

Example

SELECT geohashEncode(-5.60302734375, 42.593994140625, 0) AS res
┌─res──────────┐
│ ezs42d000000 │
└──────────────┘

geohashDecode

Decodes any geohash-encoded string into longitude and latitude.

Input values

  • encoded string - geohash-encoded string.

Returned values

  • (longitude, latitude) - 2-tuple of Float64 values of longitude and latitude.

Example

SELECT geohashDecode('ezs42') AS res
┌─res─────────────────────────────┐
│ (-5.60302734375,42.60498046875) │
└─────────────────────────────────┘

geohashesInBox

Returns an array of geohash-encoded strings of given precision that fall inside and intersect boundaries of given box, basically a 2D grid flattened into array.

Syntax

geohashesInBox(longitude_min, latitude_min, longitude_max, latitude_max, precision)

Parameters

  • longitude_min — Minimum longitude. Range: [-180°, 180°]. Type: Float.
  • latitude_min — Minimum latitude. Range: [-90°, 90°]. Type: Float.
  • longitude_max — Maximum longitude. Range: [-180°, 180°]. Type: Float.
  • latitude_max — Maximum latitude. Range: [-90°, 90°]. Type: Float.
  • precision — Geohash precision. Range: [1, 12]. Type: UInt8.

!!! info "Note" All coordinate parameters must be of the same type: either Float32 or Float64.

Returned values

  • Array of precision-long strings of geohash-boxes covering provided area, you should not rely on order of items.
  • [] - Empty array if minimum latitude and longitude values arent less than corresponding maximum values.

Type: Array(String).

!!! info "Note" Function throws an exception if resulting array is over 10000000 items long.

Example

Query:

SELECT geohashesInBox(24.48, 40.56, 24.785, 40.81, 4) AS thasos

Result:

┌─thasos──────────────────────────────────────┐
│ ['sx1q','sx1r','sx32','sx1w','sx1x','sx38'] │
└─────────────────────────────────────────────┘

Original article