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
6 changes: 6 additions & 0 deletions docs/reading-aml-data-sets.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ Acronyms of AML classes and attributes are lower case, such as `atsctl` and
`databa`, and acronym lookups are case-sensitive. The catalogues hold codes,
acronyms, names, attribute types and enumerated values, but not definitions.

`S57AttributeDefinition.Class` says whether an attribute is a feature,
national, spatial or cartographic attribute, and `IsNational` whether a
feature stores it in `NATF` rather than `ATTF`. The AML catalogues don't
classify their own attributes, so codes 20000 and up are feature attributes;
the IHO attributes they include keep their IHO class.

## Use the AML data set view

`AmlDataSet` is a view over the `S57Chart` of an AML cell. It doesn't copy the
Expand Down
5 changes: 5 additions & 0 deletions docs/reading-s57-charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ foreach (S57TypedFeature feature in chart.AllFeatures.Values)
`S57Document.GetFeatureRecord(S57LongName)` does the same on a document
without building a chart, but searches every record on each call.

In the binary encoding the FFPT pointer packs the long name into one 8-byte
`LNAM` subfield. `S57LongName.FromLnam` and `ToLnam` convert between the two,
and `S57RecordName.FromLongName(S57LongName)` gives the record name of a
feature with that long name.

## Apply update cells

A base cell (`.000`) is followed by update cells (`.001`, `.002`, and so on)
Expand Down
30 changes: 23 additions & 7 deletions scripts/GenerateObjectCatalogues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
// Source: the S-57 object catalogue tables GDAL ships with its S-57 driver
// (ogr/ogrsf_frmts/s57/data in https://github.com/OSGeo/gdal, MIT licence):
// s57objectclasses.csv object classes (code, name, acronym, class, primitives)
// s57attributes.csv attributes (code, name, acronym, type)
// s57attributes.csv attributes (code, name, acronym, type, class)
// s57expectedinput.csv enumerated values (attribute code, ID, meaning)
// GDAL derived them from the IHO S-57 Edition 3.1 Object Catalogue. Codes
// 17000-19999 are the Inland ENC entries GDAL kept from its former
// s57objectclasses_iw.csv/s57attributes_iw.csv; GDAL has no Inland ENC
// enumerations. Codes 20000 and up are AML entries and aren't used here.
//
// Without a data directory the script downloads the files from the GDAL tag
// below. Only codes, acronyms, names, classes, primitives, attribute types and
// enumerated values are written; definition text is never included.
// below. Only codes, acronyms, names, classes, primitives, attribute types,
// attribute classes and enumerated values are written; definition text is never included.
//
// Writes, in <output-dir> (default src/EncDotNet.S57):
// S57ObjectCatalogue.Iho.g.cs codes 1-16999
Expand Down Expand Up @@ -51,7 +51,8 @@
int.Parse(row["Code"], CultureInfo.InvariantCulture),
row["Acronym"].Trim(),
Normalize(row["Attribute"]),
row["Attributetype"].Trim()))
row["Attributetype"].Trim(),
row["Class"].Trim()))
.Where(row => row.Code > 0)
.ToList();

Expand Down Expand Up @@ -155,6 +156,10 @@ public sealed partial class S57ObjectCatalogue
foreach (var a in attributeList)
{
var head = $" new({a.Code}, {Literal(a.Acronym)}, {Literal(a.Name)}, {Type(a.Type)}";

// Feature is the constructor's default, so only the other classes are written.
var attributeClass = AttributeClass(a.Class, a.Code);
var tail = attributeClass == "Feature" ? ")" : $", attributeClass: S57AttributeClass.{attributeClass})";
if (values.TryGetValue(a.Code, out var list) && list.Count > 0)
{
builder.Append(head).Append(",\n [\n");
Expand All @@ -163,11 +168,11 @@ public sealed partial class S57ObjectCatalogue
builder.Append(CultureInfo.InvariantCulture, $" new({value.Code}, {Literal(value.Name)}),\n");
}

builder.Append(" ]),\n");
builder.Append(" ]").Append(tail).Append(",\n");
}
else
{
builder.Append(head).Append("),\n");
builder.Append(head).Append(tail).Append(",\n");
}
}

