Confirm Geometry and Add Index API
Browse files
app.py
CHANGED
|
@@ -817,6 +817,57 @@ with gr.Blocks(theme=theme, title="Kamlan: KML Analyzer") as demo:
|
|
| 817 |
</div>
|
| 818 |
""")
|
| 819 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 820 |
|
| 821 |
# Mount the Gradio app onto the FastAPI app
|
| 822 |
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 817 |
</div>
|
| 818 |
""")
|
| 819 |
|
| 820 |
+
def compute_index_histogram(file_url: str, index_name: str, start_date: str, end_date: str):
|
| 821 |
+
"""Computes histogram percentage for a vegetation index within a polygon and date range."""
|
| 822 |
+
one_time_setup()
|
| 823 |
+
decoded_url = unquote(file_url)
|
| 824 |
+
gdf = get_gdf_from_url(decoded_url)
|
| 825 |
+
gdf = preprocess_gdf(gdf)
|
| 826 |
+
|
| 827 |
+
geometry_gdf = next((gdf.iloc[[i]] for i in range(len(gdf)) if is_valid_polygon(gdf.iloc[[i]])), None)
|
| 828 |
+
if geometry_gdf is None:
|
| 829 |
+
raise HTTPException(status_code=400, detail="No valid polygon found.")
|
| 830 |
+
|
| 831 |
+
ee_geometry = ee.Geometry(json.loads(geometry_gdf.to_crs(4326).to_json())['features'][0]['geometry'])
|
| 832 |
+
|
| 833 |
+
evi_vars = {'G': 2.5, 'C1': 6.0, 'C2': 7.5, 'L': 1.0, 'C': 2.4}
|
| 834 |
+
collection = (
|
| 835 |
+
ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED")
|
| 836 |
+
.filterDate(start_date, end_date)
|
| 837 |
+
.select(["B2", "B3", "B4", "B8"], ["Blue", "Green", "Red", "NIR"])
|
| 838 |
+
.map(lambda img: add_indices(img, 'NIR', 'Red', 'Blue', 'Green', evi_vars))
|
| 839 |
+
.filterBounds(ee_geometry)
|
| 840 |
+
)
|
| 841 |
+
|
| 842 |
+
if collection.size().getInfo() == 0:
|
| 843 |
+
raise HTTPException(status_code=404, detail="No imagery found for the polygon in given date range.")
|
| 844 |
+
|
| 845 |
+
# Use median composite to avoid cloud spikes
|
| 846 |
+
mosaic = collection.median()
|
| 847 |
+
|
| 848 |
+
bins = [0, 0.2, 0.4, 0.6, 0.8, 1.0]
|
| 849 |
+
histogram, _ = get_histogram(index_name, mosaic.select(index_name), ee_geometry, bins)
|
| 850 |
+
|
| 851 |
+
total = histogram.sum()
|
| 852 |
+
result = {}
|
| 853 |
+
for i in range(len(bins) - 1):
|
| 854 |
+
label = f"{bins[i]}-{bins[i+1]}"
|
| 855 |
+
pct = (histogram[i] / total * 100) if total > 0 else 0
|
| 856 |
+
result[label] = round(pct, 2)
|
| 857 |
+
|
| 858 |
+
return result
|
| 859 |
+
|
| 860 |
+
|
| 861 |
+
# Register endpoints for multiple indices with date range
|
| 862 |
+
for idx in ["NDVI", "EVI", "EVI2", "RandomForest", "CI", "GujVDI"]:
|
| 863 |
+
endpoint_path = f"/api/{idx}"
|
| 864 |
+
|
| 865 |
+
async def index_hist_endpoint(file_url: str, start_date: str, end_date: str, index_name=idx):
|
| 866 |
+
return compute_index_histogram(file_url, index_name, start_date, end_date)
|
| 867 |
+
|
| 868 |
+
app.get(endpoint_path)(index_hist_endpoint)
|
| 869 |
+
|
| 870 |
+
|
| 871 |
|
| 872 |
# Mount the Gradio app onto the FastAPI app
|
| 873 |
app = gr.mount_gradio_app(app, demo, path="/")
|