Skip to content
Open
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
46 changes: 46 additions & 0 deletions autotest/utilities/test_gdalalg_raster_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,52 @@ def test_gdalalg_raster_clip_like():
assert ds.GetRasterBand(1).Checksum() == 3695


@pytest.mark.require_driver("PostgreSQL")
def test_gdalalg_raster_clip_like_postgis():

val = gdal.GetConfigOption("OGR_PG_CONNECTION_STRING", None)
if val is not None:
pg_connection_string = val
else:
pg_connection_string = "dbname=autotest"

try:
pg_ds = gdal.OpenEx(
"PG:" + pg_connection_string, gdal.OF_VECTOR | gdal.OF_UPDATE
)
pg_ds.CreateLayer("test_gdalalg_raster_clip_like_postgis_one")
pg_ds.CreateLayer("test_gdalalg_raster_clip_like_postgis_two")
pg_ds.Close()

except RuntimeError:
if val is None:
pytest.skip(
f"OGR_PG_CONNECTION_STRING not specified; Postgres is not available using default connection string {pg_connection_string}"
)
else:
pytest.skip(
f"Postgres is not available using supplied OGR_PG_CONNECTION_STRING {pg_connection_string}"
)

try:
alg = get_alg()
alg["input"] = "../gcore/data/byte.tif"
alg["output"] = ""
alg["output-format"] = "MEM"
alg["like"] = "PG:" + pg_connection_string
with pytest.raises(
Exception,
match="Only single layer dataset can be specified with --like when neither --like-layer or --like-sql have been specified",
):
alg.Run()
finally:
pg_ds = gdal.OpenEx(
"PG:" + pg_connection_string, gdal.OF_VECTOR | gdal.OF_UPDATE
)
pg_ds.ExecuteSQL("DROP TABLE test_gdalalg_raster_clip_like_postgis_one CASCADE")
pg_ds.ExecuteSQL("DROP TABLE test_gdalalg_raster_clip_like_postgis_two CASCADE")


def test_gdalalg_raster_clip_like_error(tmp_vsimem):

alg = get_alg()
Expand Down
21 changes: 21 additions & 0 deletions gcore/gdalalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,27 @@ bool GDALAlgorithm::ProcessDatasetArg(GDALAlgorithmArg *arg,
m_oMapDatasetNameToDataset.erase(oIter);
}

// A bit of a hack for situations like 'gdal raster clip --like "PG:..."'
// where the PG: dataset will be first opened with the PostGISRaster
// driver whereas the PostgreSQL (vector) one is actually wanted.
if (poDS->GetRasterCount() == 0 && (flags & GDAL_OF_RASTER) != 0 &&
(flags & GDAL_OF_VECTOR) != 0 && aosAllowedDrivers.empty() &&
aosOpenOptions.empty())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why to check for OO empty?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't too sure if it was appropriate to test another driver with the open options that could have been set for another one. In the particular case of the original issue, the --like operator doesn't support passing open options anyway...

{
auto poDrv = poDS->GetDriver();
if (poDrv && EQUAL(poDrv->GetDescription(), "PostGISRaster"))
{
// Retry with PostgreSQL (vector) driver
std::unique_ptr<GDALDataset> poTmpDS(GDALDataset::Open(
osDatasetName.c_str(), flags & ~GDAL_OF_RASTER));
if (poTmpDS)
{
poDS->ReleaseRef();
poDS = poTmpDS.release();
}
}
}

if (assignToOutputArg)
{
// Avoid opening twice the same datasource if it is both
Expand Down
Loading