Skip to content

Polyline

Encode and decode Google's Encoded Polyline Algorithm Format — lists of coordinates as compact ASCII strings.

Implements Encoded Polyline Algorithm Format. Spec v0.1.1; every port passes the same 44 conformance cases.

Julia registration pendingNim v0.1.0 · listing pendingZig v0.1.0

Install

using Pkg
Pkg.add(url="https://github.com/Xenoglyphiq/EncodedPolyline.jl")

Examples

The same three examples exist in every port, runnable as-is from each repository's examples/ folder.

Encode a three-point route

examples/encode_route.jl
# Canonical example `encode_route`: encode a three-point route and print the string.
using EncodedPolyline
route = [LonLat(-120.2, 38.5), LonLat(-120.95, 40.7), LonLat(-126.453, 43.252)]
println(EncodedPolyline.encode(route))

Decode Google's documented example

examples/decode_route.jl
# Canonical example `decode_route`: decode Google's example and print each point.
using EncodedPolyline
points = try
EncodedPolyline.decode("_p~iF~ps|U_ulLnnqC_mqNvxq`@")
catch e
e isa PolylineError && println(stderr, sprint(showerror, e))
rethrow()
end
for p in points
println("lon $(p.lon), lat $(p.lat)")
end

Round-trip a route at precision 6

examples/precision_6.jl
# Canonical example `precision_6`: round-trip a route at precision 6 (OSRM, Valhalla).
using EncodedPolyline
route = [LonLat(-73.985713, 40.748441), LonLat(-73.978569, 40.751657), LonLat(-73.968285, 40.785091)]
text = EncodedPolyline.encode(route; precision=6)
back = EncodedPolyline.decode(text; precision=6)
println(text)
for (a, b) in zip(route, back)
println("($(a.lon), $(a.lat)) -> ($(b.lon), $(b.lat))")
abs(a.lon - b.lon) <= 1e-6 && abs(a.lat - b.lat) <= 1e-6 || error("round trip mismatch")
end
println("round trip matches")

Ports

LanguagePackageReleaseSpecConformance
JuliaEncodedPolylineregistration pendingv0.1.1core ✓ full ✓ (44/44)
Nimpolylinev0.1.0 · listing pendingv0.1.1core ✓ full ✓ (44/44)
Zigpolylinev0.1.0v0.1.1core ✓ full ✓ (44/44)

Performance

Median time, and ratio to the reference implementation measured in the same session. The spec's target is within 2×.

PortEncode 100k pointsDecode 100k points
ReferenceRust polyline 0.11.0: 0.892 msRust polyline 0.11.0: 0.643 ms
Julia1.466 ms (1.64×)0.816 ms (1.27×)
Nim1.645 ms (1.84×)0.802 ms (1.25×)
Zig1.308 ms (1.47×)0.642 ms (1.00×)

Spec and source