Introduction
Machine is a good move and studious children, on weekdays like to take a mobile phone map point Click to query some fun things. One day the machine to the Beihai Park to play, belly hungry, and then opened the mobile phone map, search for restaurants near Beihai parks, and selected one of the meals.
After the meal, the machine began to reflect on the map backstage how to query the location of the nearby restaurants. After a long thought, the machine came up with a method: calculate the distance between the location p and all the restaurants in Beijing, and then return to the restaurant <=1000 meters away. Small proud for a while, the machine found Beijing restaurant How many ah, so calculate, so think, since know latitude and longitude, then it should know oneself in Xicheng District, that should calculate location p and Xicheng District all Restaurants distance Ah, machine use recursive thought, think of Xicheng District also many restaurants Ah, The distance between the location p and all restaurants in the street should be calculated so that the calculation is small and the efficiency is increased.
The calculation of the machine is very simple, that is, through the filtering method to reduce the number of restaurants participating in the calculation, from a certain point of view, the machine in the use of indexing technology.
As soon as the index is mentioned, the B-tree index is emerging in the brain, because a large number of databases (such as MySQL, Oracle, PostgreSQL, and so on) are using B-trees. The B-tree index essentially sorts the indexed fields, and then finds them quickly by means of a binary-like lookup, which requires that the indexed fields be sortable, in general, one-dimensional fields, such as time, age, salary, and so on. But for a point in space (two-dimensional, including longitude and latitude), how to sort it. And how to index it. There are a number of ways to solve this problem.
Thought: If you could somehow convert two-dimensional point data into one-dimensional data, then you wouldn't be able to continue using the B-Tree index. So this method really exists, the answer is yes. At present the very fire Geohash algorithm is to use the above thought, below we will begin the Geohash journey.
First, perceptual Geohash
First of all, a bit of perceptual knowledge, http://openlocation.org/geohash/geohash-js/provides the ability to display Geohash encoding on the map.
1) Geohash The two-dimensional latitude and longitude into a string, such as the following figure shows the Geohash string in Beijing 9 regions, respectively, WX4ER,WX4G2, WX4G3, and so on, each of which represents a rectangular region. That is, all the points in the rectangular region (latitude and longitude coordinates) share the same Geohash string, which can protect privacy (only the approximate area location rather than the specific point), but also easier to do cache, such as the upper left corner of the area where users constantly send location information to request restaurant data, Since these users ' geohash strings are wx4er, wx4er can be used as a key to cache the restaurant information in the area as value, and if Geohash is not used, it is difficult to cache because the latitude and longitude of the users in the region are different.
2) The longer the string, the more accurate the range represented. As shown in the figure, the 5-bit encoding can represent a rectangular region of 10 square kilometers, while a 6-bit encoding can represent a finer area (about 0.34 km²)
3) Similar representations of strings are close (described in special cases) so that the prefix matching of strings can be used to query nearby POI information. As shown in the following two figures, one is similar between the Geohash strings in urban areas, one in the suburbs, and in the suburbs, while the suburban strings are similar, while the Geohash strings in urban and suburban areas are much less similar.
Through the above introduction we know that Geohash is a way to convert latitude and longitude into a string, and in most cases, the more the string prefix matches the closer the distance, back to our case, according to the location of the query to find nearby restaurants, You only need to convert the latitude and longitude of the location into a geohash string, and match the Geohash string of the individual restaurants to match the more distance.
Ii. steps of the Geohash algorithm
Below take Beihai Park as an example to introduce the calculation steps of Geohash algorithm
2.1. Calculate Geohash binary code based on latitude and longitude
Latitude of the earth is [ -90,90], North Sea Park latitude is 39.928167, can be approximated by the following algorithm to the latitude 39.928167 code:
1) the interval [ -90,90] is divided into [ -90,0], [0,90], known as the left and right intervals, can be determined that 39.928167 belongs to an interval [0,90], to mark 1;
2) then the interval [0,90] is divided into [0,45], [45,90], can be determined that 39.928167 belongs to the left interval [0,45], to mark 0;
3) recursion The above process 39.928167 always belongs to a certain interval [a, b]. As each iteration interval [a, b] is always shrinking and approaching 39.928167 more and more;
4) If the given latitude X (39.928167) belongs to the left interval, then record 0, if the right interval is recorded 1, so as the algorithm will produce a sequence 1011100, the length of the sequence is related to the number of intervals given.
According to the Latitude calculation code
Bit |
Min |
Mid |
Max |
1 |
-90.000 |
0.000 |
90.000 |
0 |
0.000 |
45.000 |
90.000 |
1 |
0.000 |
22.500 |
45.000 |
1 |
22.500 |
33.750 |
45.000 |
1 |
33.7500 |
39.375 |
45.000 |
0 |
39.375 |
42.188 |
45.000 |
0 |
39.375 |
40.7815 |
42.188 |
0 |
39.375 |
40.07825 |
40.7815 |
1 |
39.375 |
39.726625 |
40.07825 |
1 |
39.726625 |
39.9024375 |
40.07825 |
Similarly, the longitude range of the earth is [-180,180], and longitude 116.389550 can be encoded.
According to the Longitude calculation code
Bit |
min |
Mid |
Max |
1 |
-180 |
0.000 |
|
1 |
0.000 |
All |
/td> |
0 |
All |
135 |
|
1 |
Up |
112.5 |
135 |
0 |
112.5 |
123.75 |
135 |
0 |
112.5 |
118.125 |
123.75 |