Expand Down Expand Up @@ -210,6 +215,17 @@ static void Rename(List<AttributeRow> rows, int code, string from, string to)
_ => "S57AttributeType.Unknown",
};

// The Class column of s57attributes.csv: S-57 Edition 3.1, Appendix A, Chapter 2 §2.2 (F),
// §2.3 (N) and §2.4 (S); $ marks the cartographic feature attributes.
static string AttributeClass(string value, int code) => value switch
{
"F" => "Feature",
"N" => "National",
"S" => "Spatial",
"$" => "Cartographic",
_ => throw new InvalidDataException($"Attribute {code} has an unknown class '{value}'."),
};

static string Primitives(string value)
{
var flags = new List<string>();
Expand Down Expand Up @@ -336,6 +352,6 @@ static List<string> ParseCsvLine(string line)

record ObjectClassRow(int Code, string Acronym, string Name, string Category, string Primitives);

record AttributeRow(int Code, string Acronym, string Name, string Type);
record AttributeRow(int Code, string Acronym, string Name, string Type, string Class);

record ValueRow(int Code, string Name);
43 changes: 43 additions & 0 deletions src/EncDotNet.S57/S57AttributeClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace EncDotNet.S57;

/// <summary>
/// The class of an S-57 attribute, as given in an object catalogue.
/// </summary>
/// <remarks>
/// <para>
/// S-57 Edition 3.1, Appendix A, Chapter 2 groups attributes into feature object attributes
/// (§2.2), national language attributes (§2.3) and spatial and meta object attributes (§2.4).
/// The catalogue tables mark the feature object attributes used for cartographic presentation,
/// whose acronyms start with <c>$</c>, as a fourth class.
/// </para>
/// <para>
/// The class decides where a feature record stores an attribute: national attributes are
/// encoded in the <c>NATF</c> field, at the data set's national lexical level (<c>NALL</c>), and
/// the other attributes of a feature in the <c>ATTF</c> field (S-57 Part 3, clauses 4.4, 4.5,
/// 7.6.3 and 7.6.4). Spatial attributes are encoded in the <c>ATTV</c> field of a vector record
/// (S-57 Part 3, clauses 5.1.2 and 7.7.1.2).
/// </para>
/// </remarks>
public enum S57AttributeClass
{
/// <summary>
/// A feature object attribute (<c>F</c>), such as <c>COLOUR</c>, encoded in <c>ATTF</c>.
/// </summary>
Feature = 0,

/// <summary>
/// A national language attribute (<c>N</c>), such as <c>NOBJNM</c>, encoded in <c>NATF</c>.
/// </summary>
National,

/// <summary>
/// A spatial object attribute (<c>S</c>), such as <c>POSACC</c>, encoded in a vector
/// record's <c>ATTV</c>.
/// </summary>
Spatial,

/// <summary>
/// A cartographic attribute (<c>$</c>), such as <c>$SCODE</c>, encoded in <c>ATTF</c>.
/// </summary>
Cartographic,
}
26 changes: 25 additions & 1 deletion src/EncDotNet.S57/S57AttributeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ internal S57AttributeDefinition(
string acronym,
string name,
S57AttributeType type,
IReadOnlyList<S57EnumeratedValue>? values = null)
IReadOnlyList<S57EnumeratedValue>? values = null,
S57AttributeClass attributeClass = S57AttributeClass.Feature)
{
Code = code;
Acronym = acronym;
Name = name;
Type = type;
Values = values ?? [];
Class = attributeClass;
}

