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d09de
 
 
 
 
 
 
f023554
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# main.py - FastAPI application for Flood Vulnerability Assessment
from fastapi import FastAPI, File, UploadFile, HTTPException, Request
from fastapi.responses import StreamingResponse, HTMLResponse
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel, field_validator
from typing import Optional, Dict
import pandas as pd
import io
import asyncio
from concurrent.futures import ThreadPoolExecutor

from spatial_queries import get_terrain_metrics, distance_to_water
from vulnerability import calculate_vulnerability_index
from gee_auth import initialize_gee
from height_predictor.inference import get_predictor
from height_predictor.get_height_gba import GlobalBuildingAtlasHeight

# SHAP Explainer Initialization
try:
    from explainability import VulnerabilityExplainer
    explainer = VulnerabilityExplainer()  # Automatically loads rf_explainer.pkl if present
    print("✅ SHAP model initialized successfully.")
except Exception as e:
    print(f"⚠️ SHAP explainer not available: {e}")
    explainer = None

# Initialize GEE once at startup
try:
    initialize_gee()
    print("✅ GEE initialized once at startup.")
except Exception as e:
    print(f"⚠️ GEE initialization failed at startup: {e}")

# APP INITIALIZATION
app = FastAPI(title="Flood Vulnerability Assessment API", version="1.0")

# Frontend templates setup
templates = Jinja2Templates(directory="templates")

# Thread pool for batch processing
executor = ThreadPoolExecutor(max_workers=10)

gba_getter = GlobalBuildingAtlasHeight()


# DATA MODEL
class SingleAssessment(BaseModel):
    latitude: float
    longitude: float
    height: Optional[float] = 0.0
    basement: Optional[float] = 0.0

    @field_validator('latitude')
    @classmethod
    def check_lat(cls, v: float) -> float:
        if not -90 <= v <= 90:
            raise ValueError('Latitude must be between -90 and 90')
        return v

    @field_validator('longitude')
    @classmethod
    def check_lon(cls, v: float) -> float:
        if not -180 <= v <= 180:
            raise ValueError('Longitude must be between -180 and 180')
        return v

    @field_validator('basement')
    @classmethod
    def check_basement(cls, v: float) -> float:
        if v > 0:
            raise ValueError('Basement height must be 0 or negative (e.g., -1, -2, -3)')
        return v


# FRONTEND ROUTE
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
    """Serve the main web interface"""
    return templates.TemplateResponse("index.html", {"request": request})


# API ROUTES
@app.get("/api")
async def root() -> Dict:
    """API info endpoint"""
    return {
        "service": "Flood Vulnerability Assessment API",
        "version": "1.0",
        "endpoints": {
            "POST /assess": "Assess single location",
            "POST /assess_batch": "Assess batch from CSV file",
            "GET /health": "Health check"
        }
    }


@app.post("/assess")
async def assess_single(data: SingleAssessment) -> Dict:
    """Assess flood vulnerability for a single location (non-blocking)."""
    loop = asyncio.get_event_loop()

    try:
        # Run slow terrain + water queries in a background thread
        terrain, water_dist = await loop.run_in_executor(
            None,
            lambda: (
                get_terrain_metrics(data.latitude, data.longitude),
                distance_to_water(data.latitude, data.longitude)
            )
        )

        # Calculate vulnerability after terrain + water distance retrieved
        result = calculate_vulnerability_index(
            lat=data.latitude,
            lon=data.longitude,
            height=data.height,
            basement=data.basement,
            terrain_metrics=terrain,
            water_distance=water_dist
        )

        return {
            "status": "success",
            "input": data.dict(),
            "assessment": result
        }

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Assessment failed: {e}")


@app.post("/predict_height")
async def predict_height(data: SingleAssessment) -> Dict:
    try:
        predictor = get_predictor()
        result = predictor.predict_from_coordinates(data.latitude, data.longitude)
        
        if result['status'] == 'error':
             raise HTTPException(status_code=500, detail=result['error'])
             
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


