diff --git a/src/cbers4asat/cbers4a/search.py b/src/cbers4asat/cbers4a/search.py index aedacb0..d6d1315 100644 --- a/src/cbers4asat/cbers4a/search.py +++ b/src/cbers4asat/cbers4a/search.py @@ -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" @@ -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: @@ -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): """ diff --git a/src/cbers4asat/cbers4asat.py b/src/cbers4asat/cbers4asat.py index 550d687..d03e2d5 100644 --- a/src/cbers4asat/cbers4asat.py +++ b/src/cbers4asat/cbers4asat.py @@ -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 @@ -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: @@ -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") diff --git a/tests/test_Cbers4aAPI.py b/tests/test_Cbers4aAPI.py index 7566fdd..a25afdd 100644 --- a/tests/test_Cbers4aAPI.py +++ b/tests/test_Cbers4aAPI.py @@ -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 @@ -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