/// <summary>Gets the attribute code, as it's encoded in <c>ATTL</c>.</summary>
Expand All @@ -34,6 +36,28 @@ internal S57AttributeDefinition(
/// <summary>Gets the type of the attribute's value.</summary>
public S57AttributeType Type { get; }

/// <summary>
/// Gets the class of the attribute: feature, national, spatial or cartographic.
/// </summary>
/// <remarks>
/// The class comes from S-57 Edition 3.1, Appendix A, Chapter 2. The AML catalogues don't
/// classify their attributes (codes 20000 and up), so those attributes are
/// <see cref="S57AttributeClass.Feature"/>; the IHO attributes the AML catalogues include
/// keep their IHO class.
/// </remarks>
public S57AttributeClass Class { get; }

/// <summary>
/// Gets a value indicating whether the attribute is a national attribute, which a feature
/// record stores in its <c>NATF</c> field rather than its <c>ATTF</c> field (S-57 Part 3,
/// clause 4.5).
/// </summary>
/// <remarks>
/// National attributes hold text in a national language, such as <c>NOBJNM</c>, and may use
/// any lexical level; see <see cref="S57AttributeClass.National"/>.
/// </remarks>
public bool IsNational => Class == S57AttributeClass.National;

/// <summary>
/// Gets the predefined values of an <see cref="S57AttributeType.Enumerated"/> or
/// <see cref="S57AttributeType.List"/> attribute, in code order.
Expand Down
14 changes: 2 additions & 12 deletions src/EncDotNet.S57/S57DocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,18 +1101,8 @@ private static S57RecordName DecomposeNameField(ReadOnlySpan<byte> data)
/// <summary>
/// Decomposes a LNAM composite subfield (8 bytes: AGEN(2) + FIDN(4) + FIDS(2)) into an S57LongName.
/// </summary>
private static S57LongName DecomposeLongNameField(ReadOnlySpan<byte> data)
{
if (data.Length < 8)
{
return default;
}

var agen = BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(0, 2));
var fidn = BinaryPrimitives.ReadUInt32LittleEndian(data.Slice(2, 4));
var fids = BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(6, 2));
return new S57LongName(agen, fidn, fids);
}
private static S57LongName DecomposeLongNameField(ReadOnlySpan<byte> data) =>
data.Length < S57LongName.LnamLength ? default : S57LongName.FromLnam(data[..S57LongName.LnamLength]);

/// <summary>
/// Parses an update control field (FSPC, FFPC, VRPC, or SGCC) from a record.
Expand Down
4 changes: 1 addition & 3 deletions src/EncDotNet.S57/S57DocumentWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,7 @@ private static Iso8211Record FeatureRecord(
encoder.Begin($"{S57FieldTags.FFPT} ({name})");
foreach (var pointer in feature.FeaturePointers)
{
encoder.B12(pointer.Name.AgencyCode, S57SubfieldNames.AGEN)
.B14(pointer.Name.FeatureId, S57SubfieldNames.FIDN)
.B12(pointer.Name.FeatureSubdivision, S57SubfieldNames.FIDS)
encoder.Lnam(pointer.Name)
.B11((byte)pointer.Relationship, S57SubfieldNames.RIND)
.Text(pointer.Comment, S57SubfieldNames.COMT, S57FieldEncoder.Latin1);
}
Expand Down
8 changes: 8 additions & 0 deletions src/EncDotNet.S57/S57FieldEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public S57FieldEncoder B14(long value, string subfield)
return this;
}

/// <summary>Writes a binary <c>LNAM</c> subfield (<c>B(64)</c>, S-57 Part 3, clause 4.3).</summary>
public S57FieldEncoder Lnam(S57LongName name)
{
name.TryWriteLnam(_buffer.GetSpan(S57LongName.LnamLength));
_buffer.Advance(S57LongName.LnamLength);
return this;
}

