Skip to content

Commit

Permalink
gdal_grid: error out on in invalid layer name, SQL statement or faile…
Browse files Browse the repository at this point in the history
…d where condition (fixes OSGeo#9406)
  • Loading branch information
rouault committed Mar 7, 2024
1 parent 0214101 commit 6a80a69
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 14 deletions.
36 changes: 22 additions & 14 deletions apps/gdal_grid_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,20 +820,24 @@ GDALDatasetH GDALGrid(const char *pszDest, GDALDatasetH hSrcDataset,
{
OGRLayer *poLayer = poSrcDS->ExecuteSQL(
psOptions->pszSQL, psOptions->poSpatialFilter, nullptr);
if (poLayer != nullptr)
if (poLayer == nullptr)
{
// Custom layer will be rasterized in the first band.
eErr = ProcessLayer(
OGRLayer::ToHandle(poLayer), hDstDS, psOptions->poSpatialFilter,
nXSize, nYSize, 1, bIsXExtentSet, bIsYExtentSet, dfXMin, dfXMax,
dfYMin, dfYMax, psOptions->pszBurnAttribute,
psOptions->dfIncreaseBurnValue, psOptions->dfMultiplyBurnValue,
psOptions->eOutputType, psOptions->eAlgorithm,
psOptions->pOptions, psOptions->bQuiet, psOptions->pfnProgress,
psOptions->pProgressData);

poSrcDS->ReleaseResultSet(poLayer);
GDALGridOptionsFree(psOptionsToFree);
GDALClose(hDstDS);
return nullptr;
}

// Custom layer will be rasterized in the first band.
eErr = ProcessLayer(
OGRLayer::ToHandle(poLayer), hDstDS, psOptions->poSpatialFilter,
nXSize, nYSize, 1, bIsXExtentSet, bIsYExtentSet, dfXMin, dfXMax,
dfYMin, dfYMax, psOptions->pszBurnAttribute,
psOptions->dfIncreaseBurnValue, psOptions->dfMultiplyBurnValue,
psOptions->eOutputType, psOptions->eAlgorithm, psOptions->pOptions,
psOptions->bQuiet, psOptions->pfnProgress,
psOptions->pProgressData);

poSrcDS->ReleaseResultSet(poLayer);
}

/* -------------------------------------------------------------------- */
Expand All @@ -850,18 +854,22 @@ GDALDatasetH GDALGrid(const char *pszDest, GDALDatasetH hSrcDataset,
if (hLayer == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Unable to find layer \"%s\", skipping.",
"Unable to find layer \"%s\".",
psOptions->papszLayers && psOptions->papszLayers[i]
? psOptions->papszLayers[i]
: "null");
continue;
eErr = CE_Failure;
break;
}

if (psOptions->pszWHERE)
{
if (OGR_L_SetAttributeFilter(hLayer, psOptions->pszWHERE) !=
OGRERR_NONE)
{
eErr = CE_Failure;
break;
}
}

if (psOptions->poSpatialFilter != nullptr)
Expand Down
51 changes: 51 additions & 0 deletions autotest/utilities/test_gdal_grid_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def test_gdal_grid_lib_1(n43_tif, n43_shp):
outputType=gdal.GDT_Int16,
algorithm="nearest:radius1=0.0:radius2=0.0:angle=0.0",
spatFilter=spatFilter,
layers=[n43_shp.GetLayer(0).GetName()],
where="1=1",
)

# We should get the same values as in n43.td0
Expand Down Expand Up @@ -147,6 +149,7 @@ def test_gdal_grid_lib_2(tmp_vsimem, env):
width=1,
height=1,
outputType=gdal.GDT_Byte,
SQLStatement="select * from test_gdal_grid_lib_2",
)

ds2 = gdal.Grid(
Expand Down Expand Up @@ -1050,3 +1053,51 @@ def test_gdal_grid_lib_dict_arguments():
ind = opt.index("-co")

assert opt[ind : ind + 4] == ["-co", "COMPRESS=DEFLATE", "-co", "LEVEL=4"]


###############################################################################
# Test various error conditions


def test_gdal_grid_lib_errors():

mem_ds = gdal.GetDriverByName("Memory").Create("", 0, 0, 0, gdal.GDT_Unknown)
mem_ds.CreateLayer("test")

with pytest.raises(Exception, match='Unable to find layer "invalid"'):
gdal.Grid(
"",
mem_ds,
width=3,
height=3,
outputBounds=[-0.25, -0.25, 1.25, 1.25],
format="MEM",
algorithm="invdist",
layers=["invalid"],
)

with pytest.raises(
Exception, match='"invalid" not recognised as an available field'
):
gdal.Grid(
"",
mem_ds,
width=3,
height=3,
outputBounds=[-0.25, -0.25, 1.25, 1.25],
format="MEM",
algorithm="invdist",
where="invalid",
)

with pytest.raises(Exception, match="SQL Expression Parsing Error"):
gdal.Grid(
"",
mem_ds,
width=3,
height=3,
outputBounds=[-0.25, -0.25, 1.25, 1.25],
format="MEM",
algorithm="invdist",
SQLStatement="invalid",
)

0 comments on commit 6a80a69

Please sign in to comment.