GeoPackage I/O#
Note
Engine: OpenSWMM 6 — refactored. The
openswmm.engine._geopackage.GeoPackage reader is only available
when the engine was built with OPENSWMM_WITH_GEOPACKAGE=ON. Guard for
it with openswmm.engine.HAS_GEOPACKAGE.
A GeoPackage (.gpkg) is a single-file SQLite database holding model
geometry, simulation results, and observed data. GeoPackage
opens one for reading results and writing observed series — it is a
standalone object (not reached through a Solver) and works as a context
manager:
from openswmm.engine import GeoPackage, HAS_GEOPACKAGE
if HAS_GEOPACKAGE:
with GeoPackage("results.gpkg") as gpkg:
for sim_id in gpkg.simulation_ids():
print(sim_id, gpkg.object_counts(sim_id))
Reference: openswmm_geopackage.h.
Quickstart#
from openswmm.engine import GeoPackage
with GeoPackage("results.gpkg") as gpkg:
sim = gpkg.simulation_ids()[0]
# Read a result time series as numpy arrays.
times, values = gpkg.read_result_ts(sim, "node", "J1", "depth")
# Read a single summary statistic.
peak = gpkg.read_summary(sim, "node", "J1", "max_depth")
Reading results#
Member |
What it returns |
|---|---|
|
Number of runs / list of run IDs in the file. |
|
|
|
Number of output variables defined. |
|
Number of topology edges for a run. |
|
Number of records matching a result query. |
|
Result time series as numpy |
|
A single summary statistic value. |
Observed data#
Write measured series alongside the simulated results for calibration and comparison:
Member |
What it does |
|---|---|
|
Create a series; returns its integer |
|
Write a single point. |
|
Bulk-write points (GIL released). |
|
Series count / point count. |
|
Read a series back as numpy arrays. |
Transactions & raw SQL#
Wrap bulk writes in a transaction for speed, and drop to read-only SQL for ad-hoc queries:
with GeoPackage("results.gpkg") as gpkg:
sid = gpkg.create_observed_series("obs_J1", "depth",
obj_type="node", obj_id="J1",
units="m")
gpkg.begin()
gpkg.write_observed_values(sid, timestamps, values)
gpkg.commit() # or gpkg.rollback()
n = gpkg.query_int("SELECT COUNT(*) FROM gpkg_contents")
x = gpkg.query_double("SELECT MAX(value) FROM observed_values")
begin / commit / rollback manage the transaction;
query_int / query_double execute a read-only query and return the
first column of the first row. The GeoPackage.last_error property
carries the last library error message.
See also#
Output reader (binary .out file) — reading the binary
.outfile.Statistics — in-memory cumulative statistics.
Error handling, edge cases & debugging — exception types.