Skip to content
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
58 changes: 47 additions & 11 deletions docs/writing-s57-charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,31 @@ var document = new S57Document
DataSetIdentification = new S57DataSetIdentification
{
RecordName = S57RecordName.FromRcnmRcid(S57RecordNameCodes.DataSetGeneralInfo, 1),
ExchangePurpose = 1,
ExchangePurpose = (int)S57ExchangePurpose.New,
IntendedUsage = 5,
DataSetName = "XX5EXAMP.000",
EditionNumber = "1",
UpdateNumber = "0",
IssueDate = "20260101",
S57EditionNumber = "03.1",
ProductSpecification = 1,
ApplicationProfile = 1,
ProductSpecification = (int)S57ProductSpecification.Enc,
ApplicationProfile = (int)S57ApplicationProfile.EncNew,
ProducingAgency = 550,
DataStructure = 2,
AttfLexicalLevel = 1,
NatfLexicalLevel = 2
DataStructure = (int)S57DataStructure.ChainNode,
AttfLexicalLevel = (int)S57LexicalLevel.Latin1,
NatfLexicalLevel = (int)S57LexicalLevel.Ucs2
},
DataSetParameters = new S57DataSetParameters
{
RecordName = S57RecordName.FromRcnmRcid(S57RecordNameCodes.DataSetGeoReference, 1),
HorizontalDatum = 2,
HorizontalDatum = (int)S57HorizontalDatum.WGS84,
VerticalDatum = (int)S57VerticalDatum.MeanSeaLevel,
SoundingDatum = (int)S57VerticalDatum.LowestAstronomicalTide,
CompilationScale = 22000,
DepthUnits = 1,
HeightUnits = 1,
PositionalUnits = 1,
CoordinateUnits = 1,
DepthUnits = (int)S57DepthUnits.Metres,
HeightUnits = (int)S57HeightUnits.Metres,
PositionalUnits = (int)S57PositionalAccuracyUnits.Metres,
CoordinateUnits = (int)S57CoordinateUnits.LatitudeLongitude,
CoordinateMultiplicationFactor = 10_000_000,
SoundingMultiplicationFactor = 10
},
Expand Down Expand Up @@ -131,6 +133,27 @@ var document = new S57Document
byte[] cell = S57DocumentWriter.Write(document);
```

The coded `DSID` and `DSPM` subfields are `int` properties. Enums name their
values, and each value is the S-57 code, so cast in either direction:

| Subfield | Property | Enum |
|---|---|---|
| `EXPP` | `ExchangePurpose` | `S57ExchangePurpose` |
| `PRSP` | `ProductSpecification` | `S57ProductSpecification` |
| `PROF` | `ApplicationProfile` | `S57ApplicationProfile` |
| `DSTR` | `DataStructure` | `S57DataStructure` |
| `AALL`, `NALL` | `AttfLexicalLevel`, `NatfLexicalLevel` | `S57LexicalLevel` |
| `HDAT` | `HorizontalDatum` | `S57HorizontalDatum` |
| `VDAT`, `SDAT` | `VerticalDatum`, `SoundingDatum` | `S57VerticalDatum` |
| `DUNI` | `DepthUnits` | `S57DepthUnits` |
| `HUNI` | `HeightUnits` | `S57HeightUnits` |
| `PUNI` | `PositionalUnits` | `S57PositionalAccuracyUnits` |
| `COUN` | `CoordinateUnits` | `S57CoordinateUnits` |

A code an enum doesn't name, such as an AML product code, casts to an unnamed
value. To interpret the product of a cell you've read, use
`DataSetIdentification.Product` instead.

Coordinates and soundings are integers scaled by `COMF` and `SOMF`, as the
reader gives them. A feature's long name (`AGEN`, `FIDN`, `FIDS`) goes in its
`RecordName` and is written as its `FOID` field. A feature without one gets a
Expand All @@ -154,6 +177,19 @@ being replaced. Put text like that in a national attribute with `NALL` set to
2. UCS-2 covers the Basic Multilingual Plane only, so characters outside it,
such as emoji, can't be written at any level.

The writer uses the levels the document sets. To use the lowest levels that
encode the document's attribute values, pass the document through
`S57LexicalLevels.WithLowestLevels` before writing it. It throws
`ArgumentException` if an `ATTF` value needs UCS-2:

```csharp
byte[] cell = S57DocumentWriter.Write(S57LexicalLevels.WithLowestLevels(document));
```

For other text, `S57LexicalLevels.Lowest` returns the lowest level that encodes
a set of strings, and `S57LexicalLevels.CanEncode` checks one string. They make
the same checks as the writer.

The writer declares the levels in the DDR, as S-57 requires. It writes a
level 2 `NATF` field with two-byte terminators.

Expand Down
24 changes: 24 additions & 0 deletions src/EncDotNet.S57/S57ApplicationProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace EncDotNet.S57;

/// <summary>
/// The application profile identification codes of the DSID subfield PROF (S-57 Edition 3.1,
/// Part 3 §7.3.1.1 and §1.4.2).
/// </summary>
/// <remarks>
/// The values are the exact S-57 codes, so cast between this enum and
/// <see cref="S57DataSetIdentification.ApplicationProfile"/> in either direction. NATO Additional
/// Military Layers (AML) data sets use other codes (see
/// <see cref="S57DataSetIdentification.ApplicationProfile"/>), which cast to unnamed values
/// rather than failing.
/// </remarks>
public enum S57ApplicationProfile
{
/// <summary>ENC new (<c>EN</c>, code 1): a base cell.</summary>
EncNew = 1,

/// <summary>ENC revision (<c>ER</c>, code 2): an update cell.</summary>
EncRevision = 2,

/// <summary>IHO data dictionary (<c>DD</c>, code 3).</summary>
DataDictionary = 3
}
22 changes: 22 additions & 0 deletions src/EncDotNet.S57/S57CoordinateUnits.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace EncDotNet.S57;

/// <summary>
/// The coordinate unit codes of the DSPM subfield COUN (S-57 Edition 3.1, Part 3 §7.3.2.1 and
/// clause 3.2.1).
/// </summary>
/// <remarks>
/// The values are the exact S-57 codes, so cast between this enum and
/// <see cref="S57DataSetParameters.CoordinateUnits"/> in either direction. A code S-57 doesn't
/// define casts to an unnamed value rather than failing.
/// </remarks>
public enum S57CoordinateUnits
{
/// <summary>Latitude and longitude (<c>LL</c>, code 1).</summary>
LatitudeLongitude = 1,

/// <summary>Easting and northing (<c>EN</c>, code 2).</summary>
EastingNorthing = 2,

/// <summary>Units on the chart or map (<c>UC</c>, code 3).</summary>
UnitsOnChart = 3
}
18 changes: 16 additions & 2 deletions src/EncDotNet.S57/S57DataSetIdentification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed record S57DataSetIdentification
/// <remarks>
/// S-57 Edition 3.1, Part 3 §7.3.1.1 enumerates 1 = N (data set is new) and 2 = R (data set
/// is a revision). The value is 0 when the subfield is absent; <see cref="S57DocumentWriter"/>
/// writes 0 as 1.
/// writes 0 as 1. <see cref="S57ExchangePurpose"/> names the codes; cast between the two.
/// </remarks>
public int ExchangePurpose { get; init; }

Expand Down Expand Up @@ -41,12 +41,24 @@ public sealed record S57DataSetIdentification
public int ProducingAgency { get; init; }

/// <summary>Gets the data structure (DSTR).</summary>
/// <remarks>
/// The values are the DSSI codes of S-57 Edition 3.1, Part 3 §7.3.1.2, which
/// <see cref="S57DataStructure"/> names; cast between the two.
/// </remarks>
public int DataStructure { get; init; }

/// <summary>Gets the lexical level for ATTF (AALL).</summary>
/// <remarks>
/// <see cref="S57LexicalLevel"/> names the levels; cast between the two. ATTF allows levels 0
/// and 1 only. <see cref="S57LexicalLevels.Lowest"/> picks the lowest level for a set of values.
/// </remarks>
public int AttfLexicalLevel { get; init; }

/// <summary>Gets the lexical level for NATF (NALL).</summary>
/// <remarks>
/// <see cref="S57LexicalLevel"/> names the levels; cast between the two.
/// <see cref="S57LexicalLevels.Lowest"/> picks the lowest level for a set of values.
/// </remarks>
public int NatfLexicalLevel { get; init; }

/// <summary>Gets the comment (COMT).</summary>
Expand All @@ -63,7 +75,8 @@ public sealed record S57DataSetIdentification
/// Additional Military Layers (AML) producers declare 51-57 (see
/// <see cref="Aml.AmlProduct"/>). When the data set uses the ASCII lexical form, the mnemonics
/// <c>ENC</c> and <c>ODD</c> are mapped to 1 and 2. The value is 0 when the subfield is
/// absent. <see cref="Product"/> interprets it.
/// absent. <see cref="Product"/> interprets it. To set it, cast from
/// <see cref="S57ProductSpecification"/> (or <see cref="Aml.AmlProduct"/>).
/// </remarks>
public int ProductSpecification { get; init; }

Expand All @@ -81,6 +94,7 @@ public sealed record S57DataSetIdentification
/// SBO 18/19), and AML 3.0 uses 20 for both. When the data set uses the ASCII lexical form,
/// the S-57 mnemonics are mapped to their numeric codes. The value is 0 when the subfield is
/// absent. <see cref="S57ProductIdentity.IsUpdate"/> interprets it.
/// <see cref="S57ApplicationProfile"/> names the S-57 codes; cast between the two.
/// </remarks>
public int ApplicationProfile { get; init; }

Expand Down
28 changes: 28 additions & 0 deletions src/EncDotNet.S57/S57DataSetParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,55 @@ public sealed record S57DataSetParameters
public S57RecordName RecordName { get; init; }

/// <summary>Gets the horizontal geodetic datum (HDAT).</summary>
/// <remarks>
/// The values are those of the attribute <c>HORDAT</c>, which <see cref="S57HorizontalDatum"/>
/// names; cast between the two. 2 is WGS 84.
/// </remarks>
public int HorizontalDatum { get; init; }

/// <summary>Gets the vertical datum (VDAT).</summary>
/// <remarks>
/// The values are those of the attribute <c>VERDAT</c>, which <see cref="S57VerticalDatum"/>
/// names; cast between the two.
/// </remarks>
public int VerticalDatum { get; init; }

/// <summary>Gets the sounding datum (SDAT).</summary>
/// <remarks>
/// The values are those of the attribute <c>VERDAT</c>, which <see cref="S57VerticalDatum"/>
/// names; cast between the two.
/// </remarks>
public int SoundingDatum { get; init; }

/// <summary>Gets the compilation scale (CSCL).</summary>
public int CompilationScale { get; init; }

/// <summary>Gets the units of depth measurement (DUNI).</summary>
/// <remarks>
/// The values are those of the attribute <c>DUNITS</c>, which <see cref="S57DepthUnits"/> names;
/// cast between the two.
/// </remarks>
public int DepthUnits { get; init; }

/// <summary>Gets the units of height measurement (HUNI).</summary>
/// <remarks>
/// The values are those of the attribute <c>HUNITS</c>, which <see cref="S57HeightUnits"/> names;
/// cast between the two.
/// </remarks>
public int HeightUnits { get; init; }

/// <summary>Gets the units of positional accuracy (PUNI).</summary>
/// <remarks>
/// The values are those of the attribute <c>PUNITS</c>, which
/// <see cref="S57PositionalAccuracyUnits"/> names; cast between the two.
/// </remarks>
public int PositionalUnits { get; init; }

/// <summary>Gets the coordinate units (COUN).</summary>
/// <remarks>
/// <see cref="S57CoordinateUnits"/> names the codes of S-57 Edition 3.1, Part 3 §7.3.2.1; cast
/// between the two.
/// </remarks>
public int CoordinateUnits { get; init; }

/// <summary>Gets the coordinate multiplication factor (COMF).</summary>
Expand Down
29 changes: 29 additions & 0 deletions src/EncDotNet.S57/S57DataStructure.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace EncDotNet.S57;

/// <summary>
/// The data structure codes of the DSSI subfield DSTR (S-57 Edition 3.1, Part 3 §7.3.1.2 and
/// clause 3.1; Part 2 describes the topology levels).
/// </summary>
/// <remarks>
/// The values are the exact S-57 codes, so cast between this enum and
/// <see cref="S57DataSetIdentification.DataStructure"/> in either direction. A code S-57 doesn't
/// define casts to an unnamed value rather than failing. The ENC product specification requires
/// <see cref="ChainNode"/>.
/// </remarks>
public enum S57DataStructure
{
/// <summary>Cartographic spaghetti (<c>CS</c>, code 1).</summary>
CartographicSpaghetti = 1,

/// <summary>Chain-node (<c>CN</c>, code 2).</summary>
ChainNode = 2,

/// <summary>Planar graph (<c>PG</c>, code 3).</summary>
PlanarGraph = 3,

/// <summary>Full topology (<c>FT</c>, code 4).</summary>
FullTopology = 4,

/// <summary>Topology is not relevant (<c>NO</c>, code 255).</summary>
TopologyNotRelevant = 255
}
25 changes: 25 additions & 0 deletions src/EncDotNet.S57/S57DepthUnits.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace EncDotNet.S57;

/// <summary>
/// The depth units of the DSPM subfield DUNI, which takes its values from the attribute
/// <c>DUNITS</c> (S-57 Edition 3.1, Part 3 §7.3.2.1; Appendix A, chapter 2, attribute 89).
/// </summary>
/// <remarks>
/// The values are the exact S-57 codes, so cast between this enum and
/// <see cref="S57DataSetParameters.DepthUnits"/> (or a <c>DUNITS</c> attribute value) in either
/// direction. A code S-57 doesn't define casts to an unnamed value rather than failing.
/// </remarks>
public enum S57DepthUnits
{
/// <summary>Metres (code 1).</summary>
Metres = 1,

/// <summary>Fathoms and feet (code 2).</summary>
FathomsAndFeet = 2,

/// <summary>Feet (code 3).</summary>
Feet = 3,

/// <summary>Fathoms and fractions of fathoms (code 4).</summary>
FathomsAndFractions = 4
}
18 changes: 18 additions & 0 deletions src/EncDotNet.S57/S57ExchangePurpose.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace EncDotNet.S57;

/// <summary>
/// The exchange purpose codes of the DSID subfield EXPP (S-57 Edition 3.1, Part 3 §7.3.1.1).
/// </summary>
/// <remarks>
/// The values are the exact S-57 codes, so cast between this enum and
/// <see cref="S57DataSetIdentification.ExchangePurpose"/> in either direction. A code S-57 doesn't
/// define casts to an unnamed value rather than failing.
/// </remarks>
public enum S57ExchangePurpose
{
/// <summary>The data set is new (<c>N</c>, code 1).</summary>
New = 1,

/// <summary>The data set is a revision to an existing one (<c>R</c>, code 2).</summary>
Revision = 2
}
50 changes: 41 additions & 9 deletions src/EncDotNet.S57/S57FieldEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,50 @@ public Iso8211Field ToField(string tag, bool twoByteTerminator = false)
return new Iso8211Field { Tag = tag, Data = _buffer.WrittenSpan.ToArray() };
}

private void Encode(string value, string subfield, Encoding encoding)
/// <summary>
/// Gets whether <see cref="Text"/> can encode a value with an encoding, without throwing.
/// </summary>
/// <remarks>
/// This makes the same checks as the encoder, so <see cref="S57LexicalLevels"/> and the
/// writers can't disagree about which lexical level a string needs.
/// </remarks>
public static bool CanEncode(string value, Encoding encoding)
{
if (ReferenceEquals(encoding, Ucs2))
{
// UCS-2 implementation level 1 covers the Basic Multilingual Plane only.
foreach (var c in value)
{
if (char.IsSurrogate(c))
{
throw Invalid($"{subfield} value '{value}' has a character outside the Basic Multilingual Plane, which UCS-2 (lexical level 2) can't encode");
}
}
return !HasSurrogate(value);
}

try
{
encoding.GetByteCount(value);
return true;
}
catch (EncoderFallbackException)
{
return false;
}
}

/// <summary>Gets the encoding of a lexical level (S-57 Part 3, clause 2.4).</summary>
public static Encoding ForLexicalLevel(S57LexicalLevel level) => level switch
{
S57LexicalLevel.Ascii => Ascii,
S57LexicalLevel.Latin1 => Latin1,
S57LexicalLevel.Ucs2 => Ucs2,
_ => throw new ArgumentOutOfRangeException(nameof(level), level, "S-57 defines lexical levels 0, 1 and 2 only.")
};

// UCS-2 implementation level 1 covers the Basic Multilingual Plane only, so neither half of
// a UTF-16 surrogate pair (nor an unpaired surrogate) can be encoded.
private static bool HasSurrogate(string value) =>
value.AsSpan().IndexOfAnyInRange('\uD800', '\uDFFF') >= 0;

private void Encode(string value, string subfield, Encoding encoding)
{
if (ReferenceEquals(encoding, Ucs2) && HasSurrogate(value))
{
throw Invalid($"{subfield} value '{value}' has a character outside the Basic Multilingual Plane, which UCS-2 (lexical level 2) can't encode");
}

try
Expand Down
Loading
Loading