Analysis of GeoHash principles

Source: Internet
Author: User
Analysis of the core principle of GeoHash

 

 

 

Introduction

 

 

 

When it comes to indexes, the B-tree index is immediately displayed in our minds, because a large number of databases (such as MySQL, oracle, and PostgreSQL) are using B-tree. In essence, the B-tree index sorts the index fields and then performs a quick search using a method similar to binary search. That is, it requires that the index fields are sortable. Generally, one-dimensional fields can be sorted, such as time, age, and salary. But how do we sort spatial points (two-dimensional, including longitude and latitude? How to index it? There are many solutions. The following describes a method to solve this problem.

 

Thoughts:If you can use a methodConverts two-dimensional point data into one-dimensional data.So that we can continue to use the B-tree index. The answer is yes. Currently, the popular GeoHash algorithm uses the above ideas. Let's start the journey of GeoHash.

 

 

 

 

1. Perceptual knowledge GeoHash

 

First of all, let's look at the perceptual knowledge. The http://openlocation.org/geohash/geohash-js/ provides the function of displaying geohash code on the map.

 

 

1) GeoHash converts two-dimensional longitude and latitude to a string. For example, it shows the GeoHash strings of nine regions in Beijing, including WX4ER, WX4G2, and WX4G3. Each string represents a rectangular area. That is to say, all vertices (longitude and latitude coordinates) in the rectangle area share the same GeoHash string.Privacy protection (only indicating the location of the region rather than the specific point), and it is easier to cacheFor example, users in this region in the upper left corner constantly send location information to request restaurant data. Because these users use the GeoHash string WX4ER, you can use WX4ER as the key, the restaurant information in the region is cached as value. If GeoHash is not used, it is difficult to cache the restaurant information because the longitude and latitude transmitted by users in the region are different.

 

 

 

2) the longer the string, the more accurate the range is ., The five-digit encoding can represent a rectangular area in the range of 10 square kilometers, while the six-digit encoding can represent a more refined area (about 0.34 square kilometers)

 

 

 

3) similar strings indicate close distances (in special cases, this article describes). In this way, you can use string prefix matching to query nearby POI information. As shown in the following two figures, the GeoHash strings in the urban area, the suburban area, and the suburban areas are similar, the GeoHash string in the city and suburb is less similar.

 

 

 

 

 

 

 

Through the above introduction, we know that GeoHash is a method to convert longitude and latitude to a string, and in most cases, the closer the string prefix matches, the closer it returns to our case, when querying the location of the root data to query nearby restaurants, you only need to convert the longitude and latitude of the location into a GeoHash string and match the GeoHash string of each restaurant with the prefix. The closer the matching distance is, the closer the matching distance is.

 

 

 

 

II,GeoHash algorithm

 

A Geohash uses a LineString to scan a two-dimensional space, and the movement in the traversal can be expressed by 0 and 1 in a simple way, then, 0/1 strings are generated during the moving process. Shows this algorithm: (Chen Hao note: divide the map into four parts, with longitude as the first and latitude as the second, so the longitude on the left is 0, and the right is 1, the same is true for the latitude and longitude. The preceding value is 1 and the following value is 0. In this way, the longitude and latitude values can be combined into the values, and 10, which identify the four regions, we can perform a four-point recursion for each region so that we can obtain a string consisting of 1 and 0, and then remove it with 0-9 and B-z (remove, i, l, o) these 32 letters are base32 encoded to get an 8-length encoding, which is the Geohash algorithm)

 

The most powerful function of Geohash is to use a simple bit operation to know the distance between the two regions, as shown in the figure (Chen Hao: proximity boxes the two, this is like an IP address ). Geohash converts a two-dimensional coordinate into a one-dimensional data model, which is the dimensionality reduction technology.

 


Geohash Index

 

 

 

The advantage of this type of spatial fill curve is that it converts two-dimensional space into one-dimensional curve (in fact, it is a fragtal dimension). For most of them, the distance of the encoding is similar, however, the biggest drawback of the Peano space filling curve is the mutation. Some codes are adjacent, but the distance is quite different. For example, 0111 and 1000 are adjacent to each other, but the distance is quite different.

 

 

 

In addition to the Peano space filling curve, there are also many space filling curves. The better effect is recognized as the Hilbert space filling curve. Compared with the Peano curve, the Hilbert curve has no major mutation. Why does GeoHash choose not the Hilbert space fill curve? It may be because the Peano curve is simple in thinking and computing. In fact, the Peano curve is a four-tree linear coding method.

 

 

 

 

 

III. GeoHash Base32 encoding length and accuracy

 

It can be seen that when the geohash base32 encoding length is 8, the precision is about 19 meters, while when the encoding length is 9, the precision is about 2 meters, the encoding length needs to be selected based on data conditions.

 

From Wikipedia: http://en.wikipedia.org/wiki/Geohash

 

 

 

 

4. Usage notes

 

1) GeoHash divides the region into regular rectangles and encodes each rectangle. This will cause the following problems when querying the POI information nearby, for example, the red dots are our locations, and the green dots are two nearby restaurants, however, when querying, we will find that the GeoHash code of a distant restaurant is the same as ours (because it is in the same GeoHash region), while the GeoHash code of a closer restaurant is different from ours. This problem often occurs at the boundary.

 

The solution is simple. In addition to the GeoHash encoding of the positioning point, we also use the GeoHash encoding of the eight surrounding regions to avoid this problem.

 

 

2) We already know that the existing GeoHash algorithm uses the Peano space fill curve, which will produce abrupt changes, resulting in the problem that although the encoding is similar, the distance may be very different, therefore, when querying a nearby restaurant, filter the POI points with similar GeoHash codes and calculate the actual distance.

 

 

 

 

References:

Http://en.wikipedia.org/wiki/Geohash

Http://openlocation.org/geohash/geohash-js/

How can I find the cantorempty box filling method?

 

 

 

 

 

 

  • Geohash: using strings to achieve nearby location search http://tech.idv2.com/2011/07/05/geohash-intro/

  • GeoHash core principle analysis http://www.cnblogs.com/LBSer/p/3310455.html

  • NoSQL data modeling http://coolshell.cn/articles/7270.html

 

Analysis of GeoHash principles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.