Skip to content

Commit 4d427d8

Browse files
committed
Fix - Adaptado ao novo STAC v0.9 do INPE
1 parent e2382e6 commit 4d427d8

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

src/cbers4asat/cbers4a/search.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Search(object):
1010
"""Simple class to search INPE STAC Catalog"""
1111

1212
# INPE STAC Catalog
13-
base_url = "http://www2.dgi.inpe.br/inpe-stac"
13+
base_url = "http://www.dgi.inpe.br/lgi-stac"
1414
collection_endpoint = "/collections"
1515
search_endpoint = "/search"
1616

@@ -44,12 +44,12 @@ def __call__(self, **search_keys):
4444
features = []
4545
for id_ in self.search_keys["ids"]:
4646
r = self.session.get(
47-
self.base_url + self.collection_endpoint + "/X/items/" + id_
47+
f'{self.base_url}{self.collection_endpoint}/{self.search_keys["collection"]}/items/{id_}'
4848
)
4949
r.raise_for_status()
5050
if r.status_code == 200:
5151
feat = r.json()
52-
if feat["type"] == "Feature":
52+
if feat.get("type") == "Feature":
5353
features.append(feat)
5454
return ItemCollection({"type": "FeatureCollection", "features": features})
5555
else:
@@ -126,7 +126,10 @@ def collections(self, collections):
126126
"""
127127
docstring
128128
"""
129-
self.update(collections=collections)
129+
if isinstance(collections, str):
130+
self.update(collection=collections)
131+
else:
132+
self.update(collections=collections)
130133

131134
def ids(self, ids):
132135
"""

src/cbers4asat/cbers4asat.py

+29-9
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,30 @@ def query(
9393
return result.featurescollection
9494

9595
@staticmethod
96-
def query_by_id(id_: Union[str, List[str]]):
96+
def query_by_id(scene_id: Union[str, List[str]], collection: str = None):
9797
"""
9898
Search a product by id
9999
100100
Args:
101-
id_: One or more scene's id
101+
scene_id: One or more scene's id
102+
collection: Collection's name
102103
Returns:
103104
dict: Dict with GeoJSON-like format
104105
"""
105106
search = Search()
106107

108+
if not scene_id:
109+
raise Exception("Especify an id")
110+
elif not collection:
111+
raise Exception("Especify a collection")
112+
107113
# search() method only works with a list of ids.
108-
if type(id_) is not list:
109-
search.ids([id_])
114+
if type(scene_id) is not list:
115+
search.ids([scene_id])
110116
else:
111-
search.ids(id_)
117+
search.ids(scene_id)
118+
119+
search.collections(collection)
112120

113121
result = search()
114122
return result.featurescollection
@@ -180,7 +188,7 @@ def __download_gdf(
180188
mkdir(new_path)
181189
outdir = new_path
182190

183-
products_query = self.query_by_id(str(index))
191+
products_query = self.query_by_id(str(index), row.collection)
184192
products_query = ItemCollection(products_query).items()
185193
for product in products_query:
186194
for band in bands:
@@ -249,6 +257,18 @@ def to_geodataframe(products: Dict, crs: str = "EPSG:4326"):
249257
Returns:
250258
GeoDataFrame
251259
"""
252-
return GeoDataFrame.from_features(products, crs=crs).set_index(
253-
json_normalize(products["features"])["id"].values
254-
)
260+
items = [feat for feat in ItemCollection(products).items()]
261+
262+
for item in items:
263+
item_metadata = {}
264+
item_metadata.update(id=item.id)
265+
item_metadata.update(bbox=item.bbox)
266+
item_metadata.update(collection=item.collection)
267+
268+
for index, feature in enumerate(products.get("features")):
269+
if feature["id"] == item.id:
270+
products.get("features")[index].get("properties").update(
271+
**item_metadata
272+
)
273+
274+
return GeoDataFrame.from_features(products, crs=crs).set_index("id")

tests/test_Cbers4aAPI.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def mock_get(*args, **kwargs):
185185

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

188-
result = self.api.query_by_id("ABC123")
188+
result = self.api.query_by_id(scene_id="ABC123", collection="y")
189189

190190
assert self.expected_result == result
191191

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

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

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

200200
assert self.expected_result == result
201201

0 commit comments

Comments
 (0)