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 2.7
  • Python 3.6
  • Python 3.7

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.

Files for pypolyline, version 0.2.4
Filename, size File type Python version Upload date Hashes
Filename, size pypolyline-0.2.4-cp27-cp27m-macosx_10_13_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 (274.6 kB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp27-cp27mu-manylinux1_x86_64.whl (507.8 kB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp27-cp27mu-manylinux2010_x86_64.whl (507.8 kB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp27-cp27m-win32.whl (290.4 kB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp27-cp27m-win_amd64.whl (310.6 kB) File type Wheel Python version cp27 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp36-cp36m-macosx_10_13_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 (274.5 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp36-cp36m-manylinux1_x86_64.whl (542.8 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp36-cp36m-manylinux2010_x86_64.whl (542.8 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp36-cp36m-win32.whl (289.0 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp36-cp36m-win_amd64.whl (312.1 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp37-cp37m-macosx_10_13_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 (272.9 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp37-cp37m-manylinux1_x86_64.whl (543.4 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp37-cp37m-manylinux2010_x86_64.whl (543.4 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp38-cp38-macosx_10_13_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 (272.2 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp38-cp38-manylinux1_x86_64.whl (581.8 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size pypolyline-0.2.4-cp38-cp38-manylinux2010_x86_64.whl (581.8 kB) File type Wheel Python version cp38 Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page