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

HARMONY-1859: Add WKT POINT support in harmony-py. #94

Merged
merged 3 commits into from
Sep 9, 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
10 changes: 9 additions & 1 deletion examples/tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@
"id": "a22a34f6-36a5-4554-b248-1b3e599dc4c2",
"metadata": {},
"source": [
"Example of submitting a request with WKT spatial:"
"Example of submitting a request with WKT spatial. The supported WKT geometry types are listed at: https://harmony-py.readthedocs.io/en/latest/api.html#harmony.harmony.WKT"
]
},
{
Expand Down Expand Up @@ -384,6 +384,14 @@
" if filename.endswith(\"png\"):\n",
" helper.show_result(filename)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "09f132f3-6117-4ca0-b2b6-ae41c6b48981",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
32 changes: 29 additions & 3 deletions harmony/harmony.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,18 @@ def __repr__(self) -> str:


class WKT:
"""The Well Known Text (WKT) representation of Spatial."""
"""The Well Known Text (WKT) representation of Spatial.
Supported WKT geometry types are: POINT, MULTIPOINT, POLYGON, MULTIPOLYGON.

Example:
spatial=WKT('POINT(-40 10)')

spatial=WKT('MULTIPOINT((-77 38.9),(-40 10))')

spatial=WKT('POLYGON((-140 20, -50 20, -50 60, -140 60, -140 20))')

spatial=WKT('MULTIPOLYGON(((10 10, 20 20, 30 10, 10 10)),((40 40, 50 50, 60 40, 40 40)))')
"""

def __init__(self, wkt: str):
"""Constructs a WKT instance of spatial area.
Expand Down Expand Up @@ -256,7 +267,8 @@ class Request(BaseRequest):
Args:
collection: The CMR collection that should be queried

spatial: Bounding box spatial constraints on the data
spatial: Bounding box spatial constraints on the data or Well Known Text (WKT) string
describing the spatial constraints.

temporal: Date/time constraints on the data provided as a dict mapping "start" and "stop"
keys to corresponding start/stop datetime.datetime objects
Expand Down Expand Up @@ -607,6 +619,20 @@ def _http_method(self, request: BaseRequest) -> str:
method = 'GET' if isinstance(request, CapabilitiesRequest) else 'POST'
return method

def _wkt_to_edr_route(self, wkt_string: str) -> str:
"""Returns the EDR route for the given WKT string."""
# Load the WKT string into a Shapely geometry object
geometry = loads(wkt_string)

if geometry.geom_type == 'Polygon' or geometry.geom_type == 'MultiPolygon':
return 'area'
elif geometry.geom_type == 'Point' or geometry.geom_type == 'MultiPoint':
return 'position'
elif geometry.geom_type == 'LineString' or geometry.geom_type == 'MultiLineString':
return 'trajectory'
else:
raise Exception(f"Unsupported geometry type: {geometry.geom_type}")

def _submit_url(self, request: BaseRequest) -> str:
"""Constructs the URL for the request that is used to submit a new Harmony Job."""
if isinstance(request, CapabilitiesRequest):
Expand All @@ -616,7 +642,7 @@ def _submit_url(self, request: BaseRequest) -> str:
f'{self.config.root_url}'
f'/ogc-api-edr/1.1.0/collections'
f'/{request.collection.id}'
f'/area'
f'/{self._wkt_to_edr_route(request.spatial.wkt)}'
)
else:
return (
Expand Down