An IO module for NTS to read and write geometries in the Tiny Well-Known-Binary format.
License | GitHub Actions | NuGet |
---|---|---|
Read a geometry like this. A TinyWkbReader
is reusable.
/*
bytes ... Array of bytes (byte[]) with twkb data from somewhere.
You can also pass a Stream or a BinaryReader
*/
var geometryReader = new TinyWkbReader();
var geometry = geometryReader.Read(bytes);
For MULTI
-geometries or GEOMETRYCOLLECTION
s the TWKB specification supports
storing an identifier (System.Int64
) for each contained geometry. There are 3
possible ways to obtain or handle these:
- The identifier is stored in the
Geometry.UserData
object (default). - The caller subscribes to the
TinyWkbReader.IdentifiersProvided
event.
The event's arguments provide access to the geometry that has been read along with the list of identifiers. - Call the
TinyWkbReader.Read(byte[], out IList<long> idList)
overload.
var geometryReader = new TinyWkbReader();
var geometry = geometryReader.Read(bytes, out var idList);
Write geometries like this. A TinyWkbWriter
is reusable.
/*
Create the writer. All constructor arguments are optional, the
default values are displayed.
*/
var geometryWriter = new TinyWkbWriter(
precisionXY: 7, // Number of decimal places for x- and y-ordinate values.
emitZ: true, // Emit z-ordinate values if geometry has them
precisionZ: 7, // number of decimal digits for z-ordinates
emitM: true, // Emit m-ordinate values if geometry has them
precisionM: 7, // number of decimal digits for m-ordinates
emitSize: false, // Emit the size of the geometry definition
emitBoundingBox: true,
// Emit the bounding box of the geometry for all dimensions
emitIdList: false // Emit a list of identifiers, one for every geometry in a
// MULTI-geometry or GEOMETRYCOLLECTION
);
/*
geometry ... Geometry from somewhere.
There are overloads for Write that take a Stream or
BinaryWriter as argument.
*/
var bytes = geometryWriter.Write(geometry);
As noted in [Reading id-lists] TWKB has the concept of identifiers for MULTI
-geometries or
GEOMETRYCOLLECTION
s. Writing identifiers is supported analogous to the reading mechanism:
- The identifier is taken from
Geometry.UserData
. If that does not work, a newSystem.Int64
value is created and used. - The caller subscribes to the
TinyWkbWriter.IdentifiersRequested
event, and fills adds the identifiers to the event's arguments. - Call one of the
TinyWkbWriter.Write
overloads that takes anIList<System.Int64> idList
argument.