NetTopologySuite FeaturesCollection serialization issues for GeoJSON

Trying to return some pretty simple GeoJSON data. I found NetTopologySuite and installed a simple FeatureCollection and tried to serialize it to a GeoJson string only to get the following error:

"The referencing cell found for the" CoordinateValue "property with type 'GeoAPI.Geometries.Coordinate'. Feature Track [0] .Geometry.Coordinates [0]."

Looking through class headers, Point uses Coordinate, which has the Coordinate property, so I can understand why there will be a circular reference. The thing is, most (if not all) geometries seem to use Point, so that would make it impossible to ever serialize anything ... if I didn't miss something.

So is this a mistake or am I missing something?

Tested only by a point and received the same error, so here is the code for this:

using NTS = NetTopologySuite; var ret = new NTS.Geometries.Point(42.9074, -78.7911); var jsonSerializer = NTS.IO.GeoJsonSerializer.Create(); var sw = new System.IO.StringWriter(); jsonSerializer.Serialize(sw, ret); var json = sw.ToString(); 
+6
source share
2 answers

Update

GeoJsonSerializer been moved to NetTopologySuite.IO.GeoJSON and now has its own static Create() method:

 /// <summary> /// Factory method to create a (Geo)JsonSerializer /// </summary> /// <remarks>Calls <see cref="GeoJsonSerializer.CreateDefault()"/> internally</remarks> /// <returns>A <see cref="JsonSerializer"/></returns> public new static JsonSerializer Create() { return CreateDefault(); } 

Using the direct constructor is deprecated:

 [Obsolete("Use GeoJsonSerializer.Create...() functions")] public GeoJsonSerializer() : this(Wgs84Factory) { } 

The code in the question should now work as expected.


Original answer

Use the default constructor for the GeoJsonSerializer class:

 var jsonSerializer = new NetTopologySuite.IO.GeoJsonSerializer(); 

This attaches a CoordinateConverter , which prevents the problem.

GeoJsonSerializer does not actually have a static Create() method, so you return to the base class JsonSerializer.Create() . In fact, the following may lead to a compiler error:

 GeoJsonSerializer jsonSerializer = NTS.IO.GeoJsonSerializer.Create(); 
+4
source

Instead of returning Json after you have already serialized, you can use:

  return Content(sw.ToString, "application/Json"); 
0
source

Source: https://habr.com/ru/post/1206269/


All Articles