HyperAI超神経
Back to Headlines

Building a Basic Flood Inundation Model with Python and USGS DEM Data

9日前

Flood events have grown increasingly frequent and severe due to climate change, making flood modeling crucial for risk assessment and disaster management. This article guides beginners through building a basic flood inundation model using Python and a Digital Elevation Model (DEM), enabling them to visualize and understand flood risks dynamically. What is a Digital Elevation Model (DEM)? A Digital Elevation Model (DEM) is a numerical representation of the Earth's terrain, storing elevation values in a grid-like structure known as raster data. Unlike digital images that capture color, DEMs focus on height data, often excluding surface features like buildings and vegetation. DEMs are essential in fields such as mapping, hydrology, and environmental science. Reliable and free sources of DEM data include the USGS National Map, NASA Earthdata, and the Shuttle Radar Topography Mission (SRTM). For this project, we use a DEM from the USGS National Geospatial Program, covering a 1° × 1° tile in Northeast Brazil with a spatial resolution of 1 arc second (about 30 meters at the equator). Loading and Visualizing Elevation Data with Python To begin, we import the necessary libraries: rasterio for reading DEM files, matplotlib.pyplot for plotting, and numpy for numerical operations. python import rasterio import matplotlib.pyplot as plt import numpy as np from matplotlib.animation import FuncAnimation Next, we define a helper function to load and handle the DEM file: ```python def load_dem(path): with rasterio.open(path) as src: dem = src.read(1) transform = src.transform nodata = src.nodata if nodata is not None: dem = np.ma.masked_equal(dem, nodata) return dem, transform ``` This function reads the elevation data and masks out no-data values, ensuring they don't interfere with our analysis or visualization. Initially, the DEM is plotted with pixel coordinates. However, for better geographic interpretation, we adjust the plot to display latitude and longitude using the extent parameter: ```python dem, transform = load_dem("s06_w039_1arc_v3.tif") extent = [ transform[2], # xmin (longitude) transform[2] + transform[0] * dem.shape[1], # xmax transform[5] + transform[4] * dem.shape[0], # ymin (latitude) transform[5] # ymax ] fig, ax = plt.subplots() img = ax.imshow(dem, cmap='terrain', extent=extent, origin='upper') ax.set_xlabel('Longitude') ax.set_ylabel('Latitude') plt.colorbar(img, label='Elevation (m)') plt.title('DEM Visualization') plt.show() ``` The resulting visualization clearly illustrates the elevation data within the area of interest (AOI) in Northeast Brazil. Simulating Flood Scenarios with Elevation Thresholds To simulate flood scenarios, we define a height threshold and generate a binary mask that identifies areas below this level. For instance, we simulate flooding for areas with elevations below 40 meters: ```python flood_threshold = 40 # meters flood_mask = (dem <= flood_threshold).astype(int) plt.imshow(flood_mask, extent=extent, cmap='Blues') plt.title(f"Flooded Area (Threshold: {flood_threshold}m)") plt.xlabel("Longitude") plt.ylabel("Latitude") plt.show() ``` This static visualization helps identify flooded regions but does not show the flood's progression over time. To address this, we use matplotlib's FuncAnimation to create a dynamic flood simulation. Animating Flood Progression with Python We simulate a progressive flood by incrementally increasing the water level and updating the flood mask at each step. The following code animates the flood across various water levels: ```python flood_levels = np.arange(15, 100, 5) fig, ax = plt.subplots() img = ax.imshow(dem, cmap='terrain', extent=extent, origin='upper') flood_overlay = ax.imshow(np.zeros_like(dem), cmap='Blues', alpha=0.4, extent=extent, origin='upper') title = ax.set_title("") ax.set_xlabel("Longitude") ax.set_ylabel("Latitude") def update(frame): level = flood_levels[frame] mask = np.where(dem <= level, 1, np.nan) flood_overlay.set_data(mask) title.set_text(f"Flood Level: {level} m") return flood_overlay, title ani = FuncAnimation(fig, update, frames=len(flood_levels), interval=300, blit=True) plt.tight_layout() plt.show() ani.save("flood_simulation.gif", writer='pillow', fps=5) ``` This dynamic simulation provides a clear visualization of how floodwaters spread over the AOI as the water level rises. Conclusion and Next Steps This article demonstrates a straightforward workflow for simulating flood inundation using Python and a DEM, offering valuable insights for educators, students, and analysts. While it doesn't employ the most sophisticated hydraulic models, it serves as an accessible starting point for those new to geospatial data and flood modeling. Advanced techniques such as physically-based models and more complex algorithms can be explored for deeper analysis. Industry insiders praise this approach for its simplicity and effectiveness in conveying flood risks visually. The U.S. Geological Survey (USGS) continues to be a leading provider of free and accurate geospatial data, facilitating research and applications in environmental science and disaster response. This beginner-friendly guide paves the way for further exploration and customization, encouraging users to apply similar methods to their local areas or integrate more advanced techniques. The complete code is available on GitHub, allowing readers to experiment and adapt the model to their specific needs. For those interested in animation with Python, the tutorial referenced in the article is a valuable resource. References U.S. Geological Survey. National Map. U.S. Department of the Interior. Retrieved May 17, 2025, from National Map. U.S. Geological Survey. What is a digital elevation model (DEM)? U.S. Department of the Interior. Retrieved May 17, 2025, from What is a DEM?. Gillies, S. Georeferencing — Rasterio documentation (stable). Rasterio. Retrieved May 27, 2025, from Georeferencing. Gillies, Sean. Affine Transforms — Rasterio Documentation (Latest). Accessed May 27, 2025. Affine Transforms. Data Source DEM data used in this project is provided by the U.S. Geological Survey (USGS) through the National Map and is in the public domain.

Related Links