Skip to content

Commit 6bd8997

Browse files
authored
Merge pull request OSGeo#12233 from dbaston/gdal-vector-geom-set-type-tweak-options
gdal vector geom set-type: replace dimension flags with single option
2 parents 2f50cc2 + 1d46a07 commit 6bd8997

File tree

4 files changed

+28
-55
lines changed

4 files changed

+28
-55
lines changed

apps/gdalalg_vector_geom_set_type.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,9 @@ GDALVectorGeomSetTypeAlgorithm::GDALVectorGeomSetTypeAlgorithm(
7575
&m_opts.m_curve)
7676
.SetMutualExclusionGroup("linear-curve");
7777

78-
AddArg("xy", 0, _("Force geometries to XY dimension"), &m_opts.m_xy)
79-
.SetMutualExclusionGroup("xy");
80-
AddArg("xyz", 0, _("Force geometries to XYZ dimension"), &m_opts.m_xyz)
81-
.SetMutualExclusionGroup("xy");
82-
AddArg("xym", 0, _("Force geometries to XYM dimension"), &m_opts.m_xym)
83-
.SetMutualExclusionGroup("xy");
84-
AddArg("xyzm", 0, _("Force geometries to XYZM dimension"), &m_opts.m_xyzm)
85-
.SetMutualExclusionGroup("xy");
78+
AddArg("dim", 0, _("Force geometries to the specified dimension"),
79+
&m_opts.m_dim)
80+
.SetChoices("XY", "XYZ", "XYM", "XYZM");
8681

8782
AddArg("skip", 0,
8883
_("Skip feature when change of feature geometry type failed"),
@@ -214,19 +209,19 @@ GDALVectorGeomSetTypeAlgorithmLayer::ConvertType(OGRwkbGeometryType eType) const
214209
eRetType = OGR_GT_GetCurve(eRetType);
215210
}
216211

217-
if (m_opts.m_xy)
212+
if (EQUAL(m_opts.m_dim.c_str(), "XY"))
218213
{
219214
eRetType = OGR_GT_Flatten(eRetType);
220215
}
221-
else if (m_opts.m_xyz)
216+
else if (EQUAL(m_opts.m_dim.c_str(), "XYZ"))
222217
{
223218
eRetType = OGR_GT_SetZ(OGR_GT_Flatten(eRetType));
224219
}
225-
else if (m_opts.m_xym)
220+
else if (EQUAL(m_opts.m_dim.c_str(), "XYM"))
226221
{
227222
eRetType = OGR_GT_SetM(OGR_GT_Flatten(eRetType));
228223
}
229-
else if (m_opts.m_xyzm)
224+
else if (EQUAL(m_opts.m_dim.c_str(), "XYZM"))
230225
{
231226
eRetType = OGR_GT_SetZ(OGR_GT_SetM(OGR_GT_Flatten(eRetType)));
232227
}
@@ -298,12 +293,11 @@ bool GDALVectorGeomSetTypeAlgorithm::RunStep(GDALProgressFunc, void *)
298293
if (!m_opts.m_type.empty())
299294
{
300295
if (m_opts.m_multi || m_opts.m_single || m_opts.m_linear ||
301-
m_opts.m_curve || m_opts.m_xy || m_opts.m_xyz || m_opts.m_xym ||
302-
m_opts.m_xyzm)
296+
m_opts.m_curve || !m_opts.m_dim.empty())
303297
{
304298
ReportError(CE_Failure, CPLE_AppDefined,
305299
"--geometry-type cannot be used with any of "
306-
"--multi/single/linear/multi/xy/xyz/xym/xyzm");
300+
"--multi/single/linear/multi/dim");
307301
return false;
308302
}
309303

apps/gdalalg_vector_geom_set_type.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ class GDALVectorGeomSetTypeAlgorithm final
4242
bool m_single = false;
4343
bool m_linear = false;
4444
bool m_curve = false;
45-
bool m_xy = false;
46-
bool m_xyz = false;
47-
bool m_xym = false;
48-
bool m_xyzm = false;
45+
std::string m_dim{};
4946
bool m_skip = false;
5047

5148
// Computed value from m_type

autotest/utilities/test_gdalalg_vector_geom_set_type.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ def test_gdalalg_vector_geom_set_type_geometry_type_invalid():
6767
alg.Run()
6868

6969

