pypolyline
pypolyline provides fast Google Polyline encoding and decoding functionalities, leveraging Rust FFI for performance. It is currently at version 0.5.6 and maintains an active, albeit irregular, release cadence with minor updates addressing improvements and bug fixes.
Common errors
-
error: can't find Rust compiler
cause During `pip install pypolyline`, a pre-built wheel for your platform/Python version was not found, and `pip` attempted to build the library from source without an installed Rust toolchain.fixInstall Rust by following instructions on https://rustup.rs (e.g., `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh`) and then retry `pip install pypolyline`. -
TypeError: encode() got an unexpected keyword argument 'factor'
cause You are attempting to use the `factor` argument, which was deprecated and removed in `pypolyline` `v0.5.0` in favor of `precision`.fixReplace `factor=...` with `precision=...`. For example, `factor=1e5` should become `precision=5`. -
Decoded points are not accurate (e.g., missing decimal places) or encoded polyline is much shorter/longer than expected.
cause The `precision` parameter used during encoding or decoding does not match the expected precision for the polyline data.fixVerify that `precision=5` is used for standard Google Polylines, as this specifies a factor of 10^5. Adjust `precision` if your polyline format uses a different scaling factor.
Warnings
- breaking The `factor` argument for `encode` and `decode` functions was replaced by `precision` in `v0.5.0`. Using `factor` will now raise a TypeError.
- gotcha Installation via `pip` requires a Rust toolchain if a pre-built wheel is not available for your specific Python version and operating system combination.
- gotcha Using an incorrect `precision` value will lead to inaccurate decoded points or unnecessarily long (overly precise) encoded polylines.
Install
-
pip install pypolyline
Imports
- decode
from pypolyline.codec import decode
- encode
from pypolyline.codec import encode
Quickstart
from pypolyline.codec import decode, encode
# Example data
test_polyline = "y~z_@mfhV~tqNvxq`@"
test_points = [(38.5, -120.2), (40.7, -120.95), (43.252, -126.453)]
# Encoding points to a polyline string (Google Polyline typically uses precision=5)
encoded_polyline = encode(test_points, precision=5)
print(f"Encoded Polyline: {encoded_polyline}")
# Decoding a polyline string back to points
decoded_points = decode(test_polyline, precision=5)
print(f"Decoded Points: {decoded_points}")