GithubHelp home page GithubHelp logo

trevorprater / pymorton Goto Github PK

View Code? Open in Web Editor NEW
106.0 5.0 21.0 62 KB

A lightweight and efficient Python Morton encoder with support for geo-hashing

License: MIT License

Python 100.00%
nearest-neighbors bit-interleaving sorting-algorithms geospatial-analysis morton-code z-order quadtree image-processing hashing-algorithm dimensionality-reduction

pymorton's Introduction

pymorton

Ordinal hashing of multidimensonal data and geographic coordinates via Morton coding / Z-ordering.

Codecov Travis Status GitHub tag License

In mathematical analysis and computer science, Z-order, Morton-order, or a Morton-code is a function which maps multidimensional data to one dimension while preserving locality of the data points. It was introduced in 1966 by IBM researcher, G. M. Morton. The z-value of a point in multidimensions is calculated by interleaving the binary representations of its coordinate values. Once the data are sorted into this ordering, any one-dimensional data structure can be used, such as binary search trees, B-trees, skip lists, or hash tables. The resulting ordering can equivalently be described as the order one would achieve from a depth-first traversal of a quadtree, where {x, y, ..., K} are combined into a single ordinal value that is easily compared, searched, and indexed against other Morton numbers.

At the highest level, pymorton is split into two logical functions:

  • (de)interleave: encodes/decodes hashes representing two or three dimensionsal integer sets. {x, y, z โˆˆ Z} or {x, y โˆˆ Z}, where Z represents all integer values.

  • (de)interleave_latlng: encodes and decodes hashes representing latitude and longitude.

Example usage scenario:

  • Given a directory of images, sort the images by color (average RGB):

    from statistics import mean
    from glob import glob
    from PIL import Image
    import pymorton
    
    imgs = [(fname, Image.open(fname)) for fname in glob('imgpath/*.jpg')[:100]]
    
    # for each image, generate a tuple of len==3, representing the image's average RGB value
    avg_rgb_values = [
        [int(mean(img.getdata(band))) for band in range(3)] for _, img in imgs]
    
    # using the average RGB values, compute the Z-order of each image
    hashed_imgs = list(zip([fname for fname, _ in imgs],
                       [pymorton.interleave(*avg_rgb) for avg_rgb in avg_rgb_values]))
    
    # returns a sorted-by-color list of photos found within the directory
    return sorted(hashed_imgs, key=lambda img_tuple: img_tuple[1])

While the above use-case is fairly uncommon in the context of Morton-coding, I believe it illustrates the utility of the algorithm quite well. Morton-coding is most commonly used within the realm of geospatial indexing, but its potential applications are infinite!

Installation

via pip:

pip install pymorton

via source:

git clone https://github.com/trevorprater/pymorton.git
cd pymorton
python setup.py install

Usage

  • 3D-hashing
import pymorton as pm

mortoncode = pm.interleave(100, 200, 50)  # 5162080
mortoncode = pm.interleave3(100, 200, 50) # 5162080

pm.deinterleave3(mortoncode)              # (100, 200, 50)
  • 2D-hashing
import pymorton as pm

mortoncode = pm.interleave(100, 200)     # 46224
mortoncode = pm.interleave2(100, 200)    # 46224

pm.deinterleave2(mortoncode)             # (100, 200)
  • geo-hashing
import pymorton as pm

geohash = pm.interleave_latlng(40.723471, -73.985361)     # '03023211233202130332202203002303'

pm.deinterleave_latlng(geohash)                           # (40.723470943048596, -73.98536103777587)

API

  • pymorton.interleave(*args)

    • Hashes x, y or x, y, z into a single value. This function wraps interleave2() and interleave3() by supporting variable-length args.
  • pymorton.interleave2(x, y)

    • Returns a hash (int) representing x, y.
  • pymorton.interleave3(x, y, z)

    • Returns a hash (int) representing x, y, z.
  • pymorton.interleave_latlng(lat, lng)

    • Returns a hash (string base-4) representing lat, lng.
  • 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.

Tests

From the project's root directory, execute nosetests.

Please feel free to contact [email protected] regarding any questions/comments/issues.

References:

License

MIT

pymorton's People

Contributors

hatesmeetings avatar smatsumt avatar trevorprater avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pymorton's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.