70-
@pytest.mark.parametrize(
71-
"other_option", ["multi", "single", "linear", "curve", "xy", "xyz", "xym", "xyzm"]
72-
)
70+
@pytest.mark.parametrize("other_option", ["multi", "single", "linear", "curve", "dim"])
7371
def test_gdalalg_vector_geom_set_type_geometry_type_exclusive_with_other_option(
7472
other_option,
7573
):
@@ -81,7 +79,7 @@ def test_gdalalg_vector_geom_set_type_geometry_type_exclusive_with_other_option(
8179
alg["output"] = ""
8280
alg["output-format"] = "stream"
8381
alg["geometry-type"] = "POINT"
84-
alg[other_option] = True
82+
alg[other_option] = "XY" if other_option == "dim" else True
8583

8684
with pytest.raises(Exception, match="cannot be used with any of"):
8785
alg.Run()
@@ -203,11 +201,11 @@ def test_gdalalg_vector_geom_set_type_geometry_type_feature_only():
203201
@pytest.mark.parametrize(
204202
"modifier,in_wkt,out_wkt",
205203
[
206-
("xy", "POINT Z (1 2 3)", "POINT (1 2)"),
207-
("xyz", "POINT (1 2)", "POINT Z (1 2 0)"),
208-
("xym", "POINT (1 2 3 4)", "POINT M (1 2 4)"),
209-
("xyzm", "POINT (1 2)", "POINT ZM (1 2 0 0)"),
210-
("xyzm", "POINT ZM (1 2 3 4)", "POINT ZM (1 2 3 4)"),
204+
(("dim", "xy"), "POINT Z (1 2 3)", "POINT (1 2)"),
205+
(("dim", "xyz"), "POINT (1 2)", "POINT Z (1 2 0)"),
206+
(("dim", "xym"), "POINT (1 2 3 4)", "POINT M (1 2 4)"),
207+
(("dim", "xyzm"), "POINT (1 2)", "POINT ZM (1 2 0 0)"),
208+
(("dim", "xyzm"), "POINT ZM (1 2 3 4)", "POINT ZM (1 2 3 4)"),
211209
("multi", "POINT Z (1 2 3)", "MULTIPOINT Z ((1 2 3))"),
212210
("single", "MULTIPOINT Z ((1 2 3))", "POINT Z (1 2 3)"),
213211
("single", "MULTILINESTRING ((1 2,3 4))", "LINESTRING (1 2,3 4)"),
@@ -239,7 +237,11 @@ def test_gdalalg_vector_geom_set_type_other_modifiers(modifier, in_wkt, out_wkt)
239237
alg["input"] = src_ds
240238
alg["output"] = ""
241239
alg["output-format"] = "stream"
242-
alg[modifier] = True
240+
241+
if type(modifier) is str:
242+
alg[modifier] = True
243+
else:
244+
alg[modifier[0]] = modifier[1]
243245

244246
assert alg.Run()
245247

doc/source/programs/gdal_vector_geom_set_type.rst

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ It can also be used as a step of :ref:`gdal_vector_pipeline`.
2828
The following groups of options can be combined together:
2929
- :option:`--multi` / :option:`--single`
3030
- :option:`--linear` / :option:`--curve`
31-
- :option:`--xy` / :option:`--xyz` / :option:`--xym` / :option:`--xyzm`
31+
- :option:`--dim`
3232

3333
Standard options
3434
++++++++++++++++
@@ -65,8 +65,7 @@ Standard options
6565
indicate the dimensionality.
6666

6767
This option is mutually exclusive with :option:`--multi`, :option:`--single`,
68-
:option:`--linear`, :option:`--curve`, :option:`--xy`, :option:`--xyz`,
69-
:option:`--xym`, :option:`--xyzm`.
68+
:option:`--linear`, :option:`--curve`, and :option:`--dim`.
7069

7170
.. option:: --multi
7271

@@ -97,28 +96,10 @@ Standard options
9796
or ``POLYGON`` ==> ``CURVEPOLYGON``. Points are kept unmodified.
9897
This option is mutually exclusive with :option:`--linear`.
9998

100-
.. option:: --xy
99+
.. option:: --dim
101100

102-
Force geometries to XY dimension.
103-
This option is mutually exclusive with :option:`--xyz`, :option:`--xym`, :option:`--xyzm`.
104-
105-
.. option:: --xyz
106-
107-
Force geometries to XYZ dimension. If the input geometry lacks a Z component,
108-
it will be set to 0.
109-
This option is mutually exclusive with :option:`--xy`, :option:`--xym`, :option:`--xyzm`.
110-
111-
.. option:: --xym
112-
113-
Force geometries to XYM dimension. If the input geometry lacks a M component,
114-
it will be set to 0.
115-
This option is mutually exclusive with :option:`--xy`, :option:`--xyz`, :option:`--xyzm`.
116-
117-
.. option:: --xyzm
118-
119-
Force geometries to XYZM dimension. If the input geometry lacks a Z or M component,
120-
it will be set to 0.
121-
This option is mutually exclusive with :option:`--xy`, :option:`--xyz`, :option:`--xym`.
101+
Force geometries to the specified dimension (XY, XYZ, XYM, or XYZM). If the input geometry lacks Z or M components, they will be set to 0. This option
102+
is mutually exclusive with :option:`--geometry-type`.
122103

123104
.. option:: --skip
124105

@@ -128,8 +109,7 @@ Standard options
128109
the output layer if it does not support mix of geometry types).
129110
This option applies both when the target geometry type is defined with
130111
:option:`--geometry-type`, or with any of :option:`--multi`, :option:`--single`,
131-
:option:`--linear`, :option:`--curve`, :option:`--xy`, :option:`--xyz`,
132-
:option:`--xym` or :option:`--xyzm`.
112+
:option:`--linear`, and :option:`--curve`.
133113

134114
Advanced options
135115
++++++++++++++++

0 commit comments

Comments
 (0)