All Projects → trevorprater → Pymorton

trevorprater / Pymorton

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

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Pymorton

js-algorithms
Computer science
Stars: ✭ 64 (+16.36%)
Mutual labels:  sorting-algorithms, search-algorithm
Data processor
数据algorithm & 分析算法
Stars: ✭ 83 (+50.91%)
Mutual labels:  sorting-algorithms, image-processing
Data Structure And Algorithms With Es6
Data Structures and Algorithms using ES6
Stars: ✭ 594 (+980%)
Mutual labels:  sorting-algorithms, search-algorithm
Algorithms
A collection of algorithms and data structures
Stars: ✭ 11,553 (+20905.45%)
Mutual labels:  sorting-algorithms, search-algorithm
Algods
Implementation of Algorithms and Data Structures, Problems and Solutions
Stars: ✭ 3,295 (+5890.91%)
Mutual labels:  sorting-algorithms, search-algorithm
Ruby.fundamental
📚 Fundamental programming with ruby examples and references. It covers threads, SOLID principles, design patterns, data structures, algorithms. Books for reading. Repo for website https://github.com/khusnetdinov/betterdocs
Stars: ✭ 391 (+610.91%)
Mutual labels:  sorting-algorithms, search-algorithm
Algorithm Notes
Comprehensive algorithms solution to help engineers prepare their interviews and future study
Stars: ✭ 44 (-20%)
Mutual labels:  sorting-algorithms, search-algorithm
Seeds Revised
Implementation of the superpixel algorithm called SEEDS [1].
Stars: ✭ 48 (-12.73%)
Mutual labels:  image-processing
Geeksforgeeks Dsa 2
This repository contains all the assignments and practice questions solved during the Data Structures and Algorithms course in C++ taught by the Geeks For Geeks team.
Stars: ✭ 53 (-3.64%)
Mutual labels:  sorting-algorithms
Inpainting
Criminisi et al's region inpainting algorithm in C++
Stars: ✭ 46 (-16.36%)
Mutual labels:  image-processing
Nimp
Nimp - Node-based image manipulation program.
Stars: ✭ 45 (-18.18%)
Mutual labels:  image-processing
Facer
Simple (🤞) face averaging (🙂) in Python (🐍)
Stars: ✭ 49 (-10.91%)
Mutual labels:  image-processing
Pixelizator
Swift/Python image pixelizer 🖼️.
Stars: ✭ 53 (-3.64%)
Mutual labels:  image-processing
Op rbf
Optimized Recursive Bilateral Filter
Stars: ✭ 47 (-14.55%)
Mutual labels:  image-processing
Imgart
🎨 IMGART it's a simple, fast and reliable HTTP service for image processing based on filters and profiles
Stars: ✭ 55 (+0%)
Mutual labels:  image-processing
Java Thumbnailer
An extensible java library to create thumbnails of different file types (image, text)
Stars: ✭ 45 (-18.18%)
Mutual labels:  image-processing
Batchimageprocessor
A Mass Image Processing tool for Windows
Stars: ✭ 55 (+0%)
Mutual labels:  image-processing
Bisweb
This is the repository for the BioImage Suite Web Project
Stars: ✭ 54 (-1.82%)
Mutual labels:  image-processing
Opencv Face Filters
Snapchat-like Face Filters in OpenCV
Stars: ✭ 51 (-7.27%)
Mutual labels:  image-processing
Gait Recognition
Distance Recognition of a Human Being with Deep CNN's
Stars: ✭ 51 (-7.27%)
Mutual labels:  image-processing

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].