Skip to content

Commit

Permalink
Merge pull request #8848 from OSGeo/backport-8840-to-release/3.8
Browse files Browse the repository at this point in the history
[Backport release/3.8] Shapefile: recogize '      0' as a null date
  • Loading branch information
rouault authored Nov 28, 2023
2 parents cb40027 + 72e2c6d commit c0cd9c6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
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

0 comments on commit c0cd9c6

Please sign in to comment.