Importing and Exporting Systems#
All Infrasys systems can be serialized and deserialized using the
to_json
andfrom_json
methods.The
DistributionSystem
class can also be exported to GeoDataFrame and GeoJSON formats using theto_gdf
andto_geojson
methods.
We start by loading a sample distribution system.
from gdm.distribution import DistributionSystem
from gdmloader.constants import GCS_CASE_SOURCE
from gdmloader.source import SystemLoader
gdm_loader = SystemLoader()
gdm_loader.add_source(GCS_CASE_SOURCE)
distribution_system: DistributionSystem = gdm_loader.load_dataset(
source_name=GCS_CASE_SOURCE.name,
system_type=DistributionSystem,
dataset_name="p5r",
)
distribution_system.name = "P5R"
/opt/hostedtoolcache/Python/3.12.10/x64/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py:502: UserWarning: Ellipsis is not a Python type (it may be an instance of an object), Pydantic will allow any object with no validation since we cannot even enforce that the input is an instance of the given type. To get rid of this error wrap the type with `pydantic.SkipValidation`.
warn(
The DistributionSystem
can be serialized to disk using the to_json
method. If the model already exists, set the overwrite
parameter to True
. Serialization ensures no loss of information. The JSON file can be deserialized using the from_json
method.
distribution_system.to_json("test.json", overwrite=True)
system = DistributionSystem.from_json("test.json")
The DistributionSystem
can also be exported in GeoDataFrame and GeoJSON formats.
Warning
Limited model information is exported in these formats. Model deserialization is not supported for these formats.
system.to_gdf(“test.csv”) system.to_geojson(“test.geojson”)
system.to_gdf("test.csv")
system.to_geojson("test.geojson")