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

[Backport release/3.8] Shapefile: recogize ' 0' as a null date #8848

Merged
merged 2 commits into from
Nov 28, 2023
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
25 changes: 25 additions & 0 deletions autotest/ogr/ogr_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -5819,3 +5819,28 @@ def test_ogr_shape_write_arrow_IF_FID_NOT_PRESERVED_ERROR(tmp_vsimem):
lyr.WriteArrowBatch(
schema, array, ["FID=OGC_FID", "IF_FID_NOT_PRESERVED=ERROR"]
)


###############################################################################
# Test writing an invalid "0000/00/00" date


@gdaltest.enable_exceptions()
def test_ogr_shape_write_date_0000_00_00(tmp_vsimem):

filename = tmp_vsimem / "test_ogr_shape_write_date_0000_00_00.shp"
ds = gdal.GetDriverByName("ESRI Shapefile").Create(
filename, 0, 0, 0, gdal.GDT_Unknown
)
lyr = ds.CreateLayer("test")
lyr.CreateField(ogr.FieldDefn("date", ogr.OFTDate))
f = ogr.Feature(lyr.GetLayerDefn())
f["date"] = "0000/00/00"
lyr.CreateFeature(f)
f = None
ds.Close()

ds = ogr.Open(filename)
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
assert f.IsFieldNull("date")
7 changes: 6 additions & 1 deletion ogr/ogrsf_frmts/shape/dbfopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,12 @@ static bool DBFIsValueNULL(char chType, const char *pszValue)

case 'D':
/* NULL date fields have value "00000000" */
return strncmp(pszValue, "00000000", 8) == 0;
/* Some DBF files have fields filled with spaces */
/* (trimmed by DBFReadStringAttribute) to indicate null */
/* values for dates (#4265). */
/* And others have ' 0': https://lists.osgeo.org/pipermail/gdal-dev/2023-November/058010.html */
return strncmp(pszValue, "00000000", 8) == 0 ||
strcmp(pszValue, " ") == 0 || strcmp(pszValue, "0") == 0;

case 'L':
/* NULL boolean fields have value "?" */
Expand Down
12 changes: 6 additions & 6 deletions ogr/ogrsf_frmts/shape/shape2ogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1447,12 +1447,6 @@ OGRFeature *SHPReadOGRFeature(SHPHandle hSHP, DBFHandle hDBF,
const char *const pszDateValue =
DBFReadStringAttribute(hDBF, iShape, iField);

// Some DBF files have fields filled with spaces
// (trimmed by DBFReadStringAttribute) to indicate null
// values for dates (#4265).
if (pszDateValue[0] == '\0')
continue;

OGRField sFld;
memset(&sFld, 0, sizeof(sFld));

Expand Down Expand Up @@ -1763,6 +1757,12 @@ OGRErr SHPWriteOGRFeature(SHPHandle hSHP, DBFHandle hDBF,
CE_Warning, CPLE_NotSupported,
"Year < 0 or > 9999 is not a valid date for shapefile");
}
else if (psField->Date.Year == 0 && psField->Date.Month == 0 &&
psField->Date.Day == 0)
{
DBFWriteNULLAttribute(
hDBF, static_cast<int>(poFeature->GetFID()), iField);
}
else
{
DBFWriteIntegerAttribute(
Expand Down
Loading