Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release/3.7] osgeo_utils: allign base.get_extension() and util.DoesDriverHandleExtension() on C++ versions, and add tests (fixes #8277) #8311

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading