GeoJson Class

Reads and writes GeoJSON (RFC 7946): a FeatureCollection, a Feature or a geometry.

public static class GeoJson

Remarks

The reader keeps the altitudes, ignores the bbox and the foreign members, except the name of a FeatureCollection, which becomes GeoFeatureCollection.Name. The properties are a String, a Double, a Boolean, null, or a JsonElement for an object or an array. A numeric id becomes its JSON text.

The writer writes the identifiers as strings, the numbers as JSON numbers (null when they are not finite), a JsonElement as it is, a dictionary as an object, a collection as an array, and the other values as their text.

Methods

NameDescription
Format(GeoFeatureCollection, GeoJsonWriterOptions?)Writes features as a GeoJSON FeatureCollection.
FormatGeometry(GeoGeometry, GeoJsonWriterOptions?)Writes a geometry as GeoJSON.
Parse(string)Reads the features of a GeoJSON document.
ParseGeometry(string)Reads a GeoJSON geometry, such as {"type":"Point","coordinates":[2.35,48.86]}.
Read(Stream)Reads the features of a GeoJSON document.
ReadFeaturesAsync(Stream, CancellationToken)Reads the features of a GeoJSON document one at a time, with bounded memory: the features of a FeatureCollection are returned as soon as they are read, and only the feature being read is kept in memory.
Write(GeoFeatureCollection, Stream, GeoJsonWriterOptions?)Writes features as a GeoJSON FeatureCollection, encoded in UTF-8.

Format(GeoFeatureCollection, GeoJsonWriterOptions?)

GeoJson.cs

Writes features as a GeoJSON FeatureCollection.

public static string Format(GeoFeatureCollection features, GeoJsonWriterOptions? options = null)

Parameters

features GeoFeatureCollection
The features.
options GeoJsonWriterOptions?
The options, or null for the defaults.

Returns

string The GeoJSON text.

Exceptions

ArgumentException
A position is not finite.

FormatGeometry(GeoGeometry, GeoJsonWriterOptions?)

GeoJson.cs

Writes a geometry as GeoJSON.

public static string FormatGeometry(GeoGeometry geometry, GeoJsonWriterOptions? options = null)

Parameters

geometry GeoGeometry
The geometry.
options GeoJsonWriterOptions?
The options, or null for the defaults.

Returns

string The GeoJSON text of the geometry.

Exceptions

ArgumentException
A position is not finite.

Parse(string)

GeoJson.cs

Reads the features of a GeoJSON document.

public static GeoFeatureCollection Parse(string json)

Parameters

json string
The GeoJSON text.

Returns

GeoFeatureCollection The features of a FeatureCollection, the feature of a Feature, or a feature without properties for a geometry.

Exceptions

FormatException
The text is not valid JSON or not GeoJSON.

ParseGeometry(string)

GeoJson.cs

Reads a GeoJSON geometry, such as {"type":"Point","coordinates":[2.35,48.86]}.

public static GeoGeometry ParseGeometry(string json)

Parameters

json string
The GeoJSON text of a geometry.

Returns

GeoGeometry The geometry.

Exceptions

FormatException
The text is not valid JSON or not a GeoJSON geometry.

Read(Stream)

GeoJson.cs

Reads the features of a GeoJSON document.

public static GeoFeatureCollection Read(Stream utf8Json)

Parameters

utf8Json Stream
The GeoJSON document, encoded in UTF-8. It is left open.

Returns

GeoFeatureCollection The features of a FeatureCollection, the feature of a Feature, or a feature without properties for a geometry.

Exceptions

FormatException
The document is not valid JSON or not GeoJSON.

ReadFeaturesAsync(Stream, CancellationToken)

GeoJson.cs

Reads the features of a GeoJSON document one at a time, with bounded memory: the features of a FeatureCollection are returned as soon as they are read, and only the feature being read is kept in memory.

public static IAsyncEnumerable<GeoFeature> ReadFeaturesAsync(Stream utf8Json, CancellationToken cancellationToken = default)

Parameters

utf8Json Stream
The GeoJSON document, encoded in UTF-8. It is read as the features are enumerated, and left open.
cancellationToken CancellationToken
The token to cancel the reading.

Returns

IAsyncEnumerable<GeoFeature> The features of a FeatureCollection, the feature of a Feature, or a feature without properties for a geometry. A document that is not a FeatureCollection is read whole before its feature is returned.

Exceptions

FormatException
The document is not valid JSON or not GeoJSON. The features read before the error are returned first.

Remarks

The name of the collection is not returned: use GeoJson.Read to get it. The members of the collection can be in any order: when the type follows the features array and is not FeatureCollection, the enumeration throws a FormatException once the features are returned.

Write(GeoFeatureCollection, Stream, GeoJsonWriterOptions?)

GeoJson.cs

Writes features as a GeoJSON FeatureCollection, encoded in UTF-8.

public static void Write(GeoFeatureCollection features, Stream utf8Json, GeoJsonWriterOptions? options = null)

Parameters

features GeoFeatureCollection
The features.
utf8Json Stream
The stream to write to. It is left open.
options GeoJsonWriterOptions?
The options, or null for the defaults.

Exceptions

ArgumentException
A position is not finite.