Skip to main content

Fast Google Polyline encoding and decoding using Rust FFI

Project description

Build Status Build status Coverage Status

Fast Google Polyline Encoding and Decoding

Installation

pip install pypolyline
Please use a recent (>= 8.1.2) version of pip.

Supported Python Versions

  • Python 3.6
  • Python 3.7
  • Python 3.8
  • Python 3.9 (Linux and macOS only)

Supported Platforms

  • Linux (manylinux1-compatible)
  • OS X
  • Windows 32-bit / 64-bit

Usage

Coordinates must be in (Longitude, Latitude) order

from pypolyline.util import encode_coordinates, decode_polyline

coords = [
            [52.64125, 23.70162],
            [52.64938, 23.70154],
            [52.64957, 23.68546],
            [52.64122, 23.68549],
            [52.64125, 23.70162]
         ]

# precision is 5 for Google Polyline, 6 for OSRM / Valhalla
polyline = encode_coordinates(coords, 5)
# polyline is 'ynh`IcftoCyq@Ne@ncBds@EEycB'
decoded_coords = decode_polyline(polyline, 5)

Attempts to decode an invalid Polyline will throw util.EncodingError
Attempts to encode invalid coordinates will throw util.DecodingError

Cython Module 🔥

If you're comfortable with a lack of built-in exceptions, you may use the compiled Cython version of the functions, giving a 3x speedup over the ctypes functions:

from pypolyline.cutil import encode_coordinates, decode_polyline
  • Longitude errors will return strings beginning with Longitude error:
  • Latitude errors will return strings beginning with Latitude error:
  • Polyline errors will return [[nan, nan]]

How it Works

FFI and a Rust binary

Is It Fast

…Yes.
You can verify this by installing the polyline and cgpolyencode packages, then running benchmarks.py, a calibrated benchmark using cProfile.
On a 1.8 GHz Intel Core i7, The pure-Python test runs in ~21 s, the C++ (cgpolyencode.GPolyEncoder) test runs in around 600 ms, and The Rust + Cython benchmark runs in around 400 ms (33% faster).

License

MIT

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

pypolyline-0.2.5-cp39-cp39-manylinux2010_x86_64.whl (595.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

pypolyline-0.2.5-cp39-cp39-manylinux1_x86_64.whl (595.2 kB view hashes)

Uploaded CPython 3.9

pypolyline-0.2.5-cp39-cp39-macosx_10_15_x86_64.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.macosx_10_11_intel.macosx_10_11_x86_64.whl (320.3 kB view hashes)

Uploaded CPython 3.9 macOS 10.10+ intel macOS 10.10+ x86-64 macOS 10.11+ intel macOS 10.11+ x86-64 macOS 10.15+ x86-64 macOS 10.9+ intel macOS 10.9+ x86-64

pypolyline-0.2.5-cp38-cp38-manylinux2010_x86_64.whl (606.7 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

pypolyline-0.2.5-cp38-cp38-manylinux1_x86_64.whl (606.7 kB view hashes)

Uploaded CPython 3.8

pypolyline-0.2.5-cp38-cp38-macosx_10_15_x86_64.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.macosx_10_11_intel.macosx_10_11_x86_64.whl (318.1 kB view hashes)

Uploaded CPython 3.8 macOS 10.10+ intel macOS 10.10+ x86-64 macOS 10.11+ intel macOS 10.11+ x86-64 macOS 10.15+ x86-64 macOS 10.9+ intel macOS 10.9+ x86-64

pypolyline-0.2.5-cp37-cp37m-manylinux2010_x86_64.whl (569.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

pypolyline-0.2.5-cp37-cp37m-manylinux1_x86_64.whl (569.6 kB view hashes)

Uploaded CPython 3.7m

pypolyline-0.2.5-cp37-cp37m-macosx_10_15_x86_64.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.macosx_10_11_intel.macosx_10_11_x86_64.whl (318.8 kB view hashes)

Uploaded CPython 3.7m macOS 10.10+ intel macOS 10.10+ x86-64 macOS 10.11+ intel macOS 10.11+ x86-64 macOS 10.15+ x86-64 macOS 10.9+ intel macOS 10.9+ x86-64

pypolyline-0.2.5-cp36-cp36m-manylinux2010_x86_64.whl (570.2 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

pypolyline-0.2.5-cp36-cp36m-manylinux1_x86_64.whl (570.2 kB view hashes)

Uploaded CPython 3.6m

pypolyline-0.2.5-cp36-cp36m-macosx_10_15_x86_64.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.macosx_10_11_intel.macosx_10_11_x86_64.whl (318.8 kB view hashes)

Uploaded CPython 3.6m macOS 10.10+ intel macOS 10.10+ x86-64 macOS 10.11+ intel macOS 10.11+ x86-64 macOS 10.15+ x86-64 macOS 10.9+ intel macOS 10.9+ 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