HmsSqlite¶
SQLite geometry and grid database helpers used by HMS geospatial workflows.
hms_commander.HmsSqlite
¶
HmsSqlite - SQLite Grid Database Operations for HMS Commander
Provides static methods for reading spatial geometry from HEC-HMS 4.x SQLite grid databases. These databases store authoritative subbasin polygons, reach linestrings, outlet points, and discretization grids for gridded HMS models (Modified Clark, SCS Grid).
Classes:
| Name | Description |
|---|---|
HmsSqlite |
Static class for SQLite grid database operations |
Key Functions
list_layers: List tables with row counts and geometry types get_crs: Extract CRS WKT from spatial_ref_sys table get_subbasins: Read subbasin2d polygons as GeoDataFrame get_reaches: Read reach2d linestrings as GeoDataFrame get_outlets: Read outlet points as GeoDataFrame get_junctions: Read junction points as GeoDataFrame get_discretization: Read grid cells (large, opt-in) read_grid_database: Read all layers in one call discover_sqlite_files: Find .sqlite files in a project directory join_with_parameters: Merge geometry with basin parameter DataFrame
Dependencies
Required for geometry methods: - geopandas: Spatial data handling - (fiona/GDAL backend reads SpatiaLite natively)
Not required for list_layers, get_crs, discover_sqlite_files: - Uses stdlib sqlite3 only
Install with: pip install hms-commander[gis] # OR pip install geopandas
Example
from hms_commander import HmsSqlite
List layers (no geopandas needed)¶
layers = HmsSqlite.list_layers("project.sqlite") print(layers)
Read subbasin polygons¶
subs = HmsSqlite.get_subbasins("project.sqlite") print(f"Found {len(subs)} subbasins")
Notes
- All methods are static (no instantiation required)
- HEC-HMS 4.x gridded models store geometry in SpatiaLite format
- The spatial_ref_sys table contains CRS as WKT (often custom, no EPSG)
- Geometry is stored as WKB blobs in GEOMETRY columns
HmsSqlite
¶
Static class for HEC-HMS SQLite grid database operations.
Provides methods for reading spatial geometry from SpatiaLite databases created by HEC-HMS 4.x for gridded models (Modified Clark, SCS Grid).
All methods are static - do not instantiate this class.
Example
from hms_commander import HmsSqlite
Read subbasin polygons¶
subs = HmsSqlite.get_subbasins("Minimum_Facility.sqlite") print(f"Found {len(subs)} subbasins")
Read all layers at once¶
layers = HmsSqlite.read_grid_database("Minimum_Facility.sqlite") for name, gdf in layers.items(): ... print(f" {name}: {len(gdf)} features")
Source code in hms_commander/HmsSqlite.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 | |
list_layers(sqlite_path)
staticmethod
¶
List all spatial layers in an HMS SQLite database.
Uses stdlib sqlite3 only - no geopandas required.
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file.
Returns¶
pd.DataFrame DataFrame with columns: - table_name: Name of the table - row_count: Number of rows - geometry_type: Geometry type name (Point, LineString, Polygon, etc.) - srid: Spatial reference ID
Raises¶
FileNotFoundError If sqlite_path does not exist.
Example¶
layers = HmsSqlite.list_layers("Minimum_Facility.sqlite") print(layers)
Source code in hms_commander/HmsSqlite.py
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 | |
get_crs(sqlite_path)
staticmethod
¶
Extract CRS as WKT string from the spatial_ref_sys table.
Uses stdlib sqlite3 only - no geopandas required.
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file.
Returns¶
Optional[str] WKT string of the coordinate reference system, or None if not found.
Raises¶
FileNotFoundError If sqlite_path does not exist.
Example¶
wkt = HmsSqlite.get_crs("Minimum_Facility.sqlite") print(wkt[:50]) 'PROJCS["NAD83 / UTM zone 16N",...'
Source code in hms_commander/HmsSqlite.py
get_subbasins(sqlite_path)
staticmethod
¶
Read subbasin polygons from the subbasin2d table.
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file.
Returns¶
gpd.GeoDataFrame
GeoDataFrame with subbasin polygons. Columns depend on the HMS
project configuration; always includes geometry (Polygon or
MultiPolygon) and typically name. May also include:
area_sqkm, centroid_x, centroid_y, latitude,
longitude (these are NULL in some projects).
Raises¶
FileNotFoundError If sqlite_path does not exist. ImportError If geopandas is not installed. ValueError If the subbasin2d table is not found.
Example¶
subs = HmsSqlite.get_subbasins("Minimum_Facility.sqlite") print(f"Found {len(subs)} subbasins")
Source code in hms_commander/HmsSqlite.py
get_reaches(sqlite_path)
staticmethod
¶
Read reach linestrings from the reach2d table.
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file.
Returns¶
gpd.GeoDataFrame GeoDataFrame with reach linestrings and topology attributes including: - name: Reach name - linkno, dslinkno: Link numbers for topology - uslinkno1, uslinkno2: Upstream link numbers - strmorder: Stream order - length: Reach length - slope: Reach slope - geometry: LineString geometry
Raises¶
FileNotFoundError If sqlite_path does not exist. ImportError If geopandas is not installed. ValueError If the reach2d table is not found.
Example¶
reaches = HmsSqlite.get_reaches("Minimum_Facility.sqlite") print(f"Found {len(reaches)} reaches")
Source code in hms_commander/HmsSqlite.py
get_outlets(sqlite_path)
staticmethod
¶
Read outlet points from the outlet table.
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file.
Returns¶
gpd.GeoDataFrame GeoDataFrame with outlet points and attributes including: - name: Outlet name - geometry: Point geometry
Raises¶
FileNotFoundError If sqlite_path does not exist. ImportError If geopandas is not installed. ValueError If the outlet table is not found.
Example¶
outlets = HmsSqlite.get_outlets("Minimum_Facility.sqlite") for _, row in outlets.iterrows(): ... print(f" {row['name']}: ({row.geometry.x:.1f}, {row.geometry.y:.1f})")
Source code in hms_commander/HmsSqlite.py
get_junctions(sqlite_path)
staticmethod
¶
Read junction points from the junction table.
Returns an empty GeoDataFrame if the junction table has no rows (common in gridded models where junctions are implicit).
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file.
Returns¶
gpd.GeoDataFrame GeoDataFrame with junction points (may be empty).
Raises¶
FileNotFoundError If sqlite_path does not exist. ImportError If geopandas is not installed.
Example¶
junctions = HmsSqlite.get_junctions("Minimum_Facility.sqlite") print(f"Found {len(junctions)} junctions") # Often 0
Source code in hms_commander/HmsSqlite.py
get_discretization(sqlite_path)
staticmethod
¶
Read grid cell discretization polygons.
This layer can be very large (thousands to hundreds of thousands of cells). Use only when grid cell geometry is needed.
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file.
Returns¶
gpd.GeoDataFrame GeoDataFrame with discretization grid cell polygons.
Raises¶
FileNotFoundError If sqlite_path does not exist. ImportError If geopandas is not installed.
Example¶
cells = HmsSqlite.get_discretization("Minimum_Facility.sqlite") print(f"Found {len(cells)} grid cells")
Source code in hms_commander/HmsSqlite.py
read_grid_database(sqlite_path, include_discretization=False, skip_empty=True)
staticmethod
¶
Read all spatial layers from an HMS SQLite database.
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file. include_discretization : bool, default False If True, include the discretization grid cells (can be very large). skip_empty : bool, default True If True, omit layers with zero rows from the result.
Returns¶
Dict[str, gpd.GeoDataFrame] Dictionary mapping layer name to GeoDataFrame.
Raises¶
FileNotFoundError If sqlite_path does not exist. ImportError If geopandas is not installed.
Example¶
layers = HmsSqlite.read_grid_database("Minimum_Facility.sqlite") for name, gdf in layers.items(): ... print(f" {name}: {len(gdf)} features")
Source code in hms_commander/HmsSqlite.py
discover_sqlite_files(project_dir)
staticmethod
¶
Find all .sqlite files in an HMS project directory.
Parameters¶
project_dir : Union[str, Path] Path to the HMS project directory.
Returns¶
List[Path] Sorted list of .sqlite file paths.
Example¶
files = HmsSqlite.discover_sqlite_files("river_bend/") print(f"Found {len(files)} SQLite files")
Source code in hms_commander/HmsSqlite.py
join_with_parameters(sqlite_gdf, subbasin_df, join_column='name')
staticmethod
¶
Merge SQLite geometry with basin parameter DataFrame.
Joins on the specified column (default: 'name') to combine spatial geometry from the SQLite database with hydrologic parameters from basin file parsing.
Parameters¶
sqlite_gdf : gpd.GeoDataFrame GeoDataFrame from get_subbasins() or similar. subbasin_df : pd.DataFrame DataFrame with subbasin parameters (e.g., from HmsPrj.subbasin_df or HmsBasin.get_subbasins()). join_column : str, default "name" Column name to join on (must exist in both DataFrames).
Returns¶
gpd.GeoDataFrame Merged GeoDataFrame with geometry and parameters.
Raises¶
ImportError If geopandas is not installed. ValueError If join_column is missing from either DataFrame.
Example¶
subs_geo = HmsSqlite.get_subbasins("Minimum_Facility.sqlite") from hms_commander import HmsBasin subs_params = HmsBasin.get_subbasins("Minimum_Facility.basin") merged = HmsSqlite.join_with_parameters(subs_geo, subs_params) print(f"Merged: {len(merged)} rows with geometry + parameters")
Source code in hms_commander/HmsSqlite.py
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | |
get_subbasin_statistics(sqlite_path)
staticmethod
¶
Read subbasin statistics (longest flow path, slopes, relief, etc.).
Reads from the subbasin_statistics table that HEC-HMS populates
when terrain/GIS preprocessing has been performed.
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file.
Returns¶
pd.DataFrame DataFrame with columns including: subbasin_name, longest_length, longest_slope, centroidal_length, centroidal_slope, 10_85_length, 10_85_slope, basin_slope, basin_relief, elongation_ratio, relief_ratio, drainage_density, length_units.
Raises¶
FileNotFoundError If sqlite_path does not exist. ValueError If the subbasin_statistics table is not found.
Source code in hms_commander/HmsSqlite.py
get_reach_statistics(sqlite_path)
staticmethod
¶
Read reach statistics (length, slope, relief, sinuosity).
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file.
Returns¶
pd.DataFrame DataFrame with columns: reach_name, reach_length, reach_slope, reach_relief, reach_sinuosity, length_units.
Raises¶
FileNotFoundError If sqlite_path does not exist. ValueError If the reach_statistics table is not found.
Source code in hms_commander/HmsSqlite.py
get_longest_flowpaths(sqlite_path)
staticmethod
¶
Read longest flow path linestrings per subbasin.
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file.
Returns¶
gpd.GeoDataFrame GeoDataFrame with columns: subbasin, geometry (LineString).
Raises¶
FileNotFoundError If sqlite_path does not exist. ValueError If the longest_flowpath table is not found.
Source code in hms_commander/HmsSqlite.py
get_centroidal_flowpaths(sqlite_path)
staticmethod
¶
Read centroidal flow path linestrings per subbasin.
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file.
Returns¶
gpd.GeoDataFrame GeoDataFrame with columns: subbasin, geometry (LineString).
Raises¶
FileNotFoundError If sqlite_path does not exist. ValueError If the centroidal_flowpath table is not found.
Source code in hms_commander/HmsSqlite.py
get_teneightyfive_flowpaths(sqlite_path)
staticmethod
¶
Read 10-85% flow path linestrings per subbasin.
Parameters¶
sqlite_path : Union[str, Path] Path to the SQLite database file.
Returns¶
gpd.GeoDataFrame GeoDataFrame with columns: subbasin, geometry (LineString).
Raises¶
FileNotFoundError If sqlite_path does not exist. ValueError If the teneightyfive_flowpath table is not found.