![]() |
OpenSWMM Engine
6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
|
C API implementation for GeoPackage I/O. More...
#include <openswmm/engine/openswmm_geopackage.h>#include "GpkgUtils.hpp"#include "GeoPackageSchema.hpp"#include "GeoPackagePluginInfo.hpp"#include <string>#include <cstring>Classes | |
| struct | GpkgHandle |
Functions | |
| SWMM_ENGINE_API SWMM_Gpkg | swmm_gpkg_open (const char *path) |
| Open or create a writable GeoPackage and ensure its OpenSWMM schema exists. | |
| SWMM_ENGINE_API void | swmm_gpkg_close (SWMM_Gpkg gpkg) |
| Roll back an outstanding transaction and delete the database handle. | |
| SWMM_ENGINE_API const char * | swmm_gpkg_last_error (SWMM_Gpkg gpkg) |
| Borrow the last stored handle diagnostic. | |
| SWMM_ENGINE_API int | swmm_gpkg_begin (SWMM_Gpkg gpkg) |
| Begin an immediate SQLite transaction on this handle. | |
| SWMM_ENGINE_API int | swmm_gpkg_commit (SWMM_Gpkg gpkg) |
| Commit this handle active transaction. | |
| SWMM_ENGINE_API int | swmm_gpkg_rollback (SWMM_Gpkg gpkg) |
| Roll back this handle active transaction if one exists. | |
| SWMM_ENGINE_API int | swmm_gpkg_register (const char *license_key, const char *organization, const char *contact_email, const char *deployment_id) |
| Submit process-global GeoPackage plugin registration metadata. | |
| SWMM_ENGINE_API int | swmm_gpkg_is_registered (void) |
| Read process-global GeoPackage plugin registration state. | |
| SWMM_ENGINE_API int | swmm_gpkg_simulation_count (SWMM_Gpkg gpkg) |
| Count stored simulation-run rows. | |
| SWMM_ENGINE_API int | swmm_gpkg_simulation_id (SWMM_Gpkg gpkg, int index, char *buf, int bufsz) |
| Copy one simulation-run ID in creation-time order. | |
| SWMM_ENGINE_API int | swmm_gpkg_node_count (SWMM_Gpkg gpkg, const char *sim_id) |
| Count node rows for one simulation. | |
| SWMM_ENGINE_API int | swmm_gpkg_link_count (SWMM_Gpkg gpkg, const char *sim_id) |
| Count link rows for one simulation. | |
| SWMM_ENGINE_API int | swmm_gpkg_subcatch_count (SWMM_Gpkg gpkg, const char *sim_id) |
| Count subcatchment rows for one simulation. | |
| SWMM_ENGINE_API int | swmm_gpkg_gage_count (SWMM_Gpkg gpkg, const char *sim_id) |
| Count rain-gage rows for one simulation. | |
| SWMM_ENGINE_API int | swmm_gpkg_topology_edge_count (SWMM_Gpkg gpkg, const char *sim_id) |
| Count node-link topology edge rows for one simulation. | |
| SWMM_ENGINE_API int | swmm_gpkg_variable_count (SWMM_Gpkg gpkg) |
| Count catalogue variables, including added observed-data variables. | |
| SWMM_ENGINE_API int | swmm_gpkg_result_ts_count (SWMM_Gpkg gpkg, const char *simulation_id, const char *object_type, const char *object_id, const char *variable_name) |
| Count result rows for one simulation/object/variable key. | |
| SWMM_ENGINE_API int | swmm_gpkg_read_result_ts (SWMM_Gpkg gpkg, const char *simulation_id, const char *object_type, const char *object_id, const char *variable_name, double *times, double *values, int max_count) |
| Copy the first time-ordered result rows for one simulation/object/variable. | |
| SWMM_ENGINE_API int | swmm_gpkg_read_summary (SWMM_Gpkg gpkg, const char *simulation_id, const char *object_type, const char *object_id, const char *variable_name, double *value) |
| Read a stored summary value for one simulation/object/variable. | |
| SWMM_ENGINE_API int | swmm_gpkg_create_observed_series (SWMM_Gpkg gpkg, const char *name, const char *variable_name, const char *object_type, const char *object_id, const char *source, const char *units) |
| Create a uniquely named observed series and resolve/create its variable row. | |
| SWMM_ENGINE_API int | swmm_gpkg_write_observed_value (SWMM_Gpkg gpkg, int series_id, const char *timestamp, double value, const char *quality_flag) |
| Insert one observed timestamp/value into a series. | |
| SWMM_ENGINE_API int | swmm_gpkg_write_observed_values (SWMM_Gpkg gpkg, int series_id, const char **timestamps, const double *values, const char **quality_flags, int count) |
| Insert a vector of observed values using one prepared statement. | |
| SWMM_ENGINE_API int | swmm_gpkg_observed_series_count (SWMM_Gpkg gpkg) |
| Count observed-series definitions. | |
| SWMM_ENGINE_API int | swmm_gpkg_observed_value_count (SWMM_Gpkg gpkg, int series_id) |
| Count rows stored for an observed series ID. | |
| SWMM_ENGINE_API int | swmm_gpkg_read_observed_values (SWMM_Gpkg gpkg, int series_id, char *timestamps, int ts_buf_len, double *values, int max_count) |
| Copy a timestamp-ordered prefix of observed values. | |
| SWMM_ENGINE_API int | swmm_gpkg_query_int (SWMM_Gpkg gpkg, const char *sql) |
| Execute supplied SQL and return the first column of its first result row as int. | |
| SWMM_ENGINE_API int | swmm_gpkg_query_double (SWMM_Gpkg gpkg, const char *sql, double *result) |
| Execute supplied SQL and copy the first row/column as double. | |
C API implementation for GeoPackage I/O.
Model definitions and simulation results are read-only. Only observed/sensor timeseries data is writable through this API.
| SWMM_ENGINE_API int swmm_gpkg_begin | ( | SWMM_Gpkg | gpkg | ) |
Begin an immediate SQLite transaction on this handle.
Uses BEGIN IMMEDIATE. Further observed inserts share this transaction until commit/rollback; closing the handle attempts rollback.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| SWMM_ENGINE_API void swmm_gpkg_close | ( | SWMM_Gpkg | gpkg | ) |
Roll back an outstanding transaction and delete the database handle.
Attempts rollback when marked in a transaction, closes the database and frees its wrapper. Does not report rollback failure.
| [in] | gpkg | Caller-owned handle, or NULL for a no-op. All borrowed diagnostics become invalid; do not use the handle afterward. |
| SWMM_ENGINE_API int swmm_gpkg_commit | ( | SWMM_Gpkg | gpkg | ) |
Commit this handle active transaction.
The active flag clears only after successful commit; after refusal the caller can inspect the diagnostic and roll back.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| SWMM_ENGINE_API int swmm_gpkg_create_observed_series | ( | SWMM_Gpkg | gpkg, |
| const char * | name, | ||
| const char * | variable_name, | ||
| const char * | object_type, | ||
| const char * | object_id, | ||
| const char * | source, | ||
| const char * | units ) |
Create a uniquely named observed series and resolve/create its variable row.
Does not validate that the linked model object exists or convert values to the declared units. Variable creation and series insertion are separate statements; use an explicit transaction when they must roll back together.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | name | Non-null copied series name; uniqueness is enforced by the database. |
| [in] | variable_name | Non-null copied measured variable name. |
| [in] | object_type | Optional copied category. NULL stores an unlinked series but uses NODE when resolving/creating its variable catalogue entry. |
| [in] | object_id | Optional copied model-object ID; NULL stores no linked ID. |
| [in] | source | Optional copied source description; NULL stores SQL NULL. |
| [in] | units | Optional copied units label; NULL stores SQL NULL for the series and an empty label for a newly created variable. |
| SWMM_ENGINE_API int swmm_gpkg_gage_count | ( | SWMM_Gpkg | gpkg, |
| const char * | simulation_id ) |
Count rain-gage rows for one simulation.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | simulation_id | Non-null NUL-terminated simulation ID as stored in the database; borrowed for the call. |
| SWMM_ENGINE_API int swmm_gpkg_is_registered | ( | void | ) |
Read process-global GeoPackage plugin registration state.
| SWMM_ENGINE_API const char * swmm_gpkg_last_error | ( | SWMM_Gpkg | gpkg | ) |
Borrow the last stored handle diagnostic.
Not every refusal stores a diagnostic and successful operations do not uniformly clear old text. Use the operation return code as the primary result.
| [in] | gpkg | Live handle, or NULL to obtain an empty static string. |
| SWMM_ENGINE_API int swmm_gpkg_link_count | ( | SWMM_Gpkg | gpkg, |
| const char * | simulation_id ) |
Count link rows for one simulation.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | simulation_id | Non-null NUL-terminated simulation ID as stored in the database; borrowed for the call. |
| SWMM_ENGINE_API int swmm_gpkg_node_count | ( | SWMM_Gpkg | gpkg, |
| const char * | simulation_id ) |
Count node rows for one simulation.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | simulation_id | Non-null NUL-terminated simulation ID as stored in the database; borrowed for the call. |
| SWMM_ENGINE_API int swmm_gpkg_observed_series_count | ( | SWMM_Gpkg | gpkg | ) |
Count observed-series definitions.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| SWMM_ENGINE_API int swmm_gpkg_observed_value_count | ( | SWMM_Gpkg | gpkg, |
| int | series_id ) |
Count rows stored for an observed series ID.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | series_id | Observed-series key; absent keys produce zero rows. |
| SWMM_ENGINE_API SWMM_Gpkg swmm_gpkg_open | ( | const char * | path | ) |
Open or create a writable GeoPackage and ensure its OpenSWMM schema exists.
Uses SQLite READWRITE|CREATE and idempotently creates schema/default variable rows, so this is not a read-only open even when the caller intends only to query. Model/result APIs below read those tables; observed-series APIs perform user writes.
| [in] | path | Non-null NUL-terminated database path, borrowed for the call. An absent file may be created. |
| SWMM_ENGINE_API int swmm_gpkg_query_double | ( | SWMM_Gpkg | gpkg, |
| const char * | sql, | ||
| double * | result ) |
Execute supplied SQL and copy the first row/column as double.
Uses SQLite numeric conversion without validating the result schema or converting units; prepares/steps one statement.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | sql | Non-null borrowed SQL text; callers requiring read-only access must supply a read-only query because this wrapper does not enforce it. |
| [out] | result | Non-null double destination, written only when SQLite returns a row. |
| SWMM_ENGINE_API int swmm_gpkg_query_int | ( | SWMM_Gpkg | gpkg, |
| const char * | sql ) |
Execute supplied SQL and return the first column of its first result row as int.
Executes one prepared statement and steps once. It is a direct database interface rather than a constrained SELECT parser; no automatic protection of model/result tables is applied here.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | sql | Non-null borrowed SQL text. Callers intending a read-only operation must supply a read-only query; this wrapper does not enforce SQL read-only status. |
| SWMM_ENGINE_API int swmm_gpkg_read_observed_values | ( | SWMM_Gpkg | gpkg, |
| int | series_id, | ||
| char * | timestamps, | ||
| int | ts_buf_len, | ||
| double * | values, | ||
| int | max_count ) |
Copy a timestamp-ordered prefix of observed values.
Orders by the stored timestamp TEXT, not a parsed calendar/timezone value. Use a consistently sortable timestamp representation. No quality flags or needed-size output are returned; earlier records may have been copied on failure.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | series_id | Observed-series key to query. |
| [out] | timestamps | Optional flat caller-owned buffer of max_count*ts_buf_len bytes. Each timestamp uses one fixed-stride slot and is NUL-terminated/truncated when stride>0. |
| [in] | ts_buf_len | Per-timestamp byte stride including NUL. Nonpositive suppresses timestamp copying, even when timestamps is non-null. |
| [out] | values | Non-null caller-owned max_count-double array; raw stored measurement values without unit conversion. |
| [in] | max_count | Positive row limit and value-array capacity. |
| SWMM_ENGINE_API int swmm_gpkg_read_result_ts | ( | SWMM_Gpkg | gpkg, |
| const char * | simulation_id, | ||
| const char * | object_type, | ||
| const char * | object_id, | ||
| const char * | variable_name, | ||
| double * | times, | ||
| double * | values, | ||
| int | max_count ) |
Copy the first time-ordered result rows for one simulation/object/variable.
Ordered by elapsed_time; ties follow query order. Does not interpolate, transform units or report an additional required size. On failure earlier rows may already have been copied.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | simulation_id | Non-null NUL-terminated simulation ID as stored in the database; borrowed for the call. |
| [in] | object_type | Non-null object category token as stored (for example NODE, LINK, SUBCATCH or SYSTEM); matched by the database query. |
| [in] | object_id | Non-null NUL-terminated object ID as stored. |
| [in] | variable_name | Non-null NUL-terminated variable name resolved with object_type in the variable catalogue. |
| [out] | times | Non-null caller-owned max_count-double array for elapsed seconds. |
| [out] | values | Non-null caller-owned max_count-double array for stored values; units follow the database variable metadata, with no conversion here. |
| [in] | max_count | Positive array capacity/SQL row limit. |
| SWMM_ENGINE_API int swmm_gpkg_read_summary | ( | SWMM_Gpkg | gpkg, |
| const char * | simulation_id, | ||
| const char * | object_type, | ||
| const char * | object_id, | ||
| const char * | variable_name, | ||
| double * | value ) |
Read a stored summary value for one simulation/object/variable.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | simulation_id | Non-null NUL-terminated simulation ID as stored in the database; borrowed for the call. |
| [in] | object_type | Non-null object category token as stored (for example NODE, LINK, SUBCATCH or SYSTEM); matched by the database query. |
| [in] | object_id | Non-null NUL-terminated object ID as stored. |
| [in] | variable_name | Non-null NUL-terminated variable name resolved with object_type in the variable catalogue. |
| [out] | value | Non-null stored-value destination; unchanged when no row is found. Units are those recorded by the summary writer; no conversion is applied. |
| SWMM_ENGINE_API int swmm_gpkg_register | ( | const char * | license_key, |
| const char * | organization, | ||
| const char * | contact_email, | ||
| const char * | deployment_id ) |
Submit process-global GeoPackage plugin registration metadata.
Does not take or return a database handle; registration does not itself open a file.
| [in] | license_key | Optional borrowed NUL-terminated registration key/token; NULL becomes empty. |
| [in] | organization | Optional borrowed organization name; NULL becomes empty. |
| [in] | contact_email | Optional borrowed contact email; NULL becomes empty. |
| [in] | deployment_id | Optional borrowed deployment identifier; NULL becomes empty. |
| SWMM_ENGINE_API int swmm_gpkg_result_ts_count | ( | SWMM_Gpkg | gpkg, |
| const char * | simulation_id, | ||
| const char * | object_type, | ||
| const char * | object_id, | ||
| const char * | variable_name ) |
Count result rows for one simulation/object/variable key.
Use to allocate result arrays; count and later read are separate database queries and can observe intervening writes.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | simulation_id | Non-null NUL-terminated simulation ID as stored in the database; borrowed for the call. |
| [in] | object_type | Non-null object category token as stored (for example NODE, LINK, SUBCATCH or SYSTEM); matched by the database query. |
| [in] | object_id | Non-null NUL-terminated object ID as stored. |
| [in] | variable_name | Non-null NUL-terminated variable name resolved with object_type in the variable catalogue. |
| SWMM_ENGINE_API int swmm_gpkg_rollback | ( | SWMM_Gpkg | gpkg | ) |
Roll back this handle active transaction if one exists.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| SWMM_ENGINE_API int swmm_gpkg_simulation_count | ( | SWMM_Gpkg | gpkg | ) |
Count stored simulation-run rows.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| SWMM_ENGINE_API int swmm_gpkg_simulation_id | ( | SWMM_Gpkg | gpkg, |
| int | index, | ||
| char * | buf, | ||
| int | bufsz ) |
Copy one simulation-run ID in creation-time order.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | index | Zero-based query offset. Supply a valid nonnegative index; ordering ties follow the database result order. |
| [out] | buf | Non-null caller-owned ID destination, NUL-terminated and silently truncated to bufsz-1 bytes. |
| [in] | bufsz | Positive byte capacity including NUL. |
| SWMM_ENGINE_API int swmm_gpkg_subcatch_count | ( | SWMM_Gpkg | gpkg, |
| const char * | simulation_id ) |
Count subcatchment rows for one simulation.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | simulation_id | Non-null NUL-terminated simulation ID as stored in the database; borrowed for the call. |
| SWMM_ENGINE_API int swmm_gpkg_topology_edge_count | ( | SWMM_Gpkg | gpkg, |
| const char * | simulation_id ) |
Count node-link topology edge rows for one simulation.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | simulation_id | Non-null NUL-terminated simulation ID as stored in the database; borrowed for the call. |
| SWMM_ENGINE_API int swmm_gpkg_variable_count | ( | SWMM_Gpkg | gpkg | ) |
Count catalogue variables, including added observed-data variables.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| SWMM_ENGINE_API int swmm_gpkg_write_observed_value | ( | SWMM_Gpkg | gpkg, |
| int | series_id, | ||
| const char * | timestamp, | ||
| double | value, | ||
| const char * | quality_flag ) |
Insert one observed timestamp/value into a series.
Joins an active transaction when present; otherwise normal SQLite autocommit applies.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | series_id | Stored observed-series ID; database constraints determine acceptance. |
| [in] | timestamp | Non-null NUL-terminated timestamp string, conventionally ISO 8601; copied without parsing or timezone conversion. |
| [in] | value | Measured numeric value in the series declared units; no finite/range/unit validation here. |
| [in] | quality_flag | Optional copied quality label such as A/P/E; NULL stores SQL NULL. |
| SWMM_ENGINE_API int swmm_gpkg_write_observed_values | ( | SWMM_Gpkg | gpkg, |
| int | series_id, | ||
| const char ** | timestamps, | ||
| const double * | values, | ||
| const char ** | quality_flags, | ||
| int | count ) |
Insert a vector of observed values using one prepared statement.
Does not create an implicit all-or-nothing transaction. Earlier accepted inserts remain committed/pending if a later row fails; wrap begin/commit and roll back on refusal when batch atomicity is required.
| [in] | gpkg | Non-null live GeoPackage handle; caller serializes access and retains ownership until swmm_gpkg_close(). |
| [in] | series_id | Stored observed-series ID for all records. |
| [in] | timestamps | Non-null count-entry borrowed timestamp-pointer array. Each supplied string is copied by SQLite; provide valid timestamps consistent with the series. |
| [in] | values | Non-null count-double borrowed measurement array, in series units; no conversion/finite validation. |
| [in] | quality_flags | Optional count-entry borrowed flag-pointer array; NULL array or NULL entries store SQL NULL. |
| [in] | count | Positive record count. |