Table of Contents

Class GeoJson

Namespace
SpatialNET.IO
Assembly
SpatialNET.IO.dll

Reading and writing GeoJSON (RFC 7946, ADR 0009) via System.Text.Json. Reading goes through the geometry factory; properties and a crs member are ignored, M values are dropped. Writing uses the leanest type, with shells counter-clockwise and holes clockwise.

public static class GeoJson

Inheritance

Inherited Members

Methods

Read(string, IGeometryFactory?)

Reads a geometry from a Geometry or Feature object.

public static IGeometry2D Read(string json, IGeometryFactory? factory = null)

Parameters

json string

The GeoJSON text.

factory IGeometryFactory?

The factory; null = SpatialNET.GeometryFactory.Default.

Returns

IGeometry2D

Exceptions

ArgumentNullException

json is null.

FormatException

Not valid GeoJSON; the message names the JSON path.

NotSupportedException

FeatureCollection or GeometryCollection (use SpatialNET.IO.GeoJson.ReadAll(System.String,SpatialNET.IGeometryFactory) instead).

Read<T>(string, IGeometryFactory?)

Reads a geometry of an expected kind.

public static T Read<T>(string json, IGeometryFactory? factory = null) where T : IGeometry2D

Parameters

json string

The GeoJSON text.

factory IGeometryFactory?

The factory; null = SpatialNET.GeometryFactory.Default.

Returns

T

Type Parameters

T

The expected geometry kind.

Exceptions

FormatException

Not valid GeoJSON or not a geometry of kind T.

Read(ReadOnlySpan<byte>, IGeometryFactory?)

Reads a geometry from UTF-8 bytes.

public static IGeometry2D Read(ReadOnlySpan<byte> utf8, IGeometryFactory? factory = null)

Parameters

utf8 ReadOnlySpan<byte>

The UTF-8 bytes.

factory IGeometryFactory?

The factory; null = SpatialNET.GeometryFactory.Default.

Returns

IGeometry2D

Read(Stream, IGeometryFactory?)

Reads a geometry from a stream (UTF-8).

public static IGeometry2D Read(Stream stream, IGeometryFactory? factory = null)

Parameters

stream Stream

The stream.

factory IGeometryFactory?

The factory; null = SpatialNET.GeometryFactory.Default.

Returns

IGeometry2D

ReadAll(string, IGeometryFactory?)

Reads all geometries (Geometry, Feature, FeatureCollection; GeometryCollections are flattened).

public static IReadOnlyList<IGeometry2D> ReadAll(string json, IGeometryFactory? factory = null)

Parameters

json string

The GeoJSON text.

factory IGeometryFactory?

The factory; null = SpatialNET.GeometryFactory.Default.

Returns

IReadOnlyList<IGeometry2D>

ReadAll(ReadOnlySpan<byte>, IGeometryFactory?)

Reads all geometries from UTF-8 bytes.

public static IReadOnlyList<IGeometry2D> ReadAll(ReadOnlySpan<byte> utf8, IGeometryFactory? factory = null)

Parameters

utf8 ReadOnlySpan<byte>

The UTF-8 bytes.

factory IGeometryFactory?

The factory; null = SpatialNET.GeometryFactory.Default.

Returns

IReadOnlyList<IGeometry2D>

ReadAll(Stream, IGeometryFactory?)

Reads all geometries from a stream (UTF-8).

public static IReadOnlyList<IGeometry2D> ReadAll(Stream stream, IGeometryFactory? factory = null)

Parameters

stream Stream

The stream.

factory IGeometryFactory?

The factory; null = SpatialNET.GeometryFactory.Default.

Returns

IReadOnlyList<IGeometry2D>

ToFeatureCollection(IEnumerable<IGeometry2D>, GeoJsonOptions?)

Writes geometries as a FeatureCollection with empty properties.

public static string ToFeatureCollection(IEnumerable<IGeometry2D> geometries, GeoJsonOptions? options = null)

Parameters

geometries IEnumerable<IGeometry2D>

The geometries.

options GeoJsonOptions?

The options; null = SpatialNET.IO.GeoJsonOptions.Default.

Returns

string

ToGeoJson(IGeometry2D, GeoJsonOptions?)

Writes the geometry as a GeoJSON Geometry object.

public static string ToGeoJson(this IGeometry2D geometry, GeoJsonOptions? options = null)

Parameters

geometry IGeometry2D

The geometry.

options GeoJsonOptions?

The options; null = SpatialNET.IO.GeoJsonOptions.Default.

Returns

string

Exceptions

ArgumentNullException

geometry is null.

NotSupportedException

Coordinates are NaN/infinite (not representable in JSON) or the geometry kind is unknown.

WriteTo(IGeometry2D, Utf8JsonWriter, GeoJsonOptions?)

Writes the geometry as a GeoJSON Geometry object to a System.Text.Json.Utf8JsonWriter.

public static void WriteTo(this IGeometry2D geometry, Utf8JsonWriter writer, GeoJsonOptions? options = null)

Parameters

geometry IGeometry2D

The geometry.

writer Utf8JsonWriter

The writer.

options GeoJsonOptions?

The options; null = SpatialNET.IO.GeoJsonOptions.Default.