Source code for shapelib.core.handlers.spatialite_handler

from pathlib import Path
from typing import Any

import fiona
import geopandas as gpd
from geopandas import GeoDataFrame

from shapelib.core.geometries.base import BaseShapes
from shapelib.core.handlers.geo_file_handler import GeoFileHandler
from shapelib.factories.registry import register_handler


[docs] @register_handler(file_extension=".sqlite") class SpatialiteHandler(GeoFileHandler): driver: str = "SQLite" extension: str = ".sqlite" def __init__(self, path: Path) -> None: super().__init__(path) self.validate_file()
[docs] def validate_file(self) -> None: # Check if the file is empty if self.path.stat().st_size == 0: raise ValueError(f"Spatialite file is empty: {self.path.as_posix()}") try: layers = fiona.listlayers(self.path) if not layers: print(f"Spatialite '{self.path.name}' has no layers.") except Exception as e: raise ValueError( f"Could not open the Spatialite file: {self.path.as_posix()}\n" f"Ensure the file is valid and not corrupted. ({e})" ) from e
[docs] def load_data(self, **kwargs: Any) -> GeoDataFrame: return gpd.read_file(self.path, driver=self.driver, **kwargs)
[docs] def create_obj(self, **kwargs: Any) -> BaseShapes: data = self.load_data(**kwargs) return self.create_geometry_instance(data=data, driver=self.driver)