Skip to content

NetTopologySuite IO project to read and write geometry in Tiny Well-Known-Binary fromat

License

Notifications You must be signed in to change notification settings

NetTopologySuite/NetTopologySuite.IO.TinyWKB

Repository files navigation

NetTopologySuite.IO.TinyWKB

An IO module for NTS to read and write geometries in the Tiny Well-Known-Binary format.

License GitHub Actions NuGet
License Build Status NuGet

Usage

Reading

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);

Reading id-lists

For MULTI-geometries or GEOMETRYCOLLECTIONs 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);

Writing

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);

Writing id-lists

As noted in [Reading id-lists] TWKB has the concept of identifiers for MULTI-geometries or GEOMETRYCOLLECTIONs. Writing identifiers is supported analogous to the reading mechanism:

  • The identifier is taken from Geometry.UserData. If that does not work, a new System.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 an IList<System.Int64> idList argument.

About

NetTopologySuite IO project to read and write geometry in Tiny Well-Known-Binary fromat

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages