|
313 | 313 | " 'date_diff_days': date_diff_days,\n", |
314 | 314 | "})\n", |
315 | 315 | "\n", |
316 | | - "ndays = 15 # This will be our threshold for \"nearby\" dates in the next step\n", |
317 | | - "# Filter to pairs whose dates are within ±15 days of each other\n", |
| 316 | + "ndays = 7 # This will be our threshold for \"nearby\" dates in the next step\n", |
| 317 | + "\n", |
| 318 | + "# Filter to pairs whose dates are within ndays of each other\n", |
318 | 319 | "nearby = pairs[pairs['date_diff_days'] <= ndays].reset_index(drop=True)\n", |
319 | 320 | "print(f'Intersecting polygon pairs with dates within ±{ndays} days: {len(nearby):>10,}')\n", |
320 | 321 | "nearby.head(10)" |
|
363 | 364 | "gf" |
364 | 365 | ] |
365 | 366 | }, |
| 367 | + { |
| 368 | + "cell_type": "markdown", |
| 369 | + "id": "1a7b15e6", |
| 370 | + "metadata": {}, |
| 371 | + "source": [ |
| 372 | + "## Aggregate Flood Events to Grid\n", |
| 373 | + "\n", |
| 374 | + "We now count the number of unique flood events per grid cell." |
| 375 | + ] |
| 376 | + }, |
366 | 377 | { |
367 | 378 | "cell_type": "markdown", |
368 | 379 | "id": "c56357b6", |
369 | 380 | "metadata": {}, |
370 | 381 | "source": [ |
371 | | - "Reproject the filtered flood points to EPSG:7755 — done once as a shared step before comparing both counting approaches." |
| 382 | + "Reproject the filtered flood points to the chosen CRS." |
| 383 | + ] |
| 384 | + }, |
| 385 | + { |
| 386 | + "cell_type": "code", |
| 387 | + "execution_count": null, |
| 388 | + "id": "76c29140", |
| 389 | + "metadata": {}, |
| 390 | + "outputs": [], |
| 391 | + "source": [ |
| 392 | + "gf_reprojected = gf.to_crs(grid_crs)" |
372 | 393 | ] |
373 | 394 | }, |
374 | 395 | { |
|
386 | 407 | "metadata": {}, |
387 | 408 | "outputs": [], |
388 | 409 | "source": [ |
389 | | - "gf_reprojected = gf.to_crs(grid_crs)\n", |
390 | 410 | "joined = gpd.sjoin(grid_india, gf_reprojected, how='inner', predicate='intersects')\n", |
391 | 411 | "joined" |
392 | 412 | ] |
|
398 | 418 | "metadata": {}, |
399 | 419 | "outputs": [], |
400 | 420 | "source": [ |
401 | | - "counts_a = joined.groupby(joined.index)['flood_event'].nunique().rename('flood_count')\n", |
| 421 | + "counts = joined.groupby(joined.index)['flood_event'].nunique().rename('flood_count')\n", |
402 | 422 | "\n", |
403 | | - "grid_india_a = grid_india.copy()\n", |
404 | | - "grid_india_a['flood_count'] = grid_india_a.index.map(counts_a).fillna(0).astype(int)\n", |
405 | | - "print(grid_india_a['flood_count'].describe())" |
| 423 | + "grid_country['flood_count'] = grid_country.index.map(counts).fillna(0).astype(int)\n", |
| 424 | + "grid_country.head()" |
406 | 425 | ] |
407 | 426 | }, |
408 | 427 | { |
|
415 | 434 | "from matplotlib.cm import ScalarMappable\n", |
416 | 435 | "from matplotlib.patches import Patch\n", |
417 | 436 | "\n", |
418 | | - "plot_data = grid_india_a.copy()\n", |
419 | | - "plot_data['flood_count'] = plot_data['flood_count'].fillna(0).astype(int)\n", |
420 | | - "\n", |
421 | 437 | "fig, ax = plt.subplots(figsize=(10, 12))\n", |
422 | 438 | "\n", |
423 | 439 | "# Cells with no observations in neutral grey\n", |
424 | | - "plot_data[plot_data['flood_count'] == 0].plot(\n", |
| 440 | + "grid_country[grid_country['flood_count'] == 0].plot(\n", |
425 | 441 | " ax=ax, color='#d9d9d9', linewidth=0\n", |
426 | 442 | ")\n", |
427 | 443 | "\n", |
428 | | - "# Cells with observations coloured on a log scale\n", |
| 444 | + "# Cells with observations coloured by flood count\n", |
429 | 445 | "# (flood counts are heavily right-skewed; log scale reveals spatial variation)\n", |
430 | | - "non_zero = plot_data[plot_data['flood_count'] > 0]\n", |
431 | | - "vmax = non_zero['flood_count'].max()\n", |
| 446 | + "norm = mcolors.LogNorm(vmin=1, vmax=grid_country['flood_count'].max())\n", |
432 | 447 | "cmap = 'YlOrRd'\n", |
433 | 448 | "\n", |
| 449 | + "non_zero = grid_country[grid_country['flood_count'] > 0]\n", |
434 | 450 | "non_zero.plot(\n", |
435 | 451 | " ax=ax, column='flood_count',\n", |
436 | | - " cmap=cmap, vmin=1, vmax=50, linewidth=0\n", |
| 452 | + " cmap=cmap, norm=norm, linewidth=0\n", |
437 | 453 | ")\n", |
438 | 454 | "\n", |
439 | 455 | "# Country boundary\n", |
440 | 456 | "country_proj.plot(ax=ax, facecolor='none', edgecolor='#333333', linewidth=0.8)\n", |
441 | 457 | "\n", |
442 | | - "ax.set_title('Flood Risk Map — India (2000–2025)\\n10 km × 10 km grid, observation count per cell', fontsize=13, pad=12)\n", |
| 458 | + "ax.set_title('Flood Frequency Map (2000–2025)', fontsize=13, pad=12)\n", |
443 | 459 | "ax.axis('off')\n", |
444 | 460 | "plt.tight_layout()\n", |
445 | 461 | "plt.show()" |
|
0 commit comments