Add encode/decode for the Google Encoded Polyline format#32
Open
gistrec wants to merge 1 commit into
Open
Conversation
Port of PolyUtil.encode/decode from android-maps-utils, in a new <geo/encoding.hpp> header so <geo/poly.hpp> stays free of <string>/<vector>. The zig-zag/varint arithmetic is done in unsigned types: the Java original relies on wrapping int arithmetic, and left-shifting a negative signed value is UB in C++17. decode is bounds-checked: a string truncated mid-point yields the points decoded so far and drops the incomplete trailing point (the Java original throws IndexOutOfBounds there). Tests cover the reference strings from the Encoded Polyline Algorithm Format docs byte-for-byte, truncation, and quantized round-trips across the poles and the antimeridian.
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Ports
PolyUtil.encode/decode(Google Encoded Polyline Algorithm Format) from android-maps-utils — first of the upstream-parity features planned for 1.1.0 (next:simplify+is_closed_polygon, which will reusedecodein tests).<geo/encoding.hpp>— keeps<geo/poly.hpp>free of<string>/<vector>. Pulled in by the<geo/geo.hpp>umbrella; picked up automatically by the CMake install and the build2hxx{**}glob.geo::encode(const Path&)→std::string— samePathcontract as the other container functions; coordinates quantized to 1e-5° (~1 m).geo::decode(std::string_view)→std::vector<LatLng>— bounds-checked: a string truncated mid-point yields the points decoded so far and drops the incomplete trailing point (the Java original throwsIndexOutOfBounds). Malformed input is memory-safe but yields unspecified coordinates (the format has no checksum).Portability notes
intarithmetic; left-shifting a negative signed value is UB in C++17, so the zig-zag/varint accumulation is done in unsigned types — bit-identical to upstream on well-formed input.r >> 1on a negative value (arithmetic shift) is implementation-defined in C++17 but guaranteed on every supported compiler (GCC/Clang/MSVC) and standardized in C++20; noted in a comment.Docs
docs/api.mdwith the reference example; Conventions updated (include strategy,std::bad_allocnote) andencodeadded to thePathlist.Test plan
Encoding.encode: byte-for-byte match with the reference strings from the Encoded Polyline Algorithm Format docs (_p~iF~ps|U_ulLnnqC_mqNvxq@,?~oia@), quantization collapse.Encoding.decode: reference string, empty input, two truncation variants (mid-chunk and between chunks).Encoding.encode_decode_roundtrip: poles, antimeridian, sub-quantum coordinates (error ≤ 5e-6°).-Wall -Wextra -Wpedantic -Wconversion -Werror.