This project provides a simple FastAPI-based API endpoint to calculate NDVI (Normalized Difference Vegetation Index) statistics over a given polygon using SentinelHub services.
- Accepts GeoJSON input to define an area of interest.
- Queries Sentinel-2 L2A data through SentinelHub.
- Calculates daily NDVI values for the given period (
2022-01-01to2022-11-30). - Returns histogram-based NDVI statistics.
git clone https://your-repo-url.git
cd your-repo-folderpip install fastapi uvicorn pydantic shapely sentinelhubMake sure you set the correct SentinelHub credentials:
config.instance_id = 'YOUR_INSTANCE_ID'
config.sh_client_id = 'YOUR_CLIENT_ID'
config.sh_client_secret = 'YOUR_CLIENT_SECRET'
Alternatively, you can configure them through environment variables or sentinelhub.config file.
uvicorn main:app --reloadThe API will be available at http://127.0.0.1:8000.
Endpoint
POST /ndvi
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[13.822174072265625, 45.85080395917834],
[14.5599365234375, 45.85080395917834],
[14.5599365234375, 46.29191774991382],
[13.822174072265625, 46.29191774991382],
[13.822174072265625, 45.85080395917834]
]
]
}
}
]
}Returns a JSON containing NDVI histogram statistics for the given area over the specified time interval.
- FastAPI – API Framework
- SentinelHub-py – SDK to interact with SentinelHub APIs
- Pydantic – Data validation
- Shapely – GeoJSON geometry parsing
- Be aware that the SentinelHub Service Worker can rate limit you based on your subscription plan.
- The NDVI calculation uses the formula:
NDVI = (NIR - RED) / (NIR + RED)