def process_single_row(row, use_predicted_height=False, use_gba_height=False):
    """Process a single row from CSV - used for parallel processing."""
    try:
        lat = row['latitude']
        lon = row['longitude']
        height = row.get('height', 0.0)
        basement = row.get('basement', 0.0)
        if use_gba_height:
            try:
                result = gba_getter.get_height_m(lat, lon, buffer_m=5.0)
                if result.get('status') == 'success' and result.get('predicted_height') is not None:
                    h = result['predicted_height']
                    if h >= 0:  # Only use valid positive heights
                        height = h
            except Exception as e:
                print(f"GBA height failed for {lat},{lon}: {e}")
        elif use_predicted_height:
            try:
                predictor = get_predictor()
                pred = predictor.predict_from_coordinates(lat, lon)
                if pred['status'] == 'success' and pred['predicted_height'] is not None:
                     height = pred['predicted_height']
            except Exception as e:
                print(f"Height prediction failed for {lat},{lon}: {e}")

        terrain = get_terrain_metrics(lat, lon)
        water_dist = distance_to_water(lat, lon)

        result = calculate_vulnerability_index(
            lat=lat,
            lon=lon,
            height=height,
            basement=basement,
            terrain_metrics=terrain,
            water_distance=water_dist
        )

        # CSV output - essential columns
        return {
            'latitude': lat,
            'longitude': lon,
            'height': height,
            'basement': basement,
            'vulnerability_index': result['vulnerability_index'],
            'ci_lower_95': result['confidence_interval']['lower_bound_95'],
            'ci_upper_95': result['confidence_interval']['upper_bound_95'],
            'vulnerability_level': result['risk_level'],
            'confidence': result['uncertainty_analysis']['confidence'],
            'confidence_interpretation': result['uncertainty_analysis']['interpretation'],
            'elevation_m': result['elevation_m'],
            'tpi_m': result['relative_elevation_m'],
            'slope_degrees': result['slope_degrees'],
            'distance_to_water_m': result['distance_to_water_m'],
            'quality_flags': ','.join(result['uncertainty_analysis']['data_quality_flags']) if result['uncertainty_analysis']['data_quality_flags'] else ''
        }

    except Exception as e:
        return {
            'latitude': row.get('latitude'),
            'longitude': row.get('longitude'),
            'height': row.get('height', 0.0),
            'basement': row.get('basement', 0.0),
            'error': str(e),
            'vulnerability_index': None,
            'ci_lower_95': None,
            'ci_upper_95': None,
            'risk_level': None,
            'confidence': None,
            'confidence_interpretation': None,
            'elevation_m': None,
            'tpi_m': None,
            'slope_degrees': None,
            'distance_to_water_m': None,
            'quality_flags': ''
        }


