3

Hi I have been trying to serialize a polygon to a variable using GeoJSON4STJ for Nettopologysuite. So far deserialization works fine, but I am unable to serialize it. Is there any way to do this?

I have added the following code to the startup file as required

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

And I am trying to use the following lin

geoStr = JsonSerializer.Serialize(geometry);
Joren V
  • 41
  • 5
  • I'm trying to get the deserialization to work myself, to no avail. Have you tried deserializing as shown in the [GitHub readme](https://github.com/NetTopologySuite/NetTopologySuite.IO.GeoJSON#:~:text=Geometry%20to%20GeoJSON%3A)? – Joshua Abbott Jun 16 '22 at 14:50
  • @JoshuaAbbott Honestly, for deserialization I just assign the raw GeoJson data to a Geometry type (using NetTopologySuite.Geometries). Important to note in the Readme is that there are two packages 1) GeoJSON4STJ and 2) GeoJSON. I use the former which requires System.Text.Json, so if you use NewtonSoft, you might have to switch over as I have done. – Joren V Aug 01 '22 at 09:10
  • @JorenV I believe the question was the other way around: How to convert from `NetTopologySuite Geometry` to `GeoJSON` – GotRakija Jan 25 '23 at 11:24

2 Answers2

1

It's been a while but since people are still interested, using GeoJSON4STJ requires setting JsonSerializerOptions for System.Text.Json. This is needed for both serialization and deserialization.

Example:

using Microsoft.AspNetCore.Mvc;

_jsonOptions = new JsonOptions();
_jsonOptions.JsonSerializerOptions.Converters.Add(new NetTopologySuite.IO.Converters.GeoJsonConverterFactory());

var gf = NetTopologySuite.NtsGeometryServices.Instance.CreateGeometryFactory(4326);

// Create a polygon from Aurich over Emden, Leer to Aurich
Geometry geometry = gf.CreatePolygon(new[] {
    new NetTopologySuite.Geometries.Coordinate(7.5404, 53.4837),
    new NetTopologySuite.Geometries.Coordinate(7.1559, 53.3646),
    new NetTopologySuite.Geometries.Coordinate(7.4550, 53.2476),
    new NetTopologySuite.Geometries.Coordinate(7.5404, 53.4837),
});

var str = JsonSerializer.Serialize(geometry, _jsonOptions.JsonSerializerOptions);
Console.WriteLine(str);     

If you don't specifically need NetTopologySuite.IO.GeoJSON4STJ or System.Text.Json and are fine with NetTopologySuite.IO.GeoJSON, the answer of @shage_in_excelsior will serve you just fine.

Joren V
  • 41
  • 5
0

I know this question is aged and this answer doesn't quite exactly match your ask but I think it might make your specific request for GeoJSON4STJ unnecessary.

You can convert   NetTopologySuite   Geometry   directly to GeoJSON using `NetTopologySuite.IO' as follows:

using NetTopologySuite.IO;

var geometryData = <Your NetTopologySuite Geometry data>;
GeoJsonWriter _geoJsonWriter = new GeoJsonWriter();
var str = _geoJsonWriter.Write(geometryData);

This will very simply sidestep your need for GeoJSON4STJ to do the work.

Hope this helps someone.

  • Interesting, but in the end this sidesteps the issue by installing the same package, just the version without system.text.json support. I haven't specifically used NetTopologySuite.IO.GeoJSON, but it doesn't seem to add the deserialization functionality to controllers, which was more relevant for my personally. Still interesting and an easier solution in some cases I suppose – Joren V Jan 27 '23 at 09:05
  • This is where I ended up following guidance from Microsoft (which is quite sparse) and NetTopologySuite (which is also quite sparse). My project does not have `GeoJSON4STJ` simply as a result of not seeing other examples to follow. Now I know the right way thanks for the comment. – shage_in_excelsior Jan 27 '23 at 11:51