Skip to content

Commit

Permalink
Merge pull request #15 from philiporlando/add-existing-layer-test
Browse files Browse the repository at this point in the history
Add existing layer test
  • Loading branch information
philiporlando authored Jan 1, 2024
2 parents 540aa55 + 36fdff1 commit 57c0e2e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
24 changes: 17 additions & 7 deletions fgdb_to_gpkg/fgdb_to_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
import geopandas as gpd
import fiona
import os
import warnings
from tqdm import tqdm


def fgdb_to_gpkg(fgdb_path, gpkg_path, overwrite=True, **kwargs):
def fgdb_to_gpkg(
fgdb_path: str, gpkg_path: str, overwrite: bool = True, **kwargs
) -> None:
"""Converts all feature classes within a File GeoDataBase to new layers within a GeoPackage.
:param fgdb_path: file path of an Esri File GeoDataBase (.gdb)
Expand Down Expand Up @@ -35,14 +38,21 @@ def fgdb_to_gpkg(fgdb_path, gpkg_path, overwrite=True, **kwargs):
# Create progress bar
progress_bar = tqdm(total=len(fc_list), desc="Converting layers")

# List all layers within GeoPackage if it exists
gpkg_exists = os.path.exists(gpkg_path)
if gpkg_exists:
layer_list = fiona.listlayers(gpkg_path)

# Loop through each feature class
for fc in fc_list:
# Check if layer exists in GeoPackage when not overwriting
if not overwrite and os.path.exists(gpkg_path):
with fiona.open(gpkg_path, "r") as gpkg:
if fc in gpkg:
print(f"Layer {fc} already exists in {gpkg_path}. Skipping...")
continue
# Skip writing layer if it already exists in GeoPackage when not overwriting
if not overwrite and gpkg_exists:
if fc in layer_list:
warnings.warn(
f"Layer {fc} already exists in {gpkg_path}. Skipping...",
UserWarning,
)
continue

# Read the feature class into GeoDataFrame
gdf = gpd.read_file(fgdb_path, layer=fc)
Expand Down
27 changes: 23 additions & 4 deletions tests/test_fgdb_to_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
import tempfile
import os
import geopandas as gpd
import warnings
from typing import Literal
from shapely.geometry.polygon import Polygon
from shapely.geometry.multipolygon import MultiPolygon
from fgdb_to_gpkg import fgdb_to_gpkg


@pytest.fixture
def setup_fgdb_gpkg():
def setup_fgdb_gpkg() -> tuple[str, str, list[str]]:
# Setup fixture for creating a temporary File GeoDatabase and GeoPackage
with tempfile.TemporaryDirectory() as temp_dir:
fgdb_path = os.path.join(temp_dir, "test.gdb")
gpkg_path = os.path.join(temp_dir, "test.gpkg")
layer = "test_fc"
layer = "layer1"
layers = [layer, "layer2", "layer3"]

# Create a GeoDataFrame
gdf = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
Expand All @@ -23,8 +25,10 @@ def setup_fgdb_gpkg():
for feature in gdf["geometry"]
]

# Write the GeoDataFrame to File GeoDatabase
gdf.to_file(fgdb_path, layer=layer, driver="OpenFileGDB")
# Write the GeoDataFrame to layers within File GeoDatabase
for layer in layers:
gdf["layer_name"] = layer
gdf.to_file(fgdb_path, layer=layer, driver="OpenFileGDB")

yield fgdb_path, gpkg_path, layer

Expand Down Expand Up @@ -72,3 +76,18 @@ def test_nonexistent_fgdb(setup_fgdb_gpkg: tuple[str, str, Literal["test_fc"]]):
nonexistent_fgdb_path = "nonexistent.fgdb"
with pytest.raises(FileNotFoundError):
fgdb_to_gpkg(nonexistent_fgdb_path, gpkg_path, overwrite=False)


def test_layer_already_exists(setup_fgdb_gpkg: tuple[str, str, Literal["test_fc"]]):
# Test the behavior when a layer already exists and overwrite is False
fgdb_path, gpkg_path, _ = setup_fgdb_gpkg

warnings.simplefilter("always")

# First conversion to create the layer
fgdb_to_gpkg(fgdb_path, gpkg_path, overwrite=True)

# Try converting again with overwrite=False
# This should raise a warning because the layers already exists
with pytest.warns(UserWarning) as record:
fgdb_to_gpkg(fgdb_path, gpkg_path, overwrite=False)

0 comments on commit 57c0e2e

Please sign in to comment.