/// <summary>Writes a <c>b24</c> subfield.</summary>
public S57FieldEncoder B24(int value)
{
Expand Down
76 changes: 76 additions & 0 deletions src/EncDotNet.S57/S57LongName.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Buffers.Binary;
using System.Globalization;

namespace EncDotNet.S57;
Expand All @@ -7,10 +8,18 @@ namespace EncDotNet.S57;
/// object and that feature-to-feature pointers use to refer to it (S-57 Part 3, 4.3 and 4.6).
/// </summary>
/// <remarks>
/// <para>
/// A long name is the feature's producing agency (AGEN), feature identification number (FIDN)
/// and feature identification subdivision (FIDS), from its FOID field. Unlike a record's
/// <see cref="S57RecordName"/> (RCNM and RCID), it stays the same across editions and updates,
/// which is why FFPT pointers use it as their foreign key.
/// </para>
/// <para>
/// In the binary implementation an FFPT pointer encodes the long name as a single 64-bit
/// <c>LNAM</c> subfield: AGEN (<c>b12</c>), FIDN (<c>b14</c>) and FIDS (<c>b12</c>), each
/// little-endian (S-57 Part 3, clauses 4.3, 7.2.2.1 and 7.6.6). Use <see cref="FromLnam"/>,
/// <see cref="ToLnam"/> and <see cref="TryWriteLnam"/> to convert between the two.
/// </para>
/// </remarks>
public readonly record struct S57LongName
{
Expand All @@ -36,6 +45,73 @@ public S57LongName(ushort agencyCode, uint featureId, ushort featureSubdivision)
/// <summary>Gets the feature identification subdivision (FIDS).</summary>
public ushort FeatureSubdivision { get; init; }

/// <summary>
/// The length of a binary <c>LNAM</c> subfield, in bytes.
/// </summary>
public const int LnamLength = 8;

/// <summary>
/// Decodes a long name from a binary <c>LNAM</c> subfield (S-57 Part 3, clause 4.3).
/// </summary>
/// <param name="lnam">
/// The <see cref="LnamLength"/> bytes of the subfield: AGEN, FIDN and FIDS, each little-endian.
/// </param>
/// <returns>The long name.</returns>
/// <exception cref="ArgumentException">
/// <paramref name="lnam"/> isn't <see cref="LnamLength"/> bytes long.
/// </exception>
public static S57LongName FromLnam(ReadOnlySpan<byte> lnam)
{
if (lnam.Length != LnamLength)
{
throw new ArgumentException(
$"An LNAM subfield is {LnamLength} bytes long, but {lnam.Length} bytes were given.",
nameof(lnam));
}

return new S57LongName(
BinaryPrimitives.ReadUInt16LittleEndian(lnam),
BinaryPrimitives.ReadUInt32LittleEndian(lnam[2..]),
BinaryPrimitives.ReadUInt16LittleEndian(lnam[6..]));
}

/// <summary>
/// Encodes the long name as a binary <c>LNAM</c> subfield (S-57 Part 3, clause 4.3).
/// </summary>
/// <returns>
/// A new array of <see cref="LnamLength"/> bytes: AGEN, FIDN and FIDS, each little-endian.
/// </returns>
public byte[] ToLnam()
{
var lnam = new byte[LnamLength];
TryWriteLnam(lnam);
return lnam;
}

/// <summary>
/// Tries to encode the long name as a binary <c>LNAM</c> subfield (S-57 Part 3, clause 4.3).
/// </summary>
/// <param name="destination">
/// The span to write to. The first <see cref="LnamLength"/> bytes receive AGEN, FIDN and
/// FIDS, each little-endian.
/// </param>
/// <returns>
/// <c>true</c> when the long name was written; <c>false</c> when <paramref name="destination"/>
/// is shorter than <see cref="LnamLength"/> bytes, in which case nothing is written.
/// </returns>
public bool TryWriteLnam(Span<byte> destination)
{
if (destination.Length < LnamLength)
{
return false;
}

BinaryPrimitives.WriteUInt16LittleEndian(destination, AgencyCode);
BinaryPrimitives.WriteUInt32LittleEndian(destination[2..], FeatureId);
BinaryPrimitives.WriteUInt16LittleEndian(destination[6..], FeatureSubdivision);
return true;
}

/// <inheritdoc/>
public override string ToString() =>
string.Create(CultureInfo.InvariantCulture, $"AGEN={AgencyCode}, FIDN={FeatureId}, FIDS={FeatureSubdivision}");
Expand Down
Loading
Loading