Skip to content

ol.format.Polyline fixes#16955

Merged
ahocevar merged 7 commits into
openlayers:mainfrom
M393:polyline
Jul 3, 2025
Merged

ol.format.Polyline fixes#16955
ahocevar merged 7 commits into
openlayers:mainfrom
M393:polyline

Conversation

@M393

@M393 M393 commented Jul 2, 2025

Copy link
Copy Markdown
Contributor

This fixes a few subtle bugs with Polyline encoding and decoding.

  • Encoding multiple deltas could accumulate a small rounding error
  • The original polyline encode algorithm uses python2 rounding mode which is different from the one in JavaScript
  • Decoding a polyline could return coordinates with small floating point errors

The methods encodeFloats and decodeFloats are part of the api, and now no longer used. I'm not sure why they were in the api in the first place as they don't seem to be useful on their own. They are now marked as deprecated, but maybe they could be removed.

Clue for the python2 rounding mode:
https://github.com/mapbox/polyline/blob/5e797bf9cdf46db94bc3290b0e4d39e6db5a51c9/src/polyline.js#L15
Can be tested here:
https://developers.google.com/maps/documentation/utilities/polylineutility

M393 added 6 commits July 2, 2025 13:29
Doing the addition with the last number as integer prevents subtle
rounding errors, i. e. 0.3 instead of
0.30000000000000004
Rounding the number instead of the delta stops rounding errors
to accumulate.
Deltas that individually round to zero should be encoded to the
polyline once their sum is big enough.
Google's polyline algorithm uses rounding from python 2 which
this now mimics. Round -0.5 away from zero instead of towards zero.
@github-actions

github-actions Bot commented Jul 2, 2025

Copy link
Copy Markdown

📦 Preview the website for this branch here: https://deploy-preview-16955--ol-site.netlify.app/.

@ahocevar ahocevar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @M393. Would it be possible to only make the accuracy improvements you mentioned, without the API changes? If not, we'll mark this as breaking change, you'll have to add upgrade instructions in upgrade-notes.md, and we'll merge this when we release v11.

@M393

M393 commented Jul 3, 2025

Copy link
Copy Markdown
Contributor Author

@ahocevar I re-added the @api tags where they were removed.

@ahocevar ahocevar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @M393

@ahocevar ahocevar merged commit e719b26 into openlayers:main Jul 3, 2025
8 checks passed
@M393 M393 deleted the polyline branch July 3, 2025 09:07
@M393 M393 mentioned this pull request Sep 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants