Skip to content

Commit

Permalink
Merge pull request #7597 from rouault/fix_7595
Browse files Browse the repository at this point in the history
CSV: allow reading single column file (fixes #7595)
  • Loading branch information
rouault committed Apr 20, 2023
2 parents db219f3 + d3260c8 commit 8363245
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
2 changes: 2 additions & 0 deletions autotest/ogr/data/csv/single_column.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WKT
"POINT (1 2)"
12 changes: 12 additions & 0 deletions autotest/ogr/ogr_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2841,3 +2841,15 @@ def test_ogr_csv_double_quotes_in_middle_of_field():
assert f["id"] == "1"
assert f["coord"] == """50°46'06.6"N 116°42'04.4"""
assert f["str"] == "foo"


###############################################################################


def test_ogr_csv_single_column():

ds = ogr.Open("data/csv/single_column.csv")
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
assert f["WKT"] == "POINT (1 2)"
assert f.GetGeometryRef().ExportToIsoWkt() == "POINT (1 2)"
23 changes: 1 addition & 22 deletions ogr/ogrsf_frmts/csv/ogrcsvdatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ bool OGRCSVDataSource::OpenTable(const char *pszFilename,
nMaxLineSizeAsSize_t = static_cast<size_t>(-1);
}

// Read and parse a line. Did we get multiple fields?
// Read and parse a line to detect separator.

std::string osLine;
{
Expand Down Expand Up @@ -865,27 +865,6 @@ bool OGRCSVDataSource::OpenTable(const char *pszFilename,
osLine.find('|') != std::string::npos)
chDelimiter = '|';

char szDelimiter[2];
szDelimiter[0] = chDelimiter;
szDelimiter[1] = 0;
char **papszFields =
CSVReadParseLine3L(fp, nMaxLineSizeAsSize_t, szDelimiter,
true, // bHonourStrings,
false, // bKeepLeadingAndClosingQuotes
false, // bMergeDelimiter
true // bSkipBOM
);

if (CSLCount(papszFields) < 2)
{
VSIFCloseL(fp);
CSLDestroy(papszFields);
return false;
}

VSIRewindL(fp);
CSLDestroy(papszFields);

// Create a layer.
nLayers++;
papoLayers = static_cast<OGRLayer **>(
Expand Down

0 comments on commit 8363245

Please sign in to comment.