Replies: 1 comment
-
I can't replicate import httpx
import numpy
from io import BytesIO
titiler_endpoint = "https://titiler.xyz"
def fetch_bbox_array(sceneid, bbox, assets=None, expression=None, **kwargs):
"""Helper function to fetch and decode Numpy array using Titiler endpoint."""
# STAC ITEM URL
stac_item = f"https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/{sceneid}"
xmin, ymin, xmax, ymax = bbox
# TiTiler required URL + asset or expression parameters
params = (("url", stac_item), ("max_size", 1024))
if assets:
for asset in assets:
params += (("assets", asset),)
elif expression:
params += (
("expression", expression),
("asset_as_band", True),
)
else:
raise Exception("Missing band or expression input")
params += tuple(kwargs.items())
# TITILER ENDPOINT
url = f"{titiler_endpoint}/stac/bbox/{xmin},{ymin},{xmax},{ymax}.npy"
r = httpx.get(url, params=params)
data = numpy.load(BytesIO(r.content))
return sceneid, data[0:-1], data[-1]
sceneid = "S2A_30TVT_20221112_0_L2A"
bbox = [-4.5832327366838115, 47.739489692621845, -3.995851378023275, 48.061273530675265]
_, data, mask = fetch_bbox_array(sceneid, bbox, assets=["B02"])
mask
>>> array([[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
...,
[ 0, 0, 0, ..., 255, 255, 255],
[ 0, 0, 0, ..., 255, 255, 255],
[ 0, 0, 0, ..., 255, 255, 255]],
shape=(561, 1024), dtype=uint16) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I was trying to make use of the
mask
results that are returned by TiTiler when you request the API for a Numpy array. Specifically, you explain exactly how to do this in your documentation example Working with STAC - At Scale. One can simply reproduce your own documentation step by step in a notebook to see what I will discuss.In the example, the
fetch_bbox_array
returns a tuple of 3 items, which first item is the Sentinel-2 satellite produce ID, the second one a Numpy array with the data contained in the requestedbbox
, and the last one is a mask which I just assume it will define asFalse
the numpy array items that falls into the initial polygon, so one could apply this mask to the bbox results array and get only the results for the polygon. At least that was my expectation, please forgive if I am incorrect.The case is that I invariably get the
mask
results as a full array of255.
values, regardless of the polygon I submit (tried with some farmlands in UK and Spain, with always same result). So it seems to me that I am missing to do something else that will indeed return the mask array as I was expecting, as I described above?Thanks for your time.
Beta Was this translation helpful? Give feedback.
All reactions