Skip to content

Commit e1f94d2

Browse files
authored
Merge pull request #14494 from dbaston/csv-dropped-geom-warning
CSV: Emit warning if geometry will be dropped
2 parents 010d7d6 + 7bac73b commit e1f94d2

4 files changed

Lines changed: 35 additions & 4 deletions

File tree

autotest/ogr/ogr_csv.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,6 +2628,7 @@ def test_ogr_csv_string_quoting_always(tmp_vsimem):
26282628
"CREATE_CSVT=YES",
26292629
"STRING_QUOTING=ALWAYS",
26302630
"LINEFORMAT=LF",
2631+
"GEOMETRY=NONE",
26312632
],
26322633
)
26332634

@@ -3582,7 +3583,9 @@ def test_ogr_csv_32_bit_integer_invalid_value(tmp_vsimem):
35823583
def test_ogr_csv_do_not_write_header(tmp_vsimem):
35833584

35843585
filename = tmp_vsimem / "test.csv"
3585-
gdal.VectorTranslate(filename, "data/poly.shp", layerCreationOptions=["HEADER=NO"])
3586+
gdal.VectorTranslate(
3587+
filename, "data/poly.shp", layerCreationOptions=["HEADER=NO", "GEOMETRY=NONE"]
3588+
)
35863589
with ogr.Open(filename) as ds:
35873590
lyr = ds.GetLayer(0)
35883591
assert lyr.GetLayerDefn().GetFieldDefn(0).GetName() == "field_1"
@@ -3598,7 +3601,9 @@ def test_ogr_csv_open_dir(tmp_vsimem):
35983601
dirname = tmp_vsimem / "my_dir"
35993602
gdal.Mkdir(dirname, 0o755)
36003603
gdal.VectorTranslate(
3601-
dirname / "test.csv", "data/poly.shp", layerCreationOptions=["CREATE_CSVT=YES"]
3604+
dirname / "test.csv",
3605+
"data/poly.shp",
3606+
layerCreationOptions=["CREATE_CSVT=YES", "GEOMETRY=NONE"],
36023607
)
36033608

36043609
with ogr.Open(dirname) as ds:
@@ -3645,6 +3650,24 @@ def test_ogr_csv_used_creation_option_instead_of_layer_creation_option(tmp_vsime
36453650
###############################################################################
36463651

36473652

3653+
def test_ogr_csv_ignored_geometry_warning(tmp_vsimem):
3654+
3655+
errors = []
3656+
3657+
def test_handler(*args):
3658+
errors.append(args)
3659+
3660+
with gdaltest.error_handler(test_handler):
3661+
gdal.VectorTranslate(tmp_vsimem / "poly.csv", "data/poly.shp")
3662+
3663+
assert len(errors) == 1
3664+
assert errors[0][0] == gdal.CE_Warning
3665+
assert "GEOMETRY layer creation option not set" in errors[0][2]
3666+
3667+
3668+
###############################################################################
3669+
3670+
36483671
if __name__ == "__main__":
36493672
gdal.UseExceptions()
36503673
if len(sys.argv) != 2:

doc/source/drivers/vector/csv.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ The following layer creation options are supported:
456456
value of **CRLF** (DOS format) or **LF** (Unix format).
457457

458458
- .. lco:: GEOMETRY
459-
:choices: AS_WKT, AS_XYZ, AS_XY, AS_YX
459+
:choices: AS_WKT, AS_XYZ, AS_XY, AS_YX, NONE
460+
:default: NONE
460461

461462
By default, the geometry of
462463
a feature written to a .csv file is discarded. It is possible to

ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,14 +1142,20 @@ OGRCSVDataSource::ICreateLayer(const char *pszLayerName,
11421142
return nullptr;
11431143
}
11441144
}
1145-
else
1145+
else if (!EQUAL(pszGeometry, "NONE"))
11461146
{
11471147
CPLError(CE_Failure, CPLE_AppDefined,
11481148
"Unsupported value %s for creation option GEOMETRY",
11491149
pszGeometry);
11501150
return nullptr;
11511151
}
11521152
}
1153+
else if (eGType != wkbUnknown && eGType != wkbNone)
1154+
{
1155+
CPLError(CE_Warning, CPLE_AppDefined,
1156+
"Requested to create spatial CSV layer but GEOMETRY layer "
1157+
"creation option not set. No geometry will be output.");
1158+
}
11531159

11541160
// Should we create a CSVT file?
11551161
if (bCreateCSVT)

ogr/ogrsf_frmts/csv/ogrcsvdriver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ void RegisterOGRCSV()
351351
" <Value>AS_XYZ</Value>"
352352
" <Value>AS_XY</Value>"
353353
" <Value>AS_YX</Value>"
354+
" <Value>NONE</Value>"
354355
" </Option>"
355356
" <Option name='CREATE_CSVT' type='boolean' description='whether to "
356357
"create a .csvt file' default='NO'/>"

0 commit comments

Comments
 (0)