model.util.export_json

Utility script to export a scenario to a JSON file.

This script exports eflips-model database data to a JSON format compatible with django-simba imports. It handles serialization of SQLAlchemy objects including geometry types, enums, datetimes, and relationships.

Attributes

MODEL_NAME_MAPPING

FIELD_DEFAULTS

EXTRA_FIELDS

COMPUTED_FIELDS

Functions

get_foreign_key_columns(mapper)

Get column names that are foreign keys (with _id suffix).

convert_geometry_to_3d(wkt)

Convert a 2D WKT geometry string to 3D with Z=0.

serialize_value(value)

Serialize a single value based on its type.

get_many_to_many_relationships(obj)

Get many-to-many relationship data as lists of IDs.

serialize_object(obj)

Serialize an SQLAlchemy object to a dict.

export_scenario_to_json(scenario_id, session)

Export a scenario and all related objects to a JSON-compatible dict.

list_scenarios(session[, scenario_ids])

List available scenarios.

main()

CLI entry point.

Module Contents

model.util.export_json.MODEL_NAME_MAPPING: dict[str, str]
model.util.export_json.FIELD_DEFAULTS: dict[str, dict[str, Any]]
model.util.export_json.EXTRA_FIELDS: dict[str, dict[str, Any]]
model.util.export_json.COMPUTED_FIELDS: dict[str, dict[str, Callable[[dict[str, Any]], Any]]]
model.util.export_json.get_foreign_key_columns(mapper)

Get column names that are foreign keys (with _id suffix).

Args:

mapper: SQLAlchemy mapper for the class

Returns:

Set of column names that are foreign keys

Parameters:

mapper (sqlalchemy.orm.Mapper[Any])

Return type:

set[str]

model.util.export_json.convert_geometry_to_3d(wkt)

Convert a 2D WKT geometry string to 3D with Z=0.

Examples:

POINT(13.19 52.69) -> POINT Z (13.19 52.69 0) LINESTRING(13.19 52.69, 13.20 52.70) -> LINESTRING Z (13.19 52.69 0, 13.20 52.70 0) POLYGON((13.19 52.69, …)) -> POLYGON Z ((13.19 52.69 0, …))

Parameters:

wkt (str)

Return type:

str

model.util.export_json.serialize_value(value)

Serialize a single value based on its type.

Args:

value: The value to serialize

Returns:

A JSON-serializable representation of the value

Parameters:

value (Any)

Return type:

Any

model.util.export_json.get_many_to_many_relationships(obj)

Get many-to-many relationship data as lists of IDs.

Args:

obj: An SQLAlchemy object

Returns:

A dict mapping relationship names to lists of related object IDs

Parameters:

obj (eflips.model.Base)

Return type:

dict[str, list[int]]

model.util.export_json.serialize_object(obj)

Serialize an SQLAlchemy object to a dict.

Foreign key columns have their _id suffix stripped to match django-simba’s expected format (e.g., scenario_id -> scenario). Some non-FK columns like manager_id are also treated as references.

Args:

obj: An SQLAlchemy object

Returns:

A JSON-serializable dict representation of the object

Parameters:

obj (eflips.model.Base)

Return type:

dict[str, Any]

model.util.export_json.export_scenario_to_json(scenario_id, session)

Export a scenario and all related objects to a JSON-compatible dict.

Args:

scenario_id: The ID of the scenario to export session: An active database session

Returns:

A dict with class names as keys and lists of serialized objects as values

Parameters:
  • scenario_id (int)

  • session (sqlalchemy.orm.Session)

Return type:

dict[str, list[dict[str, Any]]]

model.util.export_json.list_scenarios(session, scenario_ids=None)

List available scenarios.

Args:

session: An active database session scenario_ids: Optional list of specific scenario IDs to list

Parameters:
  • session (sqlalchemy.orm.Session)

  • scenario_ids (list[int] | None)

Return type:

None

model.util.export_json.main()

CLI entry point.

Return type:

None