Write a geojson/feather/parquet file from a gdf.
This is wrapper around geopandas.GeoDataFrame I/O methods
that adds some extra functionality.
Parameters:
| Name |
Type |
Description |
Default |
gdf |
GeoDataFrame
|
|
required
|
out_fn |
Union[str, Path]
|
|
required
|
format |
str
|
The output format. One of ".feather", ".parquet", ".geojson".
|
'.feather'
|
Raises:
| Type |
Description |
ValueError
|
If format is not one of ".feather", ".geojson", ".parquet".
|
ValueError
|
If the input gdf does not have a "class_name" column.
|
Examples:
Write a geojson file.
>>> from cellseg_gsontools import gdf_to_file
>>> gdf_to_file(gdf, "out.geojson")
Source code in cellseg_gsontools/merging/save_utils.py
| def gdf_to_file(
gdf: gpd.GeoDataFrame,
out_fn: Union[str, Path],
format: str = ".feather",
) -> None:
"""Write a geojson/feather/parquet file from a gdf.
This is wrapper around `geopandas.GeoDataFrame` I/O methods
that adds some extra functionality.
Parameters:
gdf (gpd.GeoDataFrame):
The input gdf.
out_fn (Union[str, Path]):
The output filename.
format (str):
The output format. One of ".feather", ".parquet", ".geojson".
Raises:
ValueError: If `format` is not one of ".feather", ".geojson", ".parquet".
ValueError: If the input gdf does not have a "class_name" column.
Examples:
Write a geojson file.
>>> from cellseg_gsontools import gdf_to_file
>>> gdf_to_file(gdf, "out.geojson")
"""
out_fn = Path(out_fn)
if format not in (".feather", ".parquet", ".geojson", None):
raise ValueError(
f"Invalid format. Got: {format}. Allowed: .feather, .parquet, .geojson"
)
if "class_name" not in gdf.columns:
raise ValueError("The input gdf needs to have a 'class_name' column.")
# add objectType col (QuPath)
if "objectType" not in gdf.columns:
gdf["objectType"] = "annotation"
# add classification col (QuPath)
if "classification" not in gdf.columns:
gdf["classification"] = gdf_apply(
gdf, _add_qupath_classification, axis=1, columns=["class_name"]
)
if format == ".feather":
gdf.to_feather(out_fn.with_suffix(".feather"))
elif format == ".parquet":
gdf.to_parquet(out_fn.with_suffix(".parquet"))
elif format == ".geojson":
gdf.to_file(out_fn.with_suffix(".geojson"), driver="GeoJSON", index=False)
|