Skip to content

Commit da75a2f

Browse files
authored
Merge pull request #13887 from OSGeo/backport-13877-to-release/3.12
[Backport release/3.12] gdal raster as-features: make it output --layer-name and expose option when used in pipeline
2 parents 23654c4 + f5a0a5f commit da75a2f

8 files changed

+70
-10
lines changed

apps/gdalalg_abstract_pipeline.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ class GDALPipelineStepAlgorithm /* non final */ : public GDALAlgorithm
351351
void AddVectorInputArgs(bool hiddenForCLI);
352352
void AddVectorOutputArgs(bool hiddenForCLI,
353353
bool shortNameOutputLayerAllowed);
354+
using GDALAlgorithm::AddOutputLayerNameArg;
355+
void AddOutputLayerNameArg(bool hiddenForCLI,
356+
bool shortNameOutputLayerAllowed);
354357

355358
private:
356359
bool RunImpl(GDALProgressFunc pfnProgress, void *pProgressData) override;

apps/gdalalg_pipeline.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,7 @@ void GDALPipelineStepAlgorithm::AddVectorOutputArgs(
221221
}
222222
if (m_constructorOptions.addOutputLayerNameArgument)
223223
{
224-
AddArg(GDAL_ARG_NAME_OUTPUT_LAYER,
225-
shortNameOutputLayerAllowed ? 'l' : 0, _("Output layer name"),
226-
&m_outputLayerName)
227-
.AddHiddenAlias("nln") // For ogr2ogr nostalgic people
228-
.SetHiddenForCLI(hiddenForCLI);
224+
AddOutputLayerNameArg(hiddenForCLI, shortNameOutputLayerAllowed);
229225
}
230226
if (m_constructorOptions.addSkipErrorsArgument)
231227
{
@@ -235,6 +231,20 @@ void GDALPipelineStepAlgorithm::AddVectorOutputArgs(
235231
}
236232
}
237233

234+
/************************************************************************/
235+
/* GDALPipelineStepAlgorithm::AddOutputLayerNameArg() */
236+
/************************************************************************/
237+
238+
void GDALPipelineStepAlgorithm::AddOutputLayerNameArg(
239+
bool hiddenForCLI, bool shortNameOutputLayerAllowed)
240+
{
241+
AddArg(GDAL_ARG_NAME_OUTPUT_LAYER, shortNameOutputLayerAllowed ? 'l' : 0,
242+
_("Output layer name"),
243+
&m_outputLayerName)
244+
.AddHiddenAlias("nln") // For ogr2ogr nostalgic people
245+
.SetHiddenForCLI(hiddenForCLI);
246+
}
247+
238248
/************************************************************************/
239249
/* GDALPipelineStepAlgorithm::RunImpl() */
240250
/************************************************************************/

apps/gdalalg_raster_contour.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ GDALRasterContourAlgorithm::GDALRasterContourAlgorithm(bool standaloneStep)
5050
AddRasterInputArgs(false, false);
5151
AddVectorOutputArgs(false, false);
5252
}
53+
else
54+
{
55+
AddOutputLayerNameArg(/* hiddenForCLI = */ false,
56+
/* shortNameOutputLayerAllowed = */ false);
57+
}
5358

5459
// gdal_contour specific options
5560
AddBandArg(&m_band).SetDefault(1);

apps/gdalalg_raster_polygonize.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ GDALRasterPolygonizeAlgorithm::GDALRasterPolygonizeAlgorithm(
4646
AddRasterInputArgs(false, false);
4747
AddVectorOutputArgs(false, false);
4848
}
49+
else
50+
{
51+
AddOutputLayerNameArg(/* hiddenForCLI = */ false,
52+
/* shortNameOutputLayerAllowed = */ false);
53+
}
4954

5055
// gdal_polygonize specific options
5156
AddBandArg(&m_band).SetDefault(m_band);

