A simple library that enables Morton coding/hashing with built-in geo-hashing support.
Interleaved bits (aka Morton numbers) are useful for linearizing K-dimensional integer coordinates, so x, y ... K
are combined into a single number that is easily compared, searched, and indexed in addition to being ordinally close to a Morton number that represents a point nearby to x, y ... K
.
Useful references:
- Implementation for the algorithm (1)
- Implementation for the algorithm (2)
- Extended explanation with different algorithms
pip install pymorton
or
git clone https://github.com/trevorprater/pymorton.git
cd pymorton
python setup.py install
From the root directory, execute nosetests
.
- geo-hashing
import pymorton as pm
geohash = pm.interleave_latlng(40.723471, -73.985361) # returns '03023211233202130332202203002303'
pm.deinterleave_latlng(geohash) # returns (40.723470943048596, -73.98536103777587)
- 3D-hashing
import pymorton as pm
mortoncode = pm.interleave(100, 200, 50) # returns 5162080
# or
mortoncode = pm.interleave3(100, 200, 50) # returns 5162080
pm.deinterleave3(mortoncode) # returns (100, 200, 50)
- 2D-hashing
import pymorton as pm
mortoncode = pm.interleave(100, 200) # returns (46224)
# or
mortoncode = pm.interleave2(100, 200) # returns (46224)
pm.deinterleave2(mortoncode) # returns (100, 200)
-
pymorton.interleave(*args)
- Hashes
x, y
orx, y, z
into a single value. This function wraps interleave2() and interleave3() by supporting variable-length args.
- Hashes
-
pymorton.interleave2(x, y)
- Returns a hash (int) representing
x, y
.
- Returns a hash (int) representing
-
pymorton.interleave3(x, y, z)
- Returns a hash (int) representing
x, y, z
.
- Returns a hash (int) representing
-
pymorton.interleave_latlng(lat, lng)
- Returns a hash (string base-4)
representing
lat, lng
.
- Returns a hash (string base-4)
representing
-
pymorton.deinterleave2(hash)
- Returns a tuple representing the arguments to the corresponding interleave2() call.
-
pymorton.deinterleave3(hash)
- Returns a tuple representing the arguments to the corresponding interleave3() call.
-
pymorton.deinterleave_latlng(hash)
- Returns a tuple representing the arguments to the corresponding interleave_latlng() call.
MIT