Skip to content

Commit

Permalink
Issue #51
Browse files Browse the repository at this point in the history
Fix the issue with the column length mismatch: the 'D' column should be 8 characters long, and the 'L' column should be 1 character long.
  • Loading branch information
mahdirastegari committed Jul 2, 2024
1 parent e6b6323 commit d27c690
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static DbfField ReadDbaseFieldDescriptor(this Stream stream, Encoding enc
}
else if (type == DbfType.Date)
{
return new DbfDateField(name);
return new DbfDateField(name, length);
}
else if (type == DbfType.Numeric)
{
Expand All @@ -127,7 +127,7 @@ public static DbfField ReadDbaseFieldDescriptor(this Stream stream, Encoding enc
}
else if (type == DbfType.Logical)
{
return new DbfLogicalField(name);
return new DbfLogicalField(name, length);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ namespace NetTopologySuite.IO.Esri.Dbf.Fields
/// </summary>
public class DbfDateField : DbfField
{
private static readonly int FieldLength = 8; // This width is fixed and cannot be changed
private const int FieldLength = 8; // This width is fixed and cannot be changed
private static readonly string DateFormat = "yyyyMMdd";

/// <summary>
/// Initializes a new instance of the field class.
/// </summary>
/// <param name="name">Field name.</param>
public DbfDateField(string name)
: base(name, DbfType.Date, FieldLength, 0)
/// <param name="length">Field length.</param>
public DbfDateField(string name, int length = FieldLength)
: base(name, DbfType.Date, length, 0)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DbfLogicalField : DbfField
private readonly static byte DefaultValue = (byte)' '; // (byte)'?'; Initialized to 0x20 (space) otherwise T or F (http://www.dbase.com/KnowledgeBase/int/db7_file_fmt.htm)
private readonly static byte TrueValue = (byte)'T';
private readonly static byte FalseValue = (byte)'F';
private readonly static int FieldLength = 1; // This width is fixed and cannot be changed
private const int FieldLength = 1; // This width is fixed and cannot be changed

private readonly static string TrueValues = "TtYy";
private readonly static string FalseValues = "FfNn";
Expand All @@ -22,8 +22,9 @@ public class DbfLogicalField : DbfField
/// Initializes a new instance of the field class.
/// </summary>
/// <param name="name">Field name.</param>
public DbfLogicalField(string name)
: base(name, DbfType.Logical, FieldLength, 0)
/// <param name="length">Field length.</param>
public DbfLogicalField(string name, int length = FieldLength)
: base(name, DbfType.Logical, length, 0)
{
}

Expand All @@ -45,7 +46,7 @@ public override object Value
internal override void ReadValue(Stream stream)
{
var logicalValue = stream.ReadByteChar();

stream.Seek(stream.Position + Length - 1, SeekOrigin.Begin);
if (TrueValues.Contains(logicalValue))
{
LogicalValue = true;
Expand Down
46 changes: 46 additions & 0 deletions test/NetTopologySuite.IO.Esri.Test/Issues/Issue051.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.Linq;
using NetTopologySuite.Features;
using NetTopologySuite.IO.Esri.Dbf;
using NUnit.Framework;

namespace NetTopologySuite.IO.Esri.Test.Issues
{
/// <summary>
/// https://github.com/NetTopologySuite/NetTopologySuite.IO.Esri/issues/51
/// </summary>
internal class Issue051
{
[Test]
public void Read_When_ColumnLengthMissmatch()
{
var shpPath = TestShapefiles.PathTo("#51-columnLengthMissmatch.dbf");
List<IAttributesTable> attributes = null;
Assert.DoesNotThrow(() =>
{
var dbfReader = new DbfReader(shpPath);
attributes = dbfReader.ToList();
});

Assert.NotNull(attributes);

//First row is null and empty
Assert.AreEqual(string.Empty, attributes[0]["RoadLinkId"]);
Assert.AreEqual(null, attributes[0]["Tunnel"]);
Assert.AreEqual(null, attributes[0]["Bridge"]);
Assert.AreEqual(null, attributes[0]["CarAccess"]);
Assert.AreEqual(null, attributes[0]["Walk"]);
Assert.AreEqual(null, attributes[0]["MF_4_7"]);
Assert.AreEqual(null, attributes[0]["COREID"]);

//Second row has value but length is missmatch in each column
Assert.AreEqual("osgb4000000080908489", attributes[1]["RoadLinkId"]);
Assert.AreEqual(true, attributes[1]["Tunnel"]);
Assert.AreEqual(false, attributes[1]["Bridge"]);
Assert.AreEqual(true, attributes[1]["CarAccess"]);
Assert.AreEqual(4.8, attributes[1]["Walk"]);
Assert.AreEqual(50, attributes[1]["MF_4_7"]);
Assert.AreEqual(55483, attributes[1]["COREID"]);
}
}
}
Binary file not shown.

0 comments on commit d27c690

Please sign in to comment.