Skip to content

Commit d7919d3

Browse files
authored
BUG: fix overwriting a corrupt fileGDB directory (#600)
1 parent 4cfd296 commit d7919d3

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

CHANGES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
### Bug fixes
66

7-
- Fix regression in reading date columns (#616)
7+
- Fix regression in reading date columns (#616).
88
- Fix regression in `read_dataframe` when `use_arrow=True` and `columns` is used to filter
9-
out columns of some specific types (#611)
9+
out columns of some specific types (#611).
10+
- Fix overwriting a corrupt fileGDB directory (#600).
1011

1112
## 0.12.0 (2025-11-26)
1213

pyogrio/_io.pyx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import locale
99
import logging
1010
import math
1111
import os
12+
import shutil
1213
import sys
1314
import warnings
1415
from pathlib import Path
@@ -2408,7 +2409,15 @@ cdef create_ogr_dataset_layer(
24082409
raise exc
24092410

24102411
# otherwise create from scratch
2411-
os.unlink(path)
2412+
if (
2413+
driver == "OpenFileGDB"
2414+
and os.path.isdir(path)
2415+
and os.path.splitext(path)[1] == ".gdb"
2416+
):
2417+
# An FileGDB "file" is a directory instead of a file, so use rmtree
2418+
shutil.rmtree(path)
2419+
else:
2420+
os.unlink(path)
24122421

24132422
ogr_dataset = NULL
24142423

pyogrio/tests/test_geopandas_io.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3334,6 +3334,33 @@ def test_write_geojson_rfc7946_coordinates(tmp_path, use_arrow):
33343334
assert np.array_equal(gdf_in_appended.geometry.values, points + points_append)
33353335

33363336

3337+
@pytest.mark.requires_arrow_write_api
3338+
def test_write_openfilegdb_overwrite_corrupt(tmp_path, use_arrow):
3339+
"""Test to overwriting an existing corrupt OpenFileGDB.
3340+
3341+
Test added in context of https://github.com/geopandas/pyogrio/issues/598
3342+
"""
3343+
# Create a corrupt OpenFileGDB file. An empty directory suffices.
3344+
test_path = tmp_path / "test.gdb"
3345+
test_path.mkdir()
3346+
3347+
# Overwrite the corrupt OpenFileGDB
3348+
gdf = gp.GeoDataFrame(
3349+
{"id": [1.0, 2.0, 3.0]},
3350+
geometry=[shapely.Point(x, x) for x in range(3)],
3351+
crs="EPSG:4326",
3352+
)
3353+
write_dataframe(
3354+
gdf, test_path, layer="test_layer", driver="OpenFileGDB", use_arrow=use_arrow
3355+
)
3356+
3357+
# Read back and verify it was (over)written correctly
3358+
assert test_path.exists()
3359+
assert test_path.is_dir()
3360+
read_gdf = read_dataframe(test_path, use_arrow=use_arrow)
3361+
assert_geodataframe_equal(gdf, read_gdf)
3362+
3363+
33373364
@pytest.mark.requires_arrow_write_api
33383365
@pytest.mark.skipif(
33393366
not GDAL_HAS_PARQUET_DRIVER, reason="Parquet driver is not available"

0 commit comments

Comments
 (0)