#polyline #encoding #triple #precision #encode #polyline-encoder #polyline-decoder

bin+lib flexpolyline

Flexible Polyline encoding: a lossy compressed representation of a list of coordinate pairs or triples

2 releases (1 stable)

1.0.0 Sep 24, 2024
0.1.0 Apr 28, 2020

#68 in Compression

Download history 3149/week @ 2024-12-18 102/week @ 2024-12-25 2523/week @ 2025-01-01 3858/week @ 2025-01-08 2706/week @ 2025-01-15 3096/week @ 2025-01-22 3401/week @ 2025-01-29 3797/week @ 2025-02-05 3112/week @ 2025-02-12 3862/week @ 2025-02-19 4193/week @ 2025-02-26 3674/week @ 2025-03-05 3033/week @ 2025-03-12 2995/week @ 2025-03-19 4033/week @ 2025-03-26 4567/week @ 2025-04-02

15,409 downloads per month
Used in 2 crates

MIT license

35KB
773 lines

Flexible Polyline encoding

The flexible polyline encoding is a lossy compressed representation of a list of coordinate pairs or coordinate triples. It achieves that by:

  1. Reducing the decimal digits of each value.
  2. Encoding only the offset from the previous point.
  3. Using variable length for each coordinate delta.
  4. Using 64 URL-safe characters to display the result.

The encoding is a variant of Encoded Polyline Algorithm Format. The advantage of this encoding over the original are the following:

  • Output string is composed by only URL-safe characters, i.e. may be used without URL encoding as query parameters.
  • Floating point precision is configurable: This allows to represent coordinates with precision up to microns (5 decimal places allow meter precision only).
  • It allows to encode a 3rd dimension with a given precision, which may be a level, altitude, elevation or some other custom value.

Specification

See Specification.

Example

use flexpolyline::{Polyline, Precision};

// encode
let coordinates = vec![
    (50.1022829, 8.6982122),
    (50.1020076, 8.6956695),
    (50.1006313, 8.6914960),
    (50.0987800, 8.6875156),
];

let polyline = Polyline::Data2d {
    coordinates,
    precision2d: Precision::Digits5,
};

let encoded = polyline.encode().unwrap();
assert_eq!(encoded, "BFoz5xJ67i1B1B7PzIhaxL7Y");

// decode
let decoded = Polyline::decode("BFoz5xJ67i1B1B7PzIhaxL7Y").unwrap();
assert_eq!(
    decoded,
    Polyline::Data2d {
        coordinates: vec![
            (50.10228, 8.69821),
            (50.10201, 8.69567),
            (50.10063, 8.69150),
            (50.09878, 8.68752)
        ],
        precision2d: Precision::Digits5
    }
);

No runtime deps