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

add filter component and functionality #75

Merged
merged 4 commits into from
Jul 26, 2023
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
70 changes: 52 additions & 18 deletions stac_ipyleaflet/stac_discovery/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from stac_ipyleaflet.constants import RESCALE
from typing import TypedDict, Optional


class OutputCollectionObj(TypedDict):
id: str
title: str
has_cog: bool
start_date: str
end_date: str
bbox: str
Expand All @@ -16,14 +18,17 @@ class OutputCollectionObj(TypedDict):
description: str
license: str

class Stac():

class Stac:
@staticmethod
def organize_collections(collections=[]):
output_collections = []
for collection in collections:
try:
data = collection.to_dict()

has_cog = True if data["item_assets"] else False

id = data["id"].strip()
title = data["title"].strip()

Expand Down Expand Up @@ -61,14 +66,27 @@ def organize_collections(collections=[]):
)

license = data["license"]
collection_obj = OutputCollectionObj({'id': id, 'title': title, 'start_date': start_date, 'end_date': end_date, 'bbox': bbox, 'metadata': metadata, 'href': href, 'description': description, 'license': license})
collection_obj = OutputCollectionObj(
{
"id": id,
"title": title,
"has_cog": has_cog,
"start_date": start_date,
"end_date": end_date,
"bbox": bbox,
"metadata": metadata,
"href": href,
"description": description,
"license": license,
}
)
output_collections.append(collection_obj)
except Exception as err:
error = {'error': err, 'collection': collection}
error = {"error": err, "collection": collection}
logging.error(error)
return None
if len(output_collections) > 0:
output_collections.sort(key= lambda x:x['title'])
output_collections.sort(key=lambda x: x["title"])
return output_collections

@staticmethod
Expand All @@ -84,9 +102,8 @@ def get_item_info(url=None, **kwargs):

if isinstance(url, str):
r = make_get_request(url).json()

return r


@staticmethod
def stac_tile(
Expand Down Expand Up @@ -144,13 +161,19 @@ def stac_tile(
kwargs.pop("TileMatrixSetId")

if isinstance(titiler_stac_endpoint, str):
r = make_get_request(f"{titiler_stac_endpoint}/stac/{TileMatrixSetId}/tilejson.json", kwargs).json()
r = make_get_request(
f"{titiler_stac_endpoint}/stac/{TileMatrixSetId}/tilejson.json", kwargs
).json()
else:
r = make_get_request(titiler_stac_endpoint.url_for_stac_item(), kwargs).json()
r = make_get_request(
titiler_stac_endpoint.url_for_stac_item(), kwargs
).json()
return r["tiles"][0]

@staticmethod
def stac_bounds(url=None, collection=None, item=None, titiler_stac_endpoint=None, **kwargs):
def stac_bounds(
url=None, collection=None, item=None, titiler_stac_endpoint=None, **kwargs
):
"""Get the bounding box of a single SpatialTemporal Asset Catalog (STAC) item.
Args:
url (str): HTTP URL to a STAC item
Expand All @@ -173,12 +196,13 @@ def stac_bounds(url=None, collection=None, item=None, titiler_stac_endpoint=None
if isinstance(titiler_stac_endpoint, str):
r = make_get_request(f"{titiler_stac_endpoint}/stac/bounds", kwargs).json()
else:
r = make_get_request(titiler_stac_endpoint.url_for_stac_bounds(), kwargs).json()
r = make_get_request(
titiler_stac_endpoint.url_for_stac_bounds(), kwargs
).json()

bounds = r["bounds"]
return bounds

# QUESTION: Is this being or planning to be used?
def add_stac_layer(
self,
url=None,
Expand Down Expand Up @@ -242,7 +266,7 @@ def set_default_bands(bands):

if len(bands) == 1:
return bands

if not isinstance(bands, list):
raise ValueError("bands must be a list or a string.")

Expand Down Expand Up @@ -286,13 +310,18 @@ def stac_search(
items = list(search.item_collection())
info = {}
for item in items:
info[item.id] = {'id': item.id, 'href': item.get_self_href(), 'bands': list(item.get_assets().keys()), 'assets': item.get_assets()}
info[item.id] = {
"id": item.id,
"href": item.get_self_href(),
"bands": list(item.get_assets().keys()),
"assets": item.get_assets(),
}
return info
else:
return search

@staticmethod
def get_metadata(
def get_metadata(
data_type="cog",
titiler_stac_endpoint=None,
url=None,
Expand All @@ -305,7 +334,9 @@ def get_metadata(
kwargs["max_size"] = max_size

if isinstance(titiler_stac_endpoint, str):
r = make_get_request(f"{titiler_stac_endpoint}/{data_type}/metadata", kwargs).json()
r = make_get_request(
f"{titiler_stac_endpoint}/{data_type}/metadata", kwargs
).json()
return r
else:
return "Cannot process request: titiler stac endpoint not provided."
Expand Down Expand Up @@ -369,7 +400,10 @@ def get_tile_url(
kwargs.pop("TileMatrixSetId")

if isinstance(titiler_stac_endpoint, str):
r = make_get_request(f"{titiler_stac_endpoint}/{data_type}/{TileMatrixSetId}/tilejson.json", kwargs).json()
r = make_get_request(
f"{titiler_stac_endpoint}/{data_type}/{TileMatrixSetId}/tilejson.json",
kwargs,
).json()
return r
else:
return "STAC ENDPOINT IS NECESSARY."
return "STAC ENDPOINT IS NECESSARY."
sandrahoang686 marked this conversation as resolved.
Show resolved Hide resolved
Loading