-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3c8826
commit 2e03e72
Showing
9 changed files
with
130 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
namespace NetTopologySuite.IO.Esri.Dbf | ||
using System.Text; | ||
|
||
namespace NetTopologySuite.IO.Esri.Dbf | ||
{ | ||
/// <summary> | ||
/// Manages configurations and constants specific to the structure and operation of DBF files in the dBASE III format. | ||
/// </summary> | ||
internal static class Dbf | ||
{ | ||
public readonly static int TableDescriptorSize = 32; // Number of bytes in the table header | ||
internal readonly static int TableDescriptorSize = 32; // Number of bytes in the table header | ||
|
||
internal readonly static int FieldDescriptorSize = 32; // Number of bytes in the field descriptor | ||
internal readonly static int MaxFieldCount = 255; | ||
public readonly static byte Dbase3Version = 0x03; // dBASE III | ||
public readonly static byte HeaderTerminatorMark = 0x0D; | ||
internal readonly static byte Dbase3Version = 0x03; // dBASE III | ||
internal readonly static byte HeaderTerminatorMark = 0x0D; | ||
|
||
internal readonly static byte DeletedRecordMark = 0x2A; // '*' | ||
internal readonly static byte ValidRecordMark = 0x20; // ' ' | ||
internal readonly static byte EndOfFileMark = 0x1A; | ||
|
||
public readonly static byte DeletedRecordMark = 0x2A; // '*' | ||
public readonly static byte ValidRecordMark = 0x20; // ' ' | ||
public readonly static byte EndOfFileMark = 0x1A; | ||
internal static readonly int MaxFieldNameLength = 10; | ||
|
||
public static readonly int MaxFieldNameLength = 10; | ||
internal readonly static Encoding DefaultEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using NetTopologySuite.IO.Esri.Dbf.Fields; | ||
using NetTopologySuite.IO.Esri.Shapefiles.Writers; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace NetTopologySuite.IO.Esri.Test.Issues; | ||
|
||
/// <summary> | ||
/// https://github.com/NetTopologySuite/NetTopologySuite.IO.Esri/issues/53 | ||
/// </summary> | ||
internal class Issue053 | ||
{ | ||
[Test] | ||
public void Projection_Utf8_BOM() | ||
{ | ||
var fields = new List<DbfField>(); | ||
var fidField = fields.AddNumericInt32Field("fid"); | ||
var options = new ShapefileWriterOptions(ShapeType.Polygon, fields.ToArray()) | ||
{ | ||
Projection = "GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]" | ||
}; | ||
|
||
var shpPath = TestShapefiles.GetTempShpPath(); | ||
using (var shpWriter = Shapefile.OpenWrite(shpPath, options)) | ||
{ | ||
shpWriter.Geometry = SampleGeometry.SampleMultiPolygon; | ||
fidField.NumericValue = 1; | ||
shpWriter.Write(); | ||
} | ||
|
||
var expectedProjectionString = options.Projection; | ||
var expectedProjectionBytes = options.Encoding.GetBytes(options.Projection); | ||
|
||
var prjPath = Path.ChangeExtension(shpPath, ".prj"); | ||
var storedProjectionString = File.ReadAllText(prjPath); | ||
var storedProjectionBytes = File.ReadAllBytes(prjPath); | ||
|
||
TestShapefiles.DeleteShp(shpPath); | ||
|
||
Assert.AreEqual(expectedProjectionString, storedProjectionString); | ||
Assert.AreEqual(expectedProjectionBytes, storedProjectionBytes); | ||
} | ||
|
||
[Test] | ||
public static void Utf8_BOM_Default() | ||
{ | ||
var encoding = Encoding.UTF8; | ||
var filePath = Path.GetTempFileName(); | ||
var expectedString = "abc"; | ||
var expectedBytes = encoding.GetBytes(expectedString); | ||
WriteFile(filePath, expectedString, encoding); | ||
|
||
var storedString = File.ReadAllText(filePath, encoding); | ||
var storedBytes = File.ReadAllBytes(filePath); | ||
|
||
Assert.AreEqual(expectedString, storedString); // C# is cleaver enough to ignore BOM when reading | ||
Assert.AreNotEqual(expectedBytes, storedBytes); // Not equal because of BOM stored by default | ||
} | ||
|
||
[Test] | ||
public static void Utf8_BOM_Included() | ||
{ | ||
var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true); | ||
var filePath = Path.GetTempFileName(); | ||
var expectedString = "abc"; | ||
var expectedBytes = encoding.GetBytes(expectedString); | ||
WriteFile(filePath, expectedString, encoding); | ||
|
||
var storedString = File.ReadAllText(filePath, encoding); | ||
var storedBytes = File.ReadAllBytes(filePath); | ||
|
||
Assert.AreEqual(expectedString, storedString); // C# is cleaver enough to ignore BOM when reading | ||
Assert.AreNotEqual(expectedBytes, storedBytes); // Not equal because of BOM stored explicitly | ||
} | ||
|
||
[Test] | ||
public static void Utf8_BOM_Excluded() | ||
{ | ||
var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); | ||
var filePath = Path.GetTempFileName(); | ||
var expectedString = "abc"; | ||
var expectedBytes = encoding.GetBytes(expectedString); | ||
WriteFile(filePath, expectedString, encoding); | ||
|
||
var storedString = File.ReadAllText(filePath, encoding); | ||
var storedBytes = File.ReadAllBytes(filePath); | ||
|
||
Assert.AreEqual(expectedString, storedString); | ||
Assert.AreEqual(expectedBytes, storedBytes); | ||
} | ||
|
||
private static void WriteFile(string filePath, string content, Encoding encoding) | ||
{ | ||
using (StreamWriter writer = new StreamWriter(filePath, false, encoding)) | ||
{ | ||
writer.Write(content); | ||
} | ||
} | ||
} |