Get statistics of a derived band (e.g., NDVI) #429
-
How to get the statistics of a derived band (e.g., NDVI). For this sample dataset, NDVI= https://developmentseed.org/titiler/endpoints/stac/#statistics import requests
titiler_endpoint = "https://titiler.xyz"
url = "https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json"
params = {
'url': url,
'assets': ['B4', 'B3']
}
r = requests.get(f"{titiler_endpoint}/stac/statistics", params=params).json()
r |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
@giswqs you are right, this features has been remove in rio-tiler 3.0 (so in titiler 4.0) because it was complex and didn't fit with the new definition of the function. It will pretty simple for someone to create a custom endpoint from fastapi import FastAPI, Depends
from titiler.core.factory import MultiBaseTilerFactory
from titiler.core.dependencies import AssetsBidxExprParams
from titiler.core.models.responses import Statistics
from rio_tiler.io import STACReader
from rio_tiler.models import BandStatistics
app = FastAPI()
# Create a Default set of endpoint using the MultiBaseTilerFactory
stac = MultiBaseTilerFactory(reader=STACReader)
# GET endpoint
@stac.router.get(
"/advanced_statistics",
response_class=JSONResponse,
response_model=Statistics,
responses={
200: {
"content": {"application/json": {}},
"description": "Return dataset's statistics.",
}
},
)
def statistics(
src_path=Depends(stac.path_dependency),
asset_params=Depends(AssetsBidxExprParams),
dataset_params=Depends(stac.dataset_dependency),
image_params=Depends(stac.img_dependency),
stats_params=Depends(stac.stats_dependency),
histogram_params=Depends(stac.histogram_dependency),
):
with rasterio.Env(**self.gdal_config):
with self.reader(src_path) as src_dst:
data = src_dst.preview(
**asset_params,
**dataset_params,
**image_params,
)
stats = get_array_statistics(
data.as_masked(),
**stats_params,
hist_options={**histogram_params},
)
return {
f"{data.band_names[ix]}": BandStatistics(**stats[ix])
for ix in range(len(stats))
}
app.include_router(cog.router) |
Beta Was this translation helpful? Give feedback.
-
@vincentsarago Thanks for sharing the script for creating a custom endpoint. The reason I wanted the statistics of a derived band is to get the @TomAugspurger Just want to keep in the loop in case you run into this problem later. I have updated leafmap and the notebook exmaple. It works fine with the latest Planetary Computer Data API. |
Beta Was this translation helpful? Give feedback.
-
I just released titiler==0.5.0 which revert the
|
Beta Was this translation helpful? Give feedback.
I just released titiler==0.5.0 which revert the
/stac/statistics
endpoint to something that should be compatible with the previous usage (using expression). Thestatistics
endpoint from0.4
is now calledasset_statistics
.