Skip to content

Commit

Permalink
Added support for multiple-point searches (#73)
Browse files Browse the repository at this point in the history
* Added support for multiple-point searches

* Made the necessary changes as suggested

* Fixed some failing tests

* Some minor doc changes
  • Loading branch information
Sherwin-14 authored Aug 19, 2024
1 parent c09bc4e commit a797e35
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
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

0 comments on commit a797e35

Please sign in to comment.