Skip to content

fix: avoid scientific notation in CMR URL coordinate parameters#112

Closed
stark256-spec wants to merge 2 commits into
nasa:developfrom
stark256-spec:fix/scientific-notation-in-coordinate-urls
Closed

fix: avoid scientific notation in CMR URL coordinate parameters#112
stark256-spec wants to merge 2 commits into
nasa:developfrom
stark256-spec:fix/scientific-notation-in-coordinate-urls

Conversation

@stark256-spec
Copy link
Copy Markdown

@stark256-spec stark256-spec commented Jun 3, 2026

Summary

Fixes #108

Python's default float-to-string conversion uses scientific notation for numbers outside roughly [1e-4, 1e16). For example, 0.00001 becomes "1e-05". CMR rejects scientific notation in URL parameters:

{"errors":["[42.0,1e-05] is not a valid URL encoded point"]}

This caused queries using coordinates near the equator (or any latitude/longitude/distance with small absolute value) to fail.

Fix

Added _format_float() which checks whether the default g-format string contains e/E and falls back to fixed-point formatting with trailing zeros stripped:

>>> _format_float(0.00001)
'0.00001'
>>> _format_float(42.5)
'42.5'
>>> _format_float(42.0)
'42'

Applied to all five coordinate methods: point(), circle(), polygon(), bounding_box(), line().

Test

from cmr import GranuleQuery
q = GranuleQuery().short_name("FOO").point(42, 0.00001)
assert "1e" not in q._build_url()
# 'https://cmr.earthdata.nasa.gov/search/granules.json?short_name=FOO&point[]=42.0,0.00001'

someshnagalla and others added 2 commits June 1, 2026 11:04
- Add Query.client_id() convenience method that sets the Client-Id
  header. When called without arguments it defaults to
  python_cmr-<version>; when called with a name it produces
  <name>/python_cmr-<version>. Closes nasa#49.

- Update GranuleCollectionBaseQuery.platform() to accept either a
  single string or a list of strings, matching CMR's support for
  multi-platform filtering. Closes nasa#80.
Python's default float formatting uses scientific notation for numbers
outside roughly [1e-4, 1e16) — e.g. 0.00001 becomes 1e-05. CMR rejects
scientific notation in URL parameters with "is not a valid URL encoded
point", causing otherwise valid queries to fail silently.

Add _format_float() which falls back to fixed-point formatting whenever
the default %g representation contains 'e' or 'E'. Apply it in all five
coordinate methods: point(), circle(), polygon(), bounding_box(), line().

Fixes nasa#108
@stark256-spec stark256-spec force-pushed the fix/scientific-notation-in-coordinate-urls branch from 17c4fa3 to 92b9b4e Compare June 3, 2026 16:27
@chuckwondo
Copy link
Copy Markdown
Collaborator

Thanks @stark256-spec. The default branch is develop, not main, so please retarget your PR to develop, then I'll review.

@stark256-spec stark256-spec changed the base branch from main to develop June 3, 2026 19:49
@stark256-spec
Copy link
Copy Markdown
Author

Done — retargeted to develop. Thanks for the catch!

@stark256-spec
Copy link
Copy Markdown
Author

Closing this in favour of a clean replacement — the branch inadvertently included unrelated client_id() and multi-platform changes stacked from a previous branch, making it equivalent to the already-closed #111. Testing also revealed two bugs in the _format_float helper (integers were gaining spurious .0 suffixes, and whole floats were losing their decimal point). Both are fixed in the replacement. A new PR targeting develop contains only the scientific notation fix with all 122 existing tests passing.

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.

very small numbers get written into URLs with error-raising scientific notation

2 participants