Skip to content

Commit

Permalink
Merge pull request #50 from NetTopologySuite/fix/issue049_handle_deci…
Browse files Browse the repository at this point in the history
…mal_fields

Handle decimal fields. Fix #49.
  • Loading branch information
KubaSzostak authored Jul 28, 2024
2 parents e6b6323 + 9a9c0ee commit e3c8826
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace NetTopologySuite.IO.Esri.Dbf.Fields
{
/// <summary>
/// Int64 field definition.
/// </summary>
public class DbfNumericDecimalField : DbfNumericField<decimal>
{
/// <summary>
/// Intializes new instance of the numerif field class.
/// </summary>
/// <param name="name">Field name.</param>
/// <param name="length">The number of significant digits (including decimal separator).</param>
/// <param name="precision">The number of fractional digits.</param>
public DbfNumericDecimalField(string name, int length, int precision)
: base(name, DbfType.Numeric, length, precision)
{
}

/// <summary>
/// Initializes a new instance of the field class.
/// </summary>
/// <param name="name">Field name.</param>
public DbfNumericDecimalField(string name) : this(name, MaxFieldLength, MaxFieldPrecision)
{
}

/// <inheritdoc/>
protected override decimal StringToNumber(string s)
{
return decimal.Parse(s, CultureInfo.InvariantCulture);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace NetTopologySuite.IO.Esri.Dbf.Fields
/// </summary>
public class DbfNumericDoubleField : DbfNumericField<double>
{
internal static readonly int DefaultFieldLength = 19; // 17 digits + decimal separator + sign https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types#characteristics-of-the-floating-point-types

/// <summary>
/// Intializes new instance of the numerif field class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ internal static DbfField Create(string name, int length, int precision)
return new DbfNumericInt32Field(name, length);
}

if (precision <= 0)
if (precision <= 0 && length <= DbfNumericInt64Field.DefaultFieldLength)
{
return new DbfNumericInt64Field(name, length);
}

return new DbfNumericDoubleField(name, length, precision);
if (length <= DbfNumericDoubleField.DefaultFieldLength)
{
return new DbfNumericDoubleField(name, length, precision);
}

return new DbfNumericDecimalField(name, length, precision);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace NetTopologySuite.IO.Esri.Dbf.Fields
/// </summary>
public class DbfNumericInt32Field : DbfNumericField<int>
{
internal static readonly int DefaultFieldLength = 10; //-2147483648..2147483647 => 11, but Esri uses 10
internal static readonly int DefaultFieldLength = 10; //-2147483648..2147483647 (11..10 digits)

/// <summary>
/// Intializes new instance of the numerif field class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace NetTopologySuite.IO.Esri.Dbf.Fields
/// </summary>
public class DbfNumericInt64Field : DbfNumericField<long>
{
internal static readonly int DefaultFieldLength = 19; // -9223372036854775808 to 9223372036854775807 (20..19 digits)

/// <summary>
/// Intializes new instance of the numerif field class.
/// </summary>
Expand Down
30 changes: 30 additions & 0 deletions test/NetTopologySuite.IO.Esri.Test/Issues/Issue049.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using NetTopologySuite.IO.Esri.Shapefiles.Readers;
using NUnit.Framework;

namespace NetTopologySuite.IO.Esri.Test.Issues;

/// <summary>
/// https://github.com/NetTopologySuite/NetTopologySuite.IO.Esri/issues/49
/// </summary>
internal class Issue049
{
[Test]
public void Decimal0_OpenShapefile()
{
var shpPath = TestShapefiles.PathTo("Issues/049/221-1_28.03.202411-16.shp");
var options = new ShapefileReaderOptions
{
GeometryBuilderMode = GeometryBuilderMode.FixInvalidShapes
};
using var shpReader = Shapefile.OpenRead(shpPath, options);

Assert.AreEqual(shpReader.ShapeType, ShapeType.Polygon);

while (shpReader.Read())
{
var dosis = shpReader.Fields["DOSIS"].Value.ToString();
Assert.IsNotEmpty(dosis);
Assert.IsFalse(shpReader.Geometry.IsEmpty);
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
Binary file not shown.
Binary file not shown.

0 comments on commit e3c8826

Please sign in to comment.