Giter Club home page Giter Club logo

pypolyline's Introduction

example workflow Coverage Status DownloadsDOI

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.7
  • Python 3.8
  • Python 3.9
  • Python 3.10

Supported Platforms

  • Linux (manylinux*-compatible)
  • macOS (x86_64 and aarch64)
  • Windows 64-bit

Usage

Coordinates must be in (Longitude, Latitude) order

from pypolyline.cutil 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)

Cython Module 🔥

If you're comfortable with a lack of built-in exceptions, you should 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]]

Otherwise, import from util instead, for a slower, ctypes-based interface. Attempts to decode an invalid Polyline will throw util.EncodingError
Attempts to encode invalid coordinates will throw util.DecodingError

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

Citing Pypolyline

If Pypolyline has been significant in your research, and you would like to acknowledge the project in your academic publication, we suggest citing it as follows (example in APA style, 7th edition):

Hügel, S. (2021). Pypolyline (Version X.Y.Z) [Computer software]. https://doi.org/10.5281/zenodo.5774925

In Bibtex format:

@software{Hugel_Pypolyline_2021,
author = {Hügel, Stephan},
doi = {10.5281/zenodo.5774925},
license = {MIT},
month = {12},
title = {{Pypolyline}},
url = {https://github.com/urschrei/simplification},
version = {X.Y.Z},
year = {2021}
}

pypolyline's People

Contributors

dependabot[bot] avatar urschrei avatar

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.