Skip to content

Commit

Permalink
Fix - Adaptado ao novo STAC v0.9 do INPE
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-russo committed Aug 20, 2023
1 parent e2382e6 commit 4d427d8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/cbers4asat/cbers4a/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Search(object):
"""Simple class to search INPE STAC Catalog"""

# INPE STAC Catalog
base_url = "http://www2.dgi.inpe.br/inpe-stac"
base_url = "http://www.dgi.inpe.br/lgi-stac"
collection_endpoint = "/collections"
search_endpoint = "/search"

Expand Down Expand Up @@ -44,12 +44,12 @@ def __call__(self, **search_keys):
features = []
for id_ in self.search_keys["ids"]:
r = self.session.get(
self.base_url + self.collection_endpoint + "/X/items/" + id_
f'{self.base_url}{self.collection_endpoint}/{self.search_keys["collection"]}/items/{id_}'
)
r.raise_for_status()
if r.status_code == 200:
feat = r.json()
if feat["type"] == "Feature":
if feat.get("type") == "Feature":
features.append(feat)
return ItemCollection({"type": "FeatureCollection", "features": features})
else:
Expand Down Expand Up @@ -126,7 +126,10 @@ def collections(self, collections):
"""
docstring
"""
self.update(collections=collections)
if isinstance(collections, str):
self.update(collection=collections)
else:
self.update(collections=collections)

def ids(self, ids):
"""
Expand Down
38 changes: 29 additions & 9 deletions src/cbers4asat/cbers4asat.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,30 @@ def query(
return result.featurescollection

@staticmethod
def query_by_id(id_: Union[str, List[str]]):
def query_by_id(scene_id: Union[str, List[str]], collection: str = None):
"""
Search a product by id
Args:
id_: One or more scene's id
scene_id: One or more scene's id
collection: Collection's name
Returns:
dict: Dict with GeoJSON-like format
"""
search = Search()

if not scene_id:
raise Exception("Especify an id")
elif not collection:
raise Exception("Especify a collection")

# search() method only works with a list of ids.
if type(id_) is not list:
search.ids([id_])
if type(scene_id) is not list:
search.ids([scene_id])
else:
search.ids(id_)
search.ids(scene_id)

search.collections(collection)

result = search()
return result.featurescollection
Expand Down Expand Up @@ -180,7 +188,7 @@ def __download_gdf(
mkdir(new_path)
outdir = new_path

products_query = self.query_by_id(str(index))
products_query = self.query_by_id(str(index), row.collection)
products_query = ItemCollection(products_query).items()
for product in products_query:
for band in bands:
Expand Down Expand Up @@ -249,6 +257,18 @@ def to_geodataframe(products: Dict, crs: str = "EPSG:4326"):
Returns:
GeoDataFrame
"""
return GeoDataFrame.from_features(products, crs=crs).set_index(
json_normalize(products["features"])["id"].values
)
items = [feat for feat in ItemCollection(products).items()]

for item in items:
item_metadata = {}
item_metadata.update(id=item.id)
item_metadata.update(bbox=item.bbox)
item_metadata.update(collection=item.collection)

for index, feature in enumerate(products.get("features")):
if feature["id"] == item.id:
products.get("features")[index].get("properties").update(
**item_metadata
)

return GeoDataFrame.from_features(products, crs=crs).set_index("id")
4 changes: 2 additions & 2 deletions tests/test_Cbers4aAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def mock_get(*args, **kwargs):

monkeypatch.setattr("requests.Session.get", mock_get)

result = self.api.query_by_id("ABC123")
result = self.api.query_by_id(scene_id="ABC123", collection="y")

assert self.expected_result == result

Expand All @@ -195,7 +195,7 @@ def mock_get(*args, **kwargs):

monkeypatch.setattr("requests.Session.get", mock_get)

result = self.api.query_by_id(["ABC123"])
result = self.api.query_by_id(scene_id=["ABC123"], collection="y")

assert self.expected_result == result

Expand Down

0 comments on commit 4d427d8

Please sign in to comment.