GIS data I/O
The GIS I/O module provides functions for reading geospatial data in various formats, including raster (GeoTIFF) and vector (Shapefile) files.
Overview
MOBIDICpy uses industry-standard libraries for geospatial data handling:
- Rasterio for raster data (GeoTIFF)
- GeoPandas for vector data (Shapefiles, GeoJSON, etc.)
All functions provide comprehensive logging and error handling, converting nodata values to NaN for consistent numerical processing.
Functions
Raster I/O
Read raster grid file and return data array with corner coordinates. This function replicates the behavior of MATLAB’s function for GeoTIFF files.
Reads raster files in GeoTIFF (.tif, .tiff) format. Returns the data array and corner coordinates adjusted to cell centers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gridname
|
str | Path
|
Path to the grid file (.tif, .tiff). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing: - ‘data’: 2D numpy array with raster values (NaN for nodata) - ‘xllcorner’: X coordinate of lower-left corner (cell center) - ‘yllcorner’: Y coordinate of lower-left corner (cell center) - ‘cellsize’: Cell size in map units |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the grid file does not exist. |
ValueError
|
If the file format is not supported. |
RuntimeError
|
If there are errors reading the file. |
Notes
- Corner coordinates are adjusted to cell centers (0.5 * cellsize offset)
- Data is flipped vertically to match MATLAB convention
- Very small values (< -1e32) are converted to NaN
Examples:
>>> result = grid_to_matrix('elevation.tif')
>>> data = result['data']
>>> print(f"Shape: {data.shape}, Resolution: {result['cellsize']}m")
Source code in mobidic/preprocessing/gis_reader.py
Vector I/O
Read a shapefile and return a GeoDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Path to the shapefile (.shp). |
required |
crs
|
str | None
|
Optional CRS to reproject the data. If None, keeps original CRS. |
None
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
GeoDataFrame containing the shapefile data. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the shapefile does not exist. |
Exception
|
If the file cannot be read by geopandas. |
Examples:
>>> river_network = read_shapefile("example/shp/Arno_river_network.shp")
>>> reach_ids = river_network['REACH_ID']
Source code in mobidic/preprocessing/gis_reader.py
Examples
Reading a Raster
from mobidic import grid_to_matrix
import numpy as np
# Read a Digital Terrain Model
dtm = grid_to_matrix("path/to/dtm.tif")
# Access raster data and metadata
print(f"Shape: {dtm['data'].shape}")
print(f"Cell size: {dtm['cellsize']} m")
print(f"Lower-left corner: ({dtm['xllcorner']}, {dtm['yllcorner']})")
print(f"CRS: {dtm['crs']}")
# Access the data array (nodata converted to NaN)
elevation = dtm['data']
mean_elevation = np.nanmean(elevation)
print(f"Mean elevation: {mean_elevation:.2f} m")
Reading a shapefile
from mobidic import read_shapefile
# Read river network shapefile
network = read_shapefile("path/to/river_network.shp")
# Access as GeoDataFrame
print(f"Number of reaches: {len(network)}")
print(f"CRS: {network.crs}")
print(network.head())
# Reproject to a different CRS
network_utm = read_shapefile(
"path/to/river_network.shp",
crs="EPSG:32632" # WGS84 / UTM zone 32N
)
Return values
Raster data
grid_to_matrix() returns a dictionary with:
data(numpy.ndarray): 2D array of raster values, nodata converted to NaN (flipped vertically to match MATLAB convention)xllcorner(float): X coordinate of lower-left corner (cell center)yllcorner(float): Y coordinate of lower-left corner (cell center)cellsize(float): Cell size in map unitscrs(rasterio.crs.CRS): Coordinate reference system
Vector data
read_shapefile() returns a geopandas.GeoDataFrame with geometry and attribute columns.
Error handling
Both functions provide detailed error messages and logging:
- File not found:
FileNotFoundErrorwith clear message - Invalid format:
RasterioIOErrororRuntimeErrorwith details - CRS issues: Warnings when CRS is missing or reprojection fails
- Logging: All operations logged using loguru (DEBUG, INFO, SUCCESS, ERROR levels)