Skip to main content

An implementation of Haversine distance calculations using Rust and PyO3

Project description

fast-geodist

PyPI CI Release Code Style

An implementation of the Haversine formula for calculating Great Circle distance using Rust and PyO3.

This package was started as an experiment for learning how to implement functions in Rust and wrap them for use in python. The Haversine Formula is implemented following georust, with an array-wise implementation written using ndarray::parallel. It is wrapped for python using Maturin and integrates rust-numpy for array operations.

Installation

pip install fast-geodist

Quick start

The package provides two main entry points:

import numpy as np
from fast_geodist import haversine

# input either a tuple of (lat/lon, lat/lon)
result = haversine((1, 1, 0, 0))

# or a numpy array of coordinates pairs
# useful if lots of distances need to be calculated
# will be computed in parallel
result = haversine(np.array([(1, 1, 0, 0), (2, 2, 0, 0)]))

Benchmarks

The results of benchmarking show the rust implementation is 14x faster than the python implementation. This benchmark computes distances on an array containing 1,000,000 pairs of coordinates (see bench.py):

--------------------------------------------- benchmark: 2 tests ---------------------------------------------
Name (time in ms)              Min                   Max                  Mean                Median
--------------------------------------------------------------------------------------------------------------
test_benchmark_fast       164.0635 (1.0)        171.7663 (1.0)        168.0218 (1.0)        168.4129 (1.0)
test_benchmark_slow     2,335.4281 (14.23)    2,439.7850 (14.20)    2,395.6077 (14.26)    2,406.6356 (14.29)
--------------------------------------------------------------------------------------------------------------

Computed on an Intel i7-1165G7.

Development

Prequisites:

After cloning the repository, the Makefile includes helpful commands for setting up a development environment, linting, formatting, testing, and benchmarking. Get started as follows:

# setup a new virtual environment
python -m venv .venv
source .venv/bin/activate

# install the development dependencies
make install

# check other available commands
make help

Tooling:

References

As well as the excellent PyO3 documentation, the following posts helped with creation of this package:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fast_geodist-0.4.0.tar.gz (11.4 kB view hashes)

Uploaded Source

Built Distributions

fast_geodist-0.4.0-cp37-abi3-win_amd64.whl (179.5 kB view hashes)

Uploaded CPython 3.7+ Windows x86-64

fast_geodist-0.4.0-cp37-abi3-win32.whl (171.1 kB view hashes)

Uploaded CPython 3.7+ Windows x86

fast_geodist-0.4.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

fast_geodist-0.4.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ i686

fast_geodist-0.4.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (597.1 kB view hashes)

Uploaded CPython 3.7+ macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

fast_geodist-0.4.0-cp37-abi3-macosx_10_7_x86_64.whl (305.4 kB view hashes)

Uploaded CPython 3.7+ macOS 10.7+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page