@app.post("/assess_batch")
async def assess_batch(file: UploadFile = File(...), use_predicted_height:bool=False, use_gba_height:bool=False) -> StreamingResponse:
    """Assess flood vulnerability for multiple locations from a CSV file."""
    try:
        contents = await file.read()
        df = pd.read_csv(io.StringIO(contents.decode('utf-8')))

        if 'latitude' not in df.columns or 'longitude' not in df.columns:
            raise HTTPException(
                status_code=400,
                detail="CSV must contain 'latitude' and 'longitude' columns"
            )

        import numpy as np
        df = df[(np.abs(df['latitude']) <= 90) & (np.abs(df['longitude']) <= 180)]
        if len(df) == 0:
            raise HTTPException(status_code=400, detail="No valid coordinates in CSV (lat -90..90, lon -180..180)")

        # Set defaults for optional columns
        if 'height' not in df.columns:
            df['height'] = 0.0
        if 'basement' not in df.columns:
            df['basement'] = 0.0

        loop = asyncio.get_event_loop()
        results = await loop.run_in_executor(
            executor,
            lambda: [process_single_row(row, use_predicted_height, use_gba_height) for _, row in df.iterrows()]
        )

        results_df = pd.DataFrame(results)
        output = io.StringIO()
        results_df.to_csv(output, index=False)
        output.seek(0)
        return StreamingResponse(
            io.BytesIO(output.getvalue().encode('utf-8')),
            media_type="text/csv",
            headers={
                "Content-Disposition": (
                    "attachment; filename=vulnerability_results.csv; "
                    "filename*=UTF-8''vulnerability_results.csv"
                )
            }
        )
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Batch processing failed: {str(e)}")
@app.post("/assess_batch_multihazard")
async def assess_batch_multihazard(file: UploadFile = File(...), use_predicted_height: bool = False, use_gba_height: bool = False) -> StreamingResponse:
    try:
        contents = await file.read()
        df = pd.read_csv(io.StringIO(contents.decode('utf-8')))

        if 'latitude' not in df.columns or 'longitude' not in df.columns:
            raise HTTPException(
                status_code=400,
                detail="CSV must contain 'latitude' and 'longitude' columns"
            )

        loop = asyncio.get_event_loop()
        from vulnerability import calculate_multi_hazard_vulnerability
        results = await loop.run_in_executor(
            executor,
            lambda: [process_single_row_multihazard(row, use_predicted_height, use_gba_height) for _, row in df.iterrows()]
        )

        results_df = pd.DataFrame(results)
        output = io.StringIO()
        results_df.to_csv(output, index=False)
        output.seek(0)
        return StreamingResponse(
            io.BytesIO(output.getvalue().encode('utf-8')),
            media_type="text/csv",
            headers={
                "Content-Disposition": (
                    "attachment; filename=multihazard_results.csv; "
                    "filename*=UTF-8''multihazard_results.csv"
                )
            }
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Batch multihazard failed: {str(e)}")
@app.post("/explain")
async def explain_assessment(data: SingleAssessment) -> Dict:
    """Assess vulnerability with SHAP explanation"""
    loop = asyncio.get_event_loop()

    try:
        # Run slow terrain + water queries in a background thread
        terrain, water_dist = await loop.run_in_executor(
            None,
            lambda: (
                get_terrain_metrics(data.latitude, data.longitude),
                distance_to_water(data.latitude, data.longitude)
            )
        )

        result = calculate_vulnerability_index(
            lat=data.latitude,
            lon=data.longitude,
            height=data.height,
            basement=data.basement,
            terrain_metrics=terrain,
            water_distance=water_dist
        )

        # Generate explanation if explainer available
        explanation = None
        if explainer:
            try:
                explanation = explainer.explain(result['components'])
            except Exception as e:
                print(f"SHAP explanation failed: {e}")

        return {
            "status": "success",
            "input": data.dict(),
            "assessment": result,
            "explanation": explanation
        }

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Assessment failed: {e}")


def process_single_row_multihazard(row, use_predicted_height=False, use_gba_height=False):
    """Process a single row with multi-hazard assessment."""
    try:
        from vulnerability import calculate_multi_hazard_vulnerability

        lat = row['latitude']
        lon = row['longitude']
        height = row.get('height', 0.0)
        basement = row.get('basement', 0.0)

        if use_gba_height:
            try:
                result = gba_getter.get_height_m(lat, lon, buffer_m=5.0)
                if result.get('status') == 'success' and result.get('predicted_height') is not None:
                    h = result['predicted_height']
                    if h >= 0:  # Only use valid positive heights
                        height = h
            except Exception as e:
                print(f"GBA height failed for {lat},{lon}: {e}")
        elif use_predicted_height:
             try:
                predictor = get_predictor()
                pred = predictor.predict_from_coordinates(lat, lon)
                if pred['status'] == 'success' and pred['predicted_height'] is not None:
                    height = pred['predicted_height']
             except Exception as e:
                print(f"Height prediction failed for {lat},{lon}: {e}")

        terrain = get_terrain_metrics(lat, lon)
        water_dist = distance_to_water(lat, lon)

        result = calculate_multi_hazard_vulnerability(
            lat=lat,
            lon=lon,
            height=height,
            basement=basement,
            terrain_metrics=terrain,
            water_distance=water_dist
        )

        return {
            'latitude': lat,
            'longitude': lon,
            'height': height,
            'basement': basement,
            'vulnerability_index': result['vulnerability_index'],
            'ci_lower_95': result['confidence_interval']['lower_bound_95'],
            'ci_upper_95': result['confidence_interval']['upper_bound_95'],
            'vulnerability_level': result['risk_level'],
            'confidence': result['uncertainty_analysis']['confidence'],
            'confidence_interpretation': result['uncertainty_analysis']['interpretation'],
            'elevation_m': result['elevation_m'],
            'tpi_m': result['relative_elevation_m'],
            'slope_degrees': result['slope_degrees'],
            'distance_to_water_m': result['distance_to_water_m'],
            'dominant_hazard': result['dominant_hazard'],
            'fluvial_risk': result['hazard_breakdown']['fluvial_riverine'],
            'coastal_risk': result['hazard_breakdown']['coastal_surge'],
            'pluvial_risk': result['hazard_breakdown']['pluvial_drainage'],
            'combined_risk': result['hazard_breakdown']['combined_index'],
            'quality_flags': ','.join(result['uncertainty_analysis']['data_quality_flags'])
                     if result['uncertainty_analysis']['data_quality_flags'] else ''
        }

    except Exception as e:
        return {
            'latitude': row.get('latitude'),
            'longitude': row.get('longitude'),
            'height': row.get('height', 0.0),
            'basement': row.get('basement', 0.0),
            'error': str(e),
            'vulnerability_index': None
        }


@app.post("/assess_multihazard")
async def assess_multihazard(data: SingleAssessment) -> Dict:
    """Multi-hazard assessment (fluvial + coastal + pluvial)"""
    loop = asyncio.get_event_loop()

    try:
        from vulnerability import calculate_multi_hazard_vulnerability

        # Run slow terrain + water queries in a background thread
        terrain, water_dist = await loop.run_in_executor(
            None,
            lambda: (
                get_terrain_metrics(data.latitude, data.longitude),
                distance_to_water(data.latitude, data.longitude)
            )
        )

        result = calculate_multi_hazard_vulnerability(
            lat=data.latitude,
            lon=data.longitude,
            height=data.height,
            basement=data.basement,
            terrain_metrics=terrain,
            water_distance=water_dist
        )

        return {
            "status": "success",
            "input": data.dict(),
            "assessment": result
        }

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Assessment failed: {e}")

@app.post("/get_height_gba")
async def get_height_gba(data: SingleAssessment):
    try:
        result = gba_getter.get_height_m(data.latitude, data.longitude, buffer_m=5.0)

        if result.get("status") != "success":
            raise HTTPException(status_code=404, detail="GBA height not found for this location. Please try predicting the height.")

        return result
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/health")
async def health_check() -> Dict:
    """Health check endpoint."""
    return {"status": "healthy", "gee_initialized": True}