Outline

  1. What is the GeoHash Algorithm and why do we need it?
  2. How did the GeoHash split the world into a hash array
  3. The calculating procedure
  4. Verify the result

Tutorial

1. What is the GeoHash Algorithm and why do we need it?

Geohash is a geographic location coding algorithm that leads us dealing the location task which is popular in many applications and service like LBS or so on, and make it efficiency.

Let’s discuss an application about when you calling a taxi to go somewhere. It’s easy to think that the app service would send a message or request to the nearest driver who could serves you as soon as possible.

Generally, you refresh your location with your country, city and geographic coordinates and let the service known where you are. And the taxi driver would also do this process to upload their position. So, the service in background could calculates the distance between you and the drivers, finding the nearest one to contact you. Quite simple, isn’t it?

Further, let’s put us hands dirty in the process above and check the efficiency. First, we generate 100,001 coordinates in longitude and latitude. One of these is your position, and the rest of these are the driver’s. I put our Python script below so you could copy it to your own .ipynb file and go ahead.

1
2
3
4
5
6
7
8
9
import numpy as np

# Simulate a position as yours
your_pos = [np.random.uniform(115, 125),np.random.uniform(20, 25)]

# Create 100,000 taxicabs
size = 10000000
drivers_pos = [[np.random.uniform(115, 125),np.random.uniform(20, 25)] for i in range(0,size)]

Here, we have prepared the simulation data and be ready for the next step. In this step, we will find out 10 drivers near you. So, let’s do it by the codes below.

1
2
3
4
5
import math

# define a function to calcuate the distance in simply way
def simple_distance(lon_a, lat_a, lon_b, lat_b):
return math.sqrt((lon_a - lon_b)**2 + (lat_a - lat_b)**2)

2. How did the GeoHash split the world into a hash array?

3. The calculating procedure

4. Verify the result

References & Documentations

GeoHash Wikipedia

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

GeoHash Explorer

https://geohash.softeng.co/

本文采用CC-BY-SA-3.0协议,转载请注明出处
Author: 徐奕峰 Eephone Xu