Skip to content

Commit

Permalink
HFA (Erdas Imagine) driver: make it optional and buildable as a plugin
Browse files Browse the repository at this point in the history
Note that when built as a plugin, HFA .aux overview building is not
supported
  • Loading branch information
rouault committed Sep 22, 2024
1 parent 410b6fa commit 624109b
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 20 deletions.
1 change: 1 addition & 0 deletions autotest/gcore/gdal_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def stats_nodata_inf_progress_cbk(value, string, extra):
extra[0] = value


@pytest.mark.require_driver("HFA")
def test_stats_nodata_inf():

ds = gdal.GetDriverByName("HFA").Create(
Expand Down
5 changes: 4 additions & 1 deletion autotest/gcore/hfa_rfc40.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@

from osgeo import gdal

pytestmark = pytest.mark.random_order(disabled=True)
pytestmark = [
pytest.mark.random_order(disabled=True),
pytest.mark.require_driver("HFA"),
]

# All tests will be skipped if numpy is unavailable.
np = pytest.importorskip("numpy")
Expand Down
2 changes: 2 additions & 0 deletions autotest/gcore/hfa_srs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

from osgeo import gdal, osr

pytestmark = pytest.mark.require_driver("HFA")

###############################################################################
# Write a HFA/Imagine and read it back to check its SRS
crs_list = [
Expand Down
31 changes: 20 additions & 11 deletions autotest/gcore/hfa_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

from osgeo import gdal, osr

pytestmark = pytest.mark.require_driver("HFA")

###############################################################################
# test that we can write a small file with a custom layer name.

Expand Down Expand Up @@ -298,31 +300,38 @@ def test_hfa_use_rrd():


@pytest.mark.require_driver("BMP")
def test_hfa_update_existing_aux_overviews():
def test_hfa_update_existing_aux_overviews(tmp_path):

tmp_filename = str(tmp_path / "hfa_update_existing_aux_overviews.bmp")

with gdal.config_option("USE_RRD", "YES"):

ds = gdal.GetDriverByName("BMP").Create(
"tmp/hfa_update_existing_aux_overviews.bmp", 100, 100, 1
)
ds = gdal.GetDriverByName("BMP").Create(tmp_filename, 100, 100, 1)
ds.GetRasterBand(1).Fill(255)
ds = None

# Create overviews
ds = gdal.Open("tmp/hfa_update_existing_aux_overviews.bmp")
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds = gdal.Open(tmp_filename)
with gdaltest.disable_exceptions():
ret = ds.BuildOverviews("NEAR", overviewlist=[2, 4])
if (
gdal.GetLastErrorMsg()
== "This build does not support creating .aux overviews"
):
pytest.skip(gdal.GetLastErrorMsg())
assert ret == 0
ds = None

# Save overviews checksum
ds = gdal.Open("tmp/hfa_update_existing_aux_overviews.bmp")
ds = gdal.Open(tmp_filename)
cs_ovr0 = ds.GetRasterBand(1).GetOverview(0).Checksum()
cs_ovr1 = ds.GetRasterBand(1).GetOverview(1).Checksum()

# and regenerate them
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds = None

ds = gdal.Open("tmp/hfa_update_existing_aux_overviews.bmp")
ds = gdal.Open(tmp_filename)
# Check overviews checksum
new_cs_ovr0 = ds.GetRasterBand(1).GetOverview(0).Checksum()
new_cs_ovr1 = ds.GetRasterBand(1).GetOverview(1).Checksum()
Expand All @@ -336,7 +345,7 @@ def test_hfa_update_existing_aux_overviews():
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds = None

ds = gdal.Open("tmp/hfa_update_existing_aux_overviews.bmp")
ds = gdal.Open(tmp_filename)
# Check overviews checksum
new_cs_ovr0 = ds.GetRasterBand(1).GetOverview(0).Checksum()
new_cs_ovr1 = ds.GetRasterBand(1).GetOverview(1).Checksum()
Expand All @@ -349,7 +358,7 @@ def test_hfa_update_existing_aux_overviews():
ds.BuildOverviews("NEAR", overviewlist=[8])
ds = None

ds = gdal.Open("tmp/hfa_update_existing_aux_overviews.bmp")
ds = gdal.Open(tmp_filename)
# Check overviews checksum
new_cs_ovr0 = ds.GetRasterBand(1).GetOverview(0).Checksum()
new_cs_ovr1 = ds.GetRasterBand(1).GetOverview(1).Checksum()
Expand All @@ -359,7 +368,7 @@ def test_hfa_update_existing_aux_overviews():
pytest.fail()
ds = None

gdal.GetDriverByName("BMP").Delete("tmp/hfa_update_existing_aux_overviews.bmp")
gdal.GetDriverByName("BMP").Delete(tmp_filename)


###############################################################################
Expand Down
1 change: 1 addition & 0 deletions autotest/gcore/pam.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def test_pam_5():
#


@pytest.mark.require_driver("HFA")
def test_pam_6():

ds = gdal.Open("data/f2r23.tif")
Expand Down
2 changes: 2 additions & 0 deletions autotest/gcore/tiff_ovr.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ def cbk(pct, _, user_data):
callback_data=tab,
options=["USE_RRD=YES"],
)
if gdal.GetLastErrorMsg() == "This build does not support creating .aux overviews":
pytest.skip(gdal.GetLastErrorMsg())
assert tab[0] == 1.0

try:
Expand Down
2 changes: 2 additions & 0 deletions autotest/gcore/tiff_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

from osgeo import gdal, osr

pytestmark = pytest.mark.require_driver("HFA")

init_list = [
("byte.tif", 1, 4672),
("uint16_sgilog.tif", 1, 4672),
Expand Down
2 changes: 2 additions & 0 deletions autotest/gdrivers/hfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

from osgeo import gdal

pytestmark = pytest.mark.require_driver("HFA")

###############################################################################
# Verify we can read the special histogram metadata from a provided image.

Expand Down
2 changes: 2 additions & 0 deletions autotest/gdrivers/pcidsk.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ def test_pcidsk_external_ovr_rrd():
with gdaltest.config_option("USE_RRD", "YES"):
ds.BuildOverviews("NEAR", [2])
ds = None
if gdal.GetLastErrorMsg() == "This build does not support creating .aux overviews":
pytest.skip(gdal.GetLastErrorMsg())
assert gdal.VSIStatL("/vsimem/test.aux") is not None
ds = gdal.Open("/vsimem/test.pix")
assert ds.GetRasterBand(1).GetOverviewCount() == 1
Expand Down
1 change: 1 addition & 0 deletions autotest/pyscripts/test_gdalinfo_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def test_gdalinfo_py_5(script_path):
# Test a dataset with overviews and RAT


@pytest.mark.require_driver("HFA")
def test_gdalinfo_py_6(script_path):

ret = test_py_scripts.run_py_script(
Expand Down
1 change: 1 addition & 0 deletions autotest/pyscripts/test_pct.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ def test_rgb2pct_3(script_path, tmp_path, rgb2pct2_tif):
# Test pct2rgb with big CT (>256 entries)


@pytest.mark.require_driver("HFA")
def test_pct2rgb_4(script_path, tmp_path):
gdal_array = pytest.importorskip("osgeo.gdal_array")
try:
Expand Down
5 changes: 5 additions & 0 deletions autotest/utilities/test_gdal_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ def test_gdal_translate_20(gdal_translate_path, tmp_path):
# in that case, they must be copied


@pytest.mark.require_driver("HFA")
def test_gdal_translate_21(gdal_translate_path, tmp_path):

dst_img = str(tmp_path / "test_gdal_translate_21.img")
Expand All @@ -594,6 +595,7 @@ def test_gdal_translate_21(gdal_translate_path, tmp_path):
# in that case, they must *NOT* be copied


@pytest.mark.require_driver("HFA")
def test_gdal_translate_22(gdal_translate_path, tmp_path):

dst_img = str(tmp_path / "test_gdal_translate_22.img")
Expand Down Expand Up @@ -662,6 +664,7 @@ def test_gdal_translate_24(gdal_translate_path, tmp_path):
# Test -norat


@pytest.mark.require_driver("HFA")
def test_gdal_translate_25(gdal_translate_path, tmp_path):

dst_tif = str(tmp_path / "test_gdal_translate_25.tif")
Expand Down Expand Up @@ -1007,6 +1010,7 @@ def test_gdal_translate_35(gdal_translate_path, tmp_vsimem):
# Test RAT is copied from hfa to gtiff - continuous/athematic


@pytest.mark.require_driver("HFA")
def test_gdal_translate_36(gdal_translate_path, tmp_path):

dst_tif = str(tmp_path / "test_gdal_translate_36.tif")
Expand All @@ -1032,6 +1036,7 @@ def test_gdal_translate_36(gdal_translate_path, tmp_path):
# Test RAT is copied from hfa to gtiff - thematic


@pytest.mark.require_driver("HFA")
def test_gdal_translate_37(gdal_translate_path, tmp_path):

dst1_tif = str(tmp_path / "test_gdal_translate_37.tif")
Expand Down
2 changes: 2 additions & 0 deletions autotest/utilities/test_gdalinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def test_gdalinfo_5(gdalinfo_path, tmp_path):
# Test a dataset with overviews and RAT


@pytest.mark.require_driver("HFA")
def test_gdalinfo_6(gdalinfo_path):

ret = gdaltest.runexternal(gdalinfo_path + " ../gdrivers/data/hfa/int.img")
Expand Down Expand Up @@ -537,6 +538,7 @@ def test_gdalinfo_stats(gdalinfo_path, tmp_path):
# Test a dataset with overviews and RAT


@pytest.mark.require_driver("HFA")
def test_gdalinfo_33(gdalinfo_path):

ret = gdaltest.runexternal(gdalinfo_path + " -json ../gdrivers/data/hfa/int.img")
Expand Down
2 changes: 1 addition & 1 deletion frmts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ if (CMAKE_BUILD_TYPE MATCHES "Debug" OR GDAL_ENABLE_DRIVER_NULL)
gdal_optional_format(null "NULL dummy driver")
endif ()

gdal_format(hfa "Erdas Imagine .img")
gdal_optional_format(hfa "Erdas Imagine .img")
gdal_optional_format(sdts "SDTS translator")
gdal_optional_format(nitf "National Imagery Transmission Format")
gdal_optional_format(gxf "GXF")
Expand Down
16 changes: 10 additions & 6 deletions frmts/hfa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ add_gdal_driver(
hfaopen.cpp
hfatype.cpp
hfa_overviews.cpp
BUILTIN)
PLUGIN_CAPABLE
NO_DEPS
)
gdal_standard_includes(gdal_HFA)
target_include_directories(gdal_HFA PRIVATE $<TARGET_PROPERTY:PROJ::proj,INTERFACE_INCLUDE_DIRECTORIES>)
target_compile_definitions(gdal_HFA PRIVATE $<TARGET_PROPERTY:PROJ::proj,INTERFACE_COMPILE_DEFINITIONS>)

add_executable(hfatest EXCLUDE_FROM_ALL hfatest.cpp ../../ogr/ogr_proj_p.cpp)
gdal_standard_includes(hfatest)
target_link_libraries(hfatest PRIVATE $<TARGET_NAME:${GDAL_LIB_TARGET_NAME}> gdal_HFA PROJ::proj)
gdal_target_link_libraries(gdal_HFA PRIVATE PROJ::proj)

if (NOT GDAL_ENABLE_DRIVER_HFA_PLUGIN)
add_executable(hfatest EXCLUDE_FROM_ALL hfatest.cpp ../../ogr/ogr_proj_p.cpp)
gdal_standard_includes(hfatest)
target_link_libraries(hfatest PRIVATE $<TARGET_NAME:${GDAL_LIB_TARGET_NAME}> gdal_HFA PROJ::proj)
endif()
4 changes: 4 additions & 0 deletions gcore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ if (ENABLE_PAM)
target_compile_definitions(gcore PRIVATE -DPAM_ENABLED)
endif ()

if (NOT GDAL_ENABLE_DRIVER_HFA OR GDAL_ENABLE_DRIVER_HFA_PLUGIN)
target_compile_definitions(gcore PRIVATE -DNO_HFA_SUPPORT)
endif()

add_subdirectory(mdreader)

# External libs now
Expand Down
6 changes: 6 additions & 0 deletions gcore/gdaldefaultoverviews.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,11 @@ CPLErr GDALDefaultOverviews::BuildOverviews(
pScaledOverviewWithoutMask);
if (bOvrIsAux)
{
#ifdef NO_HFA_SUPPORT
CPLError(CE_Failure, CPLE_NotSupported,
"This build does not support creating .aux overviews");
eErr = CE_Failure;
#else
if (nNewOverviews == 0)
{
/* if we call HFAAuxBuildOverviews() with nNewOverviews == 0 */
Expand All @@ -873,6 +878,7 @@ CPLErr GDALDefaultOverviews::BuildOverviews(
if (abValidLevel[j])
abRequireRefresh[j] = true;
}
#endif
}

/* -------------------------------------------------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion ogr/ogr_proj_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

/*! @cond Doxygen_Suppress */

PJ_CONTEXT *OSRGetProjTLSContext();
PJ_CONTEXT CPL_DLL *OSRGetProjTLSContext();
void OSRCleanupTLSContext();

class OSRProjTLSCache
Expand Down

0 comments on commit 624109b

Please sign in to comment.