Skip to content

geosdhydro

geosdhydro package.

GIS tools for semi-distributed hydrologic modelling

Classes:

Functions:

  • get_parser

    Return the CLI argument parser.

  • main

    Run the main program.

ShapefileToSwiftConverter

ShapefileToSwiftConverter(
    gdf: GeoDataFrame,
    include_coordinates: bool = False,
    linkid_field: str = "LinkID",
    fromnodeid_field: str = "FromNodeID",
    tonodeid_field: str = "ToNodeID",
    spathlen_field: str = "SPathLen",
    darea2_field: str = "DArea2",
    geometry_field: str = "geometry",
    linkname_field: Optional[str] = None,
    subarea_name_field: Optional[str] = None,
    node_names: Optional[Dict[str, str]] = None,
)

Converts shapefile data to SWIFT JSON catchment structure.

Parameters:

  • gdf (GeoDataFrame) –

    GeoDataFrame loaded from shapefile containing link data

  • include_coordinates (bool, default: False ) –

    Whether to include lat/lon in node definitions

  • linkid_field (str, default: 'LinkID' ) –

    Name of the column containing Link IDs

  • fromnodeid_field (str, default: 'FromNodeID' ) –

    Name of the column containing From Node IDs

  • tonodeid_field (str, default: 'ToNodeID' ) –

    Name of the column containing To Node IDs

  • spathlen_field (str, default: 'SPathLen' ) –

    Name of the column containing Stream Path Lengths (in meters)

  • darea2_field (str, default: 'DArea2' ) –

    Name of the column containing Subarea Drainage Area (in square meters)

  • geometry_field (str, default: 'geometry' ) –

    Name of the column containing geometry data

  • linkname_field (Optional[str], default: None ) –

    Name of the column containing Link Names (optional)

  • subarea_name_field (Optional[str], default: None ) –

    Name of the column containing SubArea Names (optional)

  • node_names (Optional[Dict[str, str]], default: None ) –

    Optional mapping of node IDs to names (optional)

Methods:

  • convert

    Convert shapefile data to SWIFT JSON format.

  • save_to_file

    Save converted data to JSON file.

Attributes:

  • gdf (GeoDataFrame) –

    The geodataframe from which we build the json file.

  • include_coordinates (bool) –

    Should the Latitude/Longitude coordinates be derived from the geometry and written in the json file.

  • routing_model (dict) –

    Dictionary for the routing model sections of the json file.

  • runoff_model (dict) –

    Dictionary for the rainfall-runoff model sections of the json file.

Source code in src/geosdhydro/_internal/swift.py
 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
def __init__(
    self,
    gdf: gpd.GeoDataFrame,
    include_coordinates: bool = False,  # noqa: FBT001, FBT002
    linkid_field: str = "LinkID",
    fromnodeid_field: str = "FromNodeID",
    tonodeid_field: str = "ToNodeID",
    spathlen_field: str = "SPathLen",
    darea2_field: str = "DArea2",
    geometry_field: str = "geometry",
    linkname_field: Optional[str] = None,
    subarea_name_field: Optional[str] = None,
    node_names: Optional[Dict[str,str]] = None,
):
    """Initialize converter with geopandas dataframe.

    Args:
        gdf: GeoDataFrame loaded from shapefile containing link data
        include_coordinates: Whether to include lat/lon in node definitions
        linkid_field: Name of the column containing Link IDs
        fromnodeid_field: Name of the column containing From Node IDs
        tonodeid_field: Name of the column containing To Node IDs
        spathlen_field: Name of the column containing Stream Path Lengths (in meters)
        darea2_field: Name of the column containing Subarea Drainage Area (in square meters)
        geometry_field: Name of the column containing geometry data
        linkname_field: Name of the column containing Link Names (optional)
        subarea_name_field: Name of the column containing SubArea Names (optional)
        node_names: Optional mapping of node IDs to names (optional)
    """
    self.gdf = gdf
    self.include_coordinates = include_coordinates
    self._linkid_field = linkid_field if linkid_field else _default_linkid_field
    self._fromnodeid_field = fromnodeid_field if fromnodeid_field else _default_fromnodeid_field
    self._tonodeid_field = tonodeid_field if tonodeid_field else _default_tonodeid_field
    self._spathlen_field = spathlen_field if spathlen_field else _default_spathlen_field
    self._darea2_field = darea2_field if darea2_field else _default_darea2_field
    self._geometry_field = geometry_field if geometry_field else _default_geometry_field
    self._linkname_field = linkname_field
    self._subarea_name_field = subarea_name_field
    self._node_names = node_names if node_names else {}
    self._check_geodf()

    self._runoff_model = {
        "PercFactor": 2.25,
        "R": 0.0,
        "RunoffModelType": "GR4J",
        "S": 0.0,
        "SurfaceRunoffRouting": {"SurfaceRunoffRoutingType": "NoRouting"},
        "UHExponent": 2.5,
        "x1": 350.0,
        "x2": 0.0,
        "x3": 40.0,
        "x4": 0.5,
    }
    self.routing_model = {"ChannelRoutingType": "NoRouting"}

gdf property writable

gdf: GeoDataFrame

The geodataframe from which we build the json file.

include_coordinates property writable

include_coordinates: bool

Should the Latitude/Longitude coordinates be derived from the geometry and written in the json file.

routing_model property writable

routing_model: dict

Dictionary for the routing model sections of the json file.

runoff_model property writable

runoff_model: dict

Dictionary for the rainfall-runoff model sections of the json file.

convert

convert() -> Dict[str, Any]

Convert shapefile data to SWIFT JSON format.

Returns:

  • Dict[str, Any]

    Dictionary containing Links, Nodes, and SubAreas sections

Source code in src/geosdhydro/_internal/swift.py
177
178
179
180
181
182
183
def convert(self) -> Dict[str, Any]:
    """Convert shapefile data to SWIFT JSON format.

    Returns:
        Dictionary containing Links, Nodes, and SubAreas sections
    """
    return {"Links": self._create_links(), "Nodes": self._create_nodes(), "SubAreas": self._create_subareas()}

save_to_file

save_to_file(filepath: str, indent: int = 2) -> None

Save converted data to JSON file.

Parameters:

  • filepath (str) –

    Path where to save the JSON file

  • indent (int, default: 2 ) –

    Number of spaces for JSON indentation (default: 2)

Source code in src/geosdhydro/_internal/swift.py
185
186
187
188
189
190
191
192
193
def save_to_file(self, filepath: str, indent: int = 2) -> None:
    """Save converted data to JSON file.

    Args:
        filepath: Path where to save the JSON file
        indent: Number of spaces for JSON indentation (default: 2)
    """
    with open(filepath, "w") as f:
        json.dump(self.convert(), f, indent=indent)

get_parser

get_parser() -> ArgumentParser

Return the CLI argument parser.

Returns:

Source code in src/geosdhydro/_internal/cli.py
30
31
32
33
34
35
36
37
38
39
def get_parser() -> argparse.ArgumentParser:
    """Return the CLI argument parser.

    Returns:
        An argparse parser.
    """
    parser = argparse.ArgumentParser(prog="geosdhydro")
    parser.add_argument("-V", "--version", action="version", version=f"%(prog)s {debug._get_version()}")
    parser.add_argument("--debug-info", action=_DebugInfo, help="Print debug information.")
    return parser

main

main(args: list[str] | None = None) -> int

Run the main program.

This function is executed when you type geosdhydro or python -m geosdhydro.

Parameters:

  • args (list[str] | None, default: None ) –

    Arguments passed from the command line.

Returns:

  • int

    An exit code.

Source code in src/geosdhydro/_internal/cli.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def main(args: list[str] | None = None) -> int:
    """Run the main program.

    This function is executed when you type `geosdhydro` or `python -m geosdhydro`.

    Parameters:
        args: Arguments passed from the command line.

    Returns:
        An exit code.
    """
    parser = get_parser()
    opts = parser.parse_args(args=args)
    print(opts)
    return 0