geohash

| 分类 技术  | 标签 业务开发 

what is Geohash?

From Wikipedia:

Geohash is a public domain geocoding system invented by Gustavo Niemeyer, which encodes a geographic location into a short string of letters and digits. It is a hierarchical spatial data structure which subdivides space into buckets of grid shape, which is one of the many applications of what is known as a Z-order curve, and generally space-filling curves.

Geohashes offer properties like arbitrary precision and the possibility of gradually removing characters from the end of the code to reduce its size (and gradually lose precision). As a consequence of the gradual precision degradation, nearby places will often (but not always) present similar prefixes. The longer a shared prefix is, the closer the two places are.

Uses

From Wikipedia:

The main usages of Geohashes are:

  • As a unique identifier.
  • To represent point data, e.g. in databases.

Geohashes have also been proposed to be used for geotagging.

When used in a database, the structure of geohashed data has two advantages. First, data indexed by geohash will have all points for a given rectangular area in contiguous slices (the number of slices depends on the precision required and the presence of geohash “fault lines”). This is especially useful in database systems where queries on a single index are much easier or faster than multiple-index queries. Second, this index structure can be used for a quick-and-dirty proximity search: the closest points are often among the closest geohashes.

Now, we use geohash as cache of geographic location, which is used for a quick-and-dirty proximity search: the closest points are often among the closest geohashes.

Geohash cache

As shown above, we choose geohash length:6 to ensure an acceptable level of geographical error.

It is easy to generate a geohash cache system if you follow these steps:

  • 1.geohash encode geo-location information in the forms of latitude-longitude
  • 2.search geohash generated by first step in Key-value database
  • 3.get GPS information if it exists
  • 4.cache GPS information queried by GIS if it does not exist
// step1: geohash encode geo-location information in the forms of latitude-longitude
geohash = geohash_encode(latitude, longitude, 6);
// step2: search geohash generated by first step in Key-value database
exist = XXX.Get(geohash, GPS-info);
// step 3: get GPS information if it exists
if (exist)
{
    return GPS-info;
}
else
{
    // step4: cache GPS information queried by GIS if it does not exist
    GPS-info = GIS(latitude, longitude);
    XXX.Set(geohash, GPS-info);
}

Refs


上一篇     下一篇