Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for multiple-point searches #73

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- Support multi-point searches ([#72](https://github.com/nasa/python_cmr/issues/72))

### Fixed
- Setup vcrpy for new `revision_date` unit tests ([#70](https://github.com/nasa/python_cmr/issues/70))

Expand Down
16 changes: 13 additions & 3 deletions cmr/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ def _build_url(self) -> str:
if isinstance(val, list):
for list_val in val:
formatted_params.append(f"{key}[]={list_val}")

elif isinstance(val, bool):
formatted_params.append(f"{key}={str(val).lower()}")

else:
formatted_params.append(f"{key}={val}")

Expand Down Expand Up @@ -490,18 +492,26 @@ def version(self, version: str) -> Self:

def point(self, lon: FloatLike, lat: FloatLike) -> Self:
"""
Filter by granules that include a geographic point.
Filter by granules that include one or more geographic points. Call this method
once for each point of interest.

By default, query results will include items that include _all_ given points.
To return items that include _any_ given point, set the option on your `query`
instance like so: `query.options["point"] = {"or": True}`

:param lon: longitude of geographic point
:param lat: latitude of geographic point
:returns: self
"""

# coordinates must be a float
lon = float(lon)
lat = float(lat)

if "point" not in self.params:
self.params["point"] = []

self.params['point'] = f"{lon},{lat}"
self.params["point"].append(f"{lon},{lat}")

return self

Expand Down
12 changes: 10 additions & 2 deletions tests/test_granule.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ def test_point_set(self):
query.point(10, 15.1)

self.assertIn(self.point, query.params)
self.assertEqual(query.params[self.point], "10.0,15.1")
self.assertEqual(query.params[self.point], ["10.0,15.1"])

def test_points_set(self):
query = GranuleQuery()

query.point(10, 15.1).point(20.4, 10.2)

self.assertIn(self.point, query.params)
self.assertEqual(query.params[self.point], ["10.0,15.1", "20.4,10.2"])

def test_point_invalid_set(self):
query = GranuleQuery()
Expand Down Expand Up @@ -475,7 +483,7 @@ def test_valid_parameters(self):

self.assertEqual(query.params["short_name"], "AST_L1T")
self.assertEqual(query.params["version"], "003")
self.assertEqual(query.params["point"], "-100.0,42.0")
self.assertEqual(query.params["point"], ["-100.0,42.0"])

def test_invalid_parameters(self):
query = GranuleQuery()
Expand Down