NetTopologySuite / NetTopologySuite.IO.GeoJSON

GeoJSON IO module for NTS.
BSD 3-Clause "New" or "Revised" License
113 stars 46 forks source link
geojson geometry json library nettopologysuite

GeoJSON

GeoJSON (RFC 7946) IO module for NTS.

GeoJSON4STJ Usage

This is the package for System.Text.Json serialization and deserialization.

ASP.NET Core Example

Add the System.Text.Json.Serializer.JsonConverterFactory, GeoJsonConverterFactory, to the JsonSerializerOptions when you configure your controllers, MVC, etc in the ConfigureServices method of your Startup.cs class.

public void ConfigureServices(IServiceCollection services) {
  services.AddControllers()
  .AddJsonOptions(options => {
    options.JsonSerializerOptions.Converters.Add(new NetTopologySuite.IO.Converters.GeoJsonConverterFactory());
  });
}

GeoJSON Usage

GeoJSON to Geometry:

var geoJson = "{\"type\":\"Point\",\"coordinates\":[0.0,0.0]}";
Geometry geometry;

var serializer = GeoJsonSerializer.Create();
using (var stringReader = new StringReader(geoJson))
using (var jsonReader = new JsonTextReader(stringReader))
{
    geometry = serializer.Deserialize<Geometry>(jsonReader);
}

Geometry to GeoJSON:

var geometry = new Point(0, 0);
string geoJson;

var serializer = GeoJsonSerializer.Create();
using (var stringWriter = new StringWriter())
using (var jsonWriter = new JsonTextWriter(stringWriter))
{
    serializer.Serialize(jsonWriter, geometry);
    geoJson = stringWriter.ToString();
}