water module
Water body segmentation using OmniWaterMask.
This module provides a high-level interface for detecting water bodies in satellite and aerial imagery using the OmniWaterMask library. It supports a wide range of sensors (Sentinel-2, NAIP, Landsat, etc.) and resolutions (0.2m to 50m) by combining a sensor-agnostic deep learning model with NDWI calculations and OpenStreetMap reference data.
Reference
BAND_ORDER_PRESETS = {'naip': [1, 2, 3, 4], 'sentinel2': [3, 2, 1, 4], 'landsat': [4, 3, 2, 5]}
module-attribute
¶
Predefined band order mappings for common sensors.
Each preset maps to a list of 1-based band indices in the order [Red, Green, Blue, NIR] as used by rasterio.
"naip": R, G, B, NIR (bands 1-4)"sentinel2": For 6-band composites (B2, B3, B4, B8, B11, B12), maps Red=B4 (band 3), Green=B3 (band 2), Blue=B2 (band 1), NIR=B8 (band 4)"landsat": For Landsat 8/9 (B1-B7), maps Red=B4 (band 4), Green=B3 (band 3), Blue=B2 (band 2), NIR=B5 (band 5)
segment_water(input_path, band_order='naip', output_raster=None, output_vector=None, batch_size=4, device=None, dtype='float32', no_data_value=0, patch_size=1000, overlap_size=300, use_osm_water=True, use_osm_building=True, use_osm_roads=True, cache_dir=None, model_dir=None, overwrite=True, min_area=10, smooth=True, smooth_iterations=3, verbose=True, **kwargs)
¶
Segment water bodies from satellite or aerial imagery using OmniWaterMask.
Uses a sensor-agnostic deep learning model combined with NDWI and OpenStreetMap data to detect water bodies in imagery ranging from 0.2m to 50m resolution. Supports Sentinel-2, NAIP, Landsat, and other multispectral sensors with Red, Green, Blue, and NIR bands.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
Union[str, Path, List[Union[str, Path]]]
|
Path to input GeoTIFF file(s). Can be a single path (string or Path) or a list of paths for batch processing. When a list is provided, each scene is processed and output files are named based on each input filename. |
required |
band_order
|
Union[List[int], str]
|
Band indices for Red, Green, Blue, NIR channels
(1-based, as used by rasterio). Can be a list of 4 integers
or a string preset: |
'naip'
|
output_raster
|
Optional[str]
|
Path to save the output water mask GeoTIFF. If None,
derives from input filename (e.g., |
None
|
output_vector
|
Optional[str]
|
Path to save vectorized water body polygons (e.g., GeoJSON, GPKG, Shapefile). If provided, the raster mask is converted to vector polygons. For multiple inputs, this is ignored and output names are derived from each input filename using the same extension. Defaults to None. |
None
|
batch_size
|
int
|
Number of scenes to process in parallel. Defaults to 4. |
4
|
device
|
Optional[str]
|
Device for inference (e.g., |
None
|
dtype
|
str
|
Data type for model inference precision. One of
|
'float32'
|
no_data_value
|
int
|
Value representing no-data pixels in the input imagery. Defaults to 0. |
0
|
patch_size
|
int
|
Size of patches for sliding-window inference in pixels. Defaults to 1000. |
1000
|
overlap_size
|
int
|
Overlap between patches to reduce edge artifacts in pixels. Defaults to 300. |
300
|
use_osm_water
|
bool
|
Include OpenStreetMap water features to improve accuracy. Defaults to True. |
True
|
use_osm_building
|
bool
|
Include OSM building data to reduce false positives in built-up areas. Defaults to True. |
True
|
use_osm_roads
|
bool
|
Include OSM road data to reduce false positives along roads. Defaults to True. |
True
|
cache_dir
|
Optional[str]
|
Directory for caching intermediate results and OSM data.
If None, uses |
None
|
model_dir
|
Optional[str]
|
Custom directory to store/load OmniWaterMask model files. If None, uses the default location. Defaults to None. |
None
|
overwrite
|
bool
|
Whether to overwrite existing output files. Defaults to True. |
True
|
min_area
|
float
|
Minimum polygon area in square map units to keep during vectorization. Defaults to 10. |
10
|
smooth
|
bool
|
Whether to smooth vectorized polygons using the smoothify
library. Only applies when |
True
|
smooth_iterations
|
int
|
Number of smoothing iterations. Higher values
produce smoother boundaries. Only applies when |
3
|
verbose
|
bool
|
Whether to print progress messages. Defaults to True. |
True
|
**kwargs
|
Any
|
Additional keyword arguments passed to
|
{}
|
Returns:
| Type | Description |
|---|---|
Union[str, GeoDataFrame, List[str], List[GeoDataFrame]]
|
For a single input file:
If |
Union[str, GeoDataFrame, List[str], List[GeoDataFrame]]
|
For multiple input files: Returns a list of results (list of str paths or list of GeoDataFrames), one per input file. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If omniwatermask is not installed. |
ValueError
|
If |
FileNotFoundError
|
If the input file(s) do not exist. |
Example
import geoai
NAIP imagery (R, G, B, NIR)¶
mask_path = geoai.segment_water( ... "naip_scene.tif", ... band_order="naip", ... output_raster="water_mask.tif", ... )
Sentinel-2 with vectorization and smoothing¶
gdf = geoai.segment_water( ... "sentinel2_scene.tif", ... band_order="sentinel2", ... output_raster="water_mask.tif", ... output_vector="water_bodies.geojson", ... smooth=True, ... smooth_iterations=3, ... min_area=100, ... )
Batch processing multiple files¶
results = geoai.segment_water( ... ["scene1.tif", "scene2.tif"], ... band_order="sentinel2", ... )
Source code in geoai/water.py
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 | |