Skip to content

Commit

Permalink
Merge pull request OSGeo#8307 from rouault/fix_8277
Browse files Browse the repository at this point in the history
osgeo_utils: allign base.get_extension() and util.DoesDriverHandleExtension() on C++ versions, and add tests (fixes OSGeo#8277)
  • Loading branch information
rouault authored Sep 2, 2023
2 parents a1464a4 + 9529471 commit 5730454
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
30 changes: 30 additions & 0 deletions autotest/pyscripts/test_gdal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,36 @@ def test_read_write_color_table_from_raster():
gdaltest.tiff_drv.Delete("tmp/ct8.tif")


def test_utils_base_get_extension():
"""test base.get_extension()"""

assert base.get_extension("foo.shp.zip") == "shp.zip"
assert base.get_extension("foo.gpkg.zip") == "gpkg.zip"
assert base.get_extension("foo.gpkg") == "gpkg"


@pytest.mark.require_driver("GeoJSON")
@pytest.mark.require_driver("GeoJSONSeq")
def test_utils_util_DoesDriverHandleExtension():
"""test util.DoesDriverHandleExtension()"""

assert util.DoesDriverHandleExtension(gdal.GetDriverByName("GeoJSON"), "geojson")
assert not util.DoesDriverHandleExtension(
gdal.GetDriverByName("GeoJSONSeq"), "geojson"
)


@pytest.mark.require_driver("HFA")
@pytest.mark.require_driver("netCDF")
@pytest.mark.require_driver("GeoJSON")
def test_utils_util_GetOutputDriverFor():
"""test util.GetOutputDriverFor()"""

assert util.GetOutputDriverFor("foo.img") == "HFA"
assert util.GetOutputDriverFor("foo.nc") == "netCDF"
assert util.GetOutputDriverFor("foo.geojson", is_raster=False) == "GeoJSON"


def test_utils_py_cleanup():
for filename in temp_files:
try:
Expand Down
7 changes: 5 additions & 2 deletions swig/python/gdal-utils/osgeo_utils/auxiliary/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ def get_suffix(filename: PathLikeOrStr) -> str:
def get_extension(filename: PathLikeOrStr) -> str:
"""
returns the suffix without the leading dot.
special case for shp.zip
special case for shp.zip and gpkg.zip
"""
if os.fspath(filename).lower().endswith(".shp.zip"):
lower_filename = os.fspath(filename).lower()
if lower_filename.endswith(".shp.zip"):
return "shp.zip"
if lower_filename.endswith(".gpkg.zip"):
return "gpkg.zip"
ext = get_suffix(filename)
if ext.startswith("."):
ext = ext[1:]
Expand Down
2 changes: 1 addition & 1 deletion swig/python/gdal-utils/osgeo_utils/auxiliary/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

def DoesDriverHandleExtension(drv: gdal.Driver, ext: str) -> bool:
exts = drv.GetMetadataItem(gdal.DMD_EXTENSIONS)
return exts is not None and exts.lower().find(ext.lower()) >= 0
return exts is not None and ext.lower() in exts.lower().split(" ")


def GetOutputDriversFor(filename: PathLikeOrStr, is_raster=True) -> List[str]:
Expand Down

0 comments on commit 5730454

Please sign in to comment.