Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from OSGeo:master #158

Merged
merged 5 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion apps/ogr2ogr_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4048,6 +4048,12 @@ static void DoFieldTypeConversion(GDALDataset *poDstDS,
}
oFieldDefn.SetType(OFTReal);
}
else if (oFieldDefn.GetType() == OFTDateTime && poDstDriver &&
EQUAL(poDstDriver->GetDescription(), "ESRI Shapefile"))
{
// Just be silent. The shapefile driver will itself emit a
// warning mentionning it converts DateTime to String.
}
else if (!bQuiet)
{
CPLError(
Expand Down Expand Up @@ -6518,7 +6524,10 @@ bool LayerTranslator::Translate(
}

poDstFeature->Reset();
if (poDstFeature->SetFrom(poFeature.get(), panMap, TRUE) !=

if (poDstFeature->SetFrom(
poFeature.get(), panMap, /* bForgiving = */ TRUE,
/* bUseISO8601ForDateTimeAsString = */ true) !=
OGRERR_NONE)
{
if (psOptions->nGroupTransactions)
Expand Down
10 changes: 10 additions & 0 deletions autotest/cpp/gtest_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@
#pragma GCC system_header
#endif

#if defined(__GNUC__)
// Workaround https://github.com/google/googletest/issues/4701
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcpp"
#endif

#include "gtest/gtest.h"

#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
first,second,third
1,two"with quote,3
10,twenty"with quote,30
20 changes: 20 additions & 0 deletions autotest/ogr/ogr_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2800,6 +2800,26 @@ def test_ogr_csv_double_quotes_in_middle_of_field():
assert f["str"] == "foo"


###############################################################################
# Test bugfix for https://github.com/OSGeo/gdal/issues/11660


def test_ogr_csv_double_quotes_in_middle_of_field_bis():

ds = ogr.Open("data/csv/double_quotes_in_middle_of_field_bis.csv")
lyr = ds.GetLayer(0)

f = lyr.GetNextFeature()
assert f["first"] == "1"
assert f["second"] == """two"with quote"""
assert f["third"] == "3"

f = lyr.GetNextFeature()
assert f["first"] == "10"
assert f["second"] == """twenty"with quote"""
assert f["third"] == "30"


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


Expand Down
43 changes: 43 additions & 0 deletions autotest/utilities/test_ogr2ogr_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3169,3 +3169,46 @@ def test_ogr2ogr_lib_transfer_filegdb_relationships(tmp_vsimem):
assert relationship.GetLeftTableName() == "table6"
assert relationship.GetRightTableName() == "table7"
assert relationship.GetMappingTableName() == "composite_many_to_many"


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


@gdaltest.enable_exceptions()
@pytest.mark.require_driver("GPKG")
@pytest.mark.parametrize("OGR2OGR_USE_ARROW_API", ["YES", "NO"])
def test_ogr2ogr_lib_datetime_in_shapefile(tmp_vsimem, OGR2OGR_USE_ARROW_API):

src_filename = str(tmp_vsimem / "src.gpkg")
with ogr.GetDriverByName("GPKG").CreateDataSource(src_filename) as src_ds:
src_lyr = src_ds.CreateLayer("test", geom_type=ogr.wkbNone)

field = ogr.FieldDefn("dt", ogr.OFTDateTime)
src_lyr.CreateField(field)
f = ogr.Feature(src_lyr.GetLayerDefn())
f.SetField("dt", "2022-05-31T12:34:56.789+05:30")
src_lyr.CreateFeature(f)

got_msg = []

def my_handler(errorClass, errno, msg):
got_msg.append(msg)
return

out_filename = str(tmp_vsimem / "out.dbf")
with gdaltest.error_handler(my_handler), gdaltest.config_options(
{"CPL_DEBUG": "ON", "OGR2OGR_USE_ARROW_API": OGR2OGR_USE_ARROW_API}
):
gdal.VectorTranslate(out_filename, src_filename)

if OGR2OGR_USE_ARROW_API == "YES":
assert "OGR2OGR: Using WriteArrowBatch()" in got_msg
else:
assert "OGR2OGR: Using WriteArrowBatch()" not in got_msg

print(got_msg)
assert "Field dt created as String field, though DateTime requested." in got_msg

with ogr.Open(out_filename) as dst_ds:
dst_lyr = dst_ds.GetLayer(0)
assert [f.GetField("dt") for f in dst_lyr] == ["2022-05-31T12:34:56.789+05:30"]
8 changes: 4 additions & 4 deletions ogr/ogrsf_frmts/shape/ogrshapelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2086,11 +2086,11 @@ OGRErr OGRShapeLayer::CreateField(const OGRFieldDefn *poFieldDefn,
case OFTDateTime:
CPLError(
CE_Warning, CPLE_NotSupported,
"Field %s create as date field, though DateTime requested.",
"Field %s created as String field, though DateTime requested.",
szNewFieldName);
chType = 'D';
nWidth = 8;
oModFieldDefn.SetType(OFTDate);
chType = 'C';
nWidth = static_cast<int>(strlen("YYYY-MM-DDTHH:MM:SS.sss+HH:MM"));
oModFieldDefn.SetType(OFTString);
break;

default:
Expand Down
63 changes: 42 additions & 21 deletions port/cpl_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,45 +647,66 @@ CSVReadParseLineGeneric(void *fp, const char *(*pfnReadLine)(void *, size_t),
return CSVSplitLine(pszLine, pszDelimiter, bKeepLeadingAndClosingQuotes,
bMergeDelimiter);

const size_t nDelimiterLength = strlen(pszDelimiter);
bool bInString = false; // keep in that scope !
std::string osWorkLine(pszLine); // keep in that scope !
size_t i = 0; // keep in that scope !

try
{
// We must now count the quotes in our working string, and as
// long as it is odd, keep adding new lines.
std::string osWorkLine(pszLine);

size_t i = 0;
int nCount = 0;

while (true)
{
for (; i < osWorkLine.size(); i++)
for (; i < osWorkLine.size(); ++i)
{
if (osWorkLine[i] == '\"')
nCount++;
{
if (!bInString)
{
// Only consider " as the start of a quoted string
// if it is the first character of the line, or
// if it is immediately after the field delimiter.
if (i == 0 ||
(i >= nDelimiterLength &&
osWorkLine.compare(i - nDelimiterLength,
nDelimiterLength, pszDelimiter,
nDelimiterLength) == 0))
{
bInString = true;
}
}
else if (i + 1 < osWorkLine.size() &&
osWorkLine[i + 1] == '"')
{
// Escaped double quote in a quoted string
++i;
}
else
{
bInString = false;
}
}
}

if (nCount % 2 == 0)
break;
if (!bInString)
{
return CSVSplitLine(osWorkLine.c_str(), pszDelimiter,
bKeepLeadingAndClosingQuotes,
bMergeDelimiter);
}

pszLine = pfnReadLine(fp, nMaxLineSize);
if (pszLine == nullptr)
const char *pszNewLine = pfnReadLine(fp, nMaxLineSize);
if (pszNewLine == nullptr)
break;

osWorkLine.append("\n");
osWorkLine.append(pszLine);
osWorkLine.append(pszNewLine);
}

char **papszReturn =
CSVSplitLine(osWorkLine.c_str(), pszDelimiter,
bKeepLeadingAndClosingQuotes, bMergeDelimiter);

return papszReturn;
}
catch (const std::exception &e)
{
CPLError(CE_Failure, CPLE_OutOfMemory, "%s", e.what());
return nullptr;
}
return nullptr;
}

/************************************************************************/
Expand Down
Loading