How to decode geohash using python in pandas?

Question:

I need code to decode geohash in python. There’s a column which contains geohashes. I need them decoded into latitude and longitude.

Asked By: Saeed Ahmad

||

Answers:

You can install pygeohash from pypi using pip

$ pip install pygeohash

Then add a new column to the dataframe with the latitude and longitude

import pygeohash as pgh
# ...
# location is a new column filled with (lat, lon) tuples
df['location'] = df.apply(lambda rec: pgh.decode(rec['geohash']), axis=1)

Here 'geohash' is the column which contains geohashes.

Answered By: Gabriel Bustamante

this solution is performant:

import pygeohash 
import numpy as np 

def dehashingit(x): 
    """
    this returns a tuple, where at index 0 is the lats and at index 1 is the longs
    """
    return pygeohash.decode(x)

func = np.vectorize(dehashingit) 

location = func(df.geohash.values)

df['lat'], df['long'] = location[0], location[1]
Answered By: hatim bennis