Skip to content

Skip missing/non-finite coordinates when encoding, with a warning - #56

Open
neon-ninja wants to merge 1 commit into
SymbolixAU:masterfrom
UoA-eResearch:claude/nan-polyline-encoding-o4cy4a
Open

Skip missing/non-finite coordinates when encoding, with a warning#56
neon-ninja wants to merge 1 commit into
SymbolixAU:masterfrom
UoA-eResearch:claude/nan-polyline-encoding-o4cy4a

Conversation

@neon-ninja

Copy link
Copy Markdown

Problem

encode() passed NaN coordinates through an undefined double -> int cast in encode_polyline() (src/googlePolylines.cpp). On x86 the cast yields INT_MIN, so a NaN pair emitted invalid polyline characters (bytes outside the valid 63–126 range, e.g. 0f ce 76 d7) and — because the format is delta-based — corrupted every subsequent point too. decode() on such a string threw a raw C++ exception (basic_string::at: __n >= this->size()). R's NA_real_ is a NaN at the C level, so NA triggered the same behaviour. Relatedly, empty sf POINTs (whose coordinates are NaN) encoded as "??", which silently decodes to the real coordinate (0, 0).

Changes

  • encode_polyline() now skips coordinate pairs where lon or lat is not finite (NA/NaN/Inf); every entry point (encode(), encodeCoordinates(), the sf/sfc path) warns with a count of pairs that could not be encoded.
  • encode(byrow = TRUE) returns NA for such rows instead of skipping (and decode() already maps NA strings back to NA coordinates).
  • Empty sf POINTs now encode to "" (with the warning) instead of "??"/(0, 0); the test pinning the old behaviour was updated.
  • RcppExports regenerated with Rcpp::compileAttributes(); roxygen docs updated (including the previously undocumented precision argument of decode()) and man pages regenerated.
  • New testthat coverage for NA/NaN/Inf inputs, byrow, and empty points; NEWS entry; version bumped to 0.8.8.9000.

Testing

Full testthat suite (with NOT_CRAN=true): 161 pass, 0 fail. R CMD check (no vignettes/manual) shows nothing beyond pre-existing environment notes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UYymBnTDZbXyuus9FY97pd


Generated by Claude Code

Coordinates containing NA, NaN or Inf values previously went through an
undefined double -> int cast in encode_polyline(), corrupting the whole
polyline with out-of-range characters (bytes below 63 and above 127)
that decode() could not even parse. Empty sf POINTs (whose coordinates
are NaN) silently encoded as "??", which decodes to the real coordinate
(0, 0).

Such coordinate pairs are now skipped during encoding and a warning
reports how many were dropped. With encode(byrow = TRUE), rows with
missing coordinates return NA instead (which decode() already maps back
to NA coordinates).

Also documents the previously undocumented `precision` argument of
decode().

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UYymBnTDZbXyuus9FY97pd
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