Skip to content

genet.utils.io

SUPPORTED_FILE_FORMATS = ['parquet', 'geoparquet', 'geojson', 'shp', 'shapefile'] module-attribute #

check_file_type_is_supported(filetype) #

Checks there is support for the given file type

PARAMETER DESCRIPTION
filetype

The file type to save a spatial object to.

TYPE: str

RAISES DESCRIPTION
RuntimeError

filetype is not a supported file type

Source code in src/genet/utils/io.py
def check_file_type_is_supported(filetype: str):
    """Checks there is support for the given file type

    Args:
        filetype (str, optional): The file type to save a spatial object to.

    Raises:
        RuntimeError: `filetype` is not a supported file type
    """
    if filetype.lower() not in SUPPORTED_FILE_FORMATS:
        raise RuntimeError(
            f"{filetype} is not a supported file type: {', '.join(SUPPORTED_FILE_FORMATS)}"
        )

save_geodataframe(gdf, filename, output_dir, filetype='parquet') #

Saves geopandas.GeoDataFrame to the requested file format

PARAMETER DESCRIPTION
gdf

GeoDataFrame to save to disk.

TYPE: GeoDataFrame

filename

Name of the file, without extension.

TYPE: str

output_dir

Path to folder where to save the file.

TYPE: str

filetype

The file type to save the GeoDataFrame to: geojson, geoparquet or shp are supported. Defaults to parquet format.

TYPE: str DEFAULT: 'parquet'

Source code in src/genet/utils/io.py
def save_geodataframe(
    gdf: gpd.GeoDataFrame, filename: str, output_dir: str, filetype: str = "parquet"
):
    """Saves geopandas.GeoDataFrame to the requested file format

    Args:
        gdf (gpd.GeoDataFrame): GeoDataFrame to save to disk.
        filename (str): Name of the file, without extension.
        output_dir (str): Path to folder where to save the file.
        filetype (str, optional):
            The file type to save the GeoDataFrame to: geojson, geoparquet or shp are supported.
            Defaults to parquet format.
    """
    if not gdf.empty:
        check_file_type_is_supported(filetype)

        _gdf = sanitiser.sanitise_geodataframe(gdf.copy())
        persistence.ensure_dir(output_dir)

        if filetype.lower() in ["parquet", "geoparquet"]:
            _gdf.to_parquet(os.path.join(output_dir, f"{filename}.parquet"))
        elif filetype.lower() == "geojson":
            _gdf.to_file(
                os.path.join(output_dir, f"{filename}.geojson"), driver="GeoJSON", engine="pyogrio"
            )
        elif filetype.lower() in ["shp", "shapefile"]:
            for col in [col for col in _gdf.columns if is_datetime(_gdf[col])]:
                _gdf[col] = _gdf[col].astype(str)
            _gdf.to_file(os.path.join(output_dir, f"{filename}.shp"))