apps/gdalalg_vector_select.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ GDALVectorSelectAlgorithm::GDALVectorSelectAlgorithm(bool standaloneStep)
3232
: GDALVectorPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL,
3333
standaloneStep)
3434
{
35+
if (!standaloneStep)
36+
{
37+
AddOutputLayerNameArg(/* hiddenForCLI = */ false,
38+
/* shortNameOutputLayerAllowed = */ false);
39+
}
3540
AddActiveLayerArg(&m_activeLayer);
3641
AddArg("fields", 0, _("Fields to select (or exclude if --exclude)"),
3742
&m_fields)
@@ -90,11 +95,14 @@ class GDALVectorSelectAlgorithmLayer final
9095
}
9196

9297
public:
93-
explicit GDALVectorSelectAlgorithmLayer(OGRLayer &oSrcLayer)
98+
explicit GDALVectorSelectAlgorithmLayer(
99+
OGRLayer &oSrcLayer, const std::string &osOutputLayerName)
94100
: GDALVectorPipelineOutputLayer(oSrcLayer),
95-
m_poFeatureDefn(new OGRFeatureDefn(oSrcLayer.GetName()))
101+
m_poFeatureDefn(new OGRFeatureDefn(osOutputLayerName.empty()
102+
? oSrcLayer.GetName()
103+
: osOutputLayerName.c_str()))
96104
{
97-
SetDescription(oSrcLayer.GetDescription());
105+
SetDescription(m_poFeatureDefn->GetName());
98106
SetMetadata(oSrcLayer.GetMetadata());
99107
m_poFeatureDefn->SetGeomType(wkbNone);
100108
m_poFeatureDefn->Reference();
@@ -301,8 +309,8 @@ bool GDALVectorSelectAlgorithm::RunStep(GDALPipelineStepRunContext &)
301309
if (m_activeLayer.empty() ||
302310
m_activeLayer == poSrcLayer->GetDescription())
303311
{
304-
auto poLayer =
305-
std::make_unique<GDALVectorSelectAlgorithmLayer>(*poSrcLayer);
312+
auto poLayer = std::make_unique<GDALVectorSelectAlgorithmLayer>(
313+
*poSrcLayer, m_outputLayerName);
306314
if (m_exclude)
307315
{
308316
poLayer->ExcludeFields(m_fields);

autotest/utilities/test_gdalalg_raster_contour.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,12 @@ def test_gdalalg_raster_contour_all_nodata(tmp_vsimem):
244244
ds = alg.Output()
245245
lyr = ds.GetLayer(0)
246246
assert lyr.GetFeatureCount() == 0
247+
248+
249+
def test_gdalalg_raster_contour_pipeline_output_layer(tmp_vsimem):
250+
251+
with gdal.alg.pipeline(
252+
pipeline="read ../gcore/data/byte.tif ! contour --interval 10 --output-layer foo"
253+
) as alg:
254+
ds = alg.Output()
255+
assert ds.GetLayer(0).GetName() == "foo"

autotest/utilities/test_gdalalg_raster_polygonize.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,12 @@ def test_gdalalg_raster_polygonize_no_next_usable_step_in_pipeline(tmp_vsimem):
304304

305305
with ogr.Open(out_filename) as ds:
306306
assert ds.GetLayer(0).GetFeatureCount() == 281
307+
308+
309+
def test_gdalalg_raster_polygonize_pipeline_output_layer(tmp_vsimem):
310+
311+
with gdal.alg.pipeline(
312+
pipeline="read ../gcore/data/byte.tif ! polygonize --output-layer foo"
313+
) as alg:
314+
ds = alg.Output()
315+
assert ds.GetLayer(0).GetName() == "foo"

autotest/utilities/test_gdalalg_vector_select.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,14 @@ def test_gdalalg_vector_select_active_layer():
241241

242242
out_lyr = out_ds.GetLayer(1)
243243
assert out_lyr.GetLayerDefn().GetFieldCount() == 2
244+
245+
246+
def test_gdalalg_vector_select_pipeline_output_layer():
247+
248+
with gdal.alg.vector.pipeline(
249+
pipeline="read ../ogr/data/poly.shp ! select --output-layer foo --fields EAS_ID"
250+
) as alg:
251+
ds = alg.Output()
252+
lyr = ds.GetLayer(0)
253+
assert lyr.GetDescription() == "foo"
254+
assert lyr.GetLayerDefn().GetName() == "foo"

0 commit comments

Comments
 (0)