NetTopologySuite / NetTopologySuite.IO.GeoJSON

GeoJSON IO module for NTS.
BSD 3-Clause "New" or "Revised" License
112 stars 46 forks source link

Serializer ignores the PrecisionModel of geometries (STJ) #135

Open MizardX opened 1 year ago

MizardX commented 1 year ago

This is very similar to #27, but now for System.Text.Json.

Example:

var coords = new[]
{
    new Coordinate(0.001, 0.001),
    new Coordinate(10.1, 0.002),
    new Coordinate(10, 10.1),
    new Coordinate(0.05, 9.999),
    new Coordinate(0.001, 0.001)
};

// Creating the polygon with PrecisionModels.Fixed
var polygon = GeometryFactory.Fixed.CreatePolygon(coords);

var str = polygon.ToString();
// The precision is one decimal place as expected
// POLYGON ((0 0, 10.1 0, 10 10.1, 0.1 10, 0 0))

var json1 = JsonSerializer.Serialize(polygon, new JsonSerializerOptions
{
    Converters = { new GeoJsonConverterFactory() }
});
// The precision is ignored
// {"type":"Polygon","coordinates":[[[0.001,0.001],[10.1,0.002],[10.0,10.1],[0.05,9.999],[0.001,0.001]]]}

var json2 = JsonSerializer.Serialize(polygon, new JsonSerializerOptions
{
    Converters = { new GeoJsonConverterFactory(GeometryFactory.Fixed) }
});
// The precision is ignored
// {"type":"Polygon","coordinates":[[[0.001,0.001],[10.1,0.002],[10.0,10.1],[0.05,9.999],[0.001,0.001]]]}

NetTopologySuite.IO.GeoJSON4STJ version: 4.0.0

gitlsl commented 7 months ago

When serializing, it supports setting decimal places, which has a significant impact on the length of the result