Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PrecisionModel for serializing/deserializing coordinates (STJ) #136

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public GeoJsonConverterFactory(GeometryFactory factory, bool writeGeometryBBox,
public override bool CanConvert(Type typeToConvert)
{
return GeometryTypes.Contains(typeToConvert)
|| typeToConvert == typeof(Envelope)
|| typeof(IFeature).IsAssignableFrom(typeToConvert)
|| typeToConvert == typeof(FeatureCollection)
|| typeof(IAttributesTable).IsAssignableFrom(typeToConvert);
Expand All @@ -200,6 +201,8 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer
{
if (GeometryTypes.Contains(typeToConvert))
return new StjGeometryConverter(_factory, _writeGeometryBBox, _ringOrientationOption);
if (typeToConvert == typeof(Envelope))
return new StjEnvelopeConverter(_factory.PrecisionModel);
if (typeToConvert == typeof(FeatureCollection))
return new StjFeatureCollectionConverter(_writeGeometryBBox);
if (typeof(IFeature).IsAssignableFrom(typeToConvert))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using NetTopologySuite.Geometries;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace NetTopologySuite.IO.Converters
{
internal class StjEnvelopeConverter : JsonConverter<Envelope>
{
private readonly PrecisionModel _precisionModel;

public StjEnvelopeConverter(PrecisionModel precisionModel)
{
_precisionModel = precisionModel;
}

public override Envelope Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Envelope res = null;

if (reader.TokenType == JsonTokenType.Null)
{
// #57: callers expect us to have read past the last token
reader.Read();
}
else
{
reader.ReadToken(JsonTokenType.StartArray);

double minX = reader.GetDouble(_precisionModel);
reader.Read();
double minY = reader.GetDouble(_precisionModel);
reader.Read();
double maxX = reader.GetDouble(_precisionModel);
reader.Read();
double maxY = reader.GetDouble(_precisionModel);
reader.Read();

if (reader.TokenType == JsonTokenType.Number)
{
maxX = maxY;
maxY = reader.GetDouble(_precisionModel);
reader.Read();
reader.Read();
}

reader.ReadToken(JsonTokenType.EndArray);

res = new Envelope(minX, maxX, minY, maxY);
}

//reader.Read(); // move away from array end
return res;
}

public override void Write(Utf8JsonWriter writer, Envelope value, JsonSerializerOptions options)
{
// if we don't want to write "null" bounding boxes, bail out.
if (value?.IsNull != false)
{
writer.WriteNullValue();

return;
}

writer.WriteStartArray();
writer.WriteNumberValue(value.MinX, _precisionModel);
writer.WriteNumberValue(value.MinY, _precisionModel);
writer.WriteNumberValue(value.MaxX, _precisionModel);
writer.WriteNumberValue(value.MaxY, _precisionModel);
writer.WriteEndArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ internal partial class StjGeometryConverter
{
private void WriteCoordinateSequence(Utf8JsonWriter writer, CoordinateSequence sequence, JsonSerializerOptions options, bool multiple = true, OrientationIndex orientation = OrientationIndex.None)
{
//writer.WritePropertyName("coordinates");
if (sequence == null)
{
writer.WriteNullValue();
Expand All @@ -29,15 +28,17 @@ private void WriteCoordinateSequence(Utf8JsonWriter writer, CoordinateSequence s
for (int i = 0; i < sequence.Count; i++)
{
writer.WriteStartArray();
writer.WriteNumberValue(sequence.GetX(i));
writer.WriteNumberValue(sequence.GetY(i));

writer.WriteNumberValue(sequence.GetX(i), _geometryFactory.PrecisionModel);
writer.WriteNumberValue(sequence.GetY(i), _geometryFactory.PrecisionModel);

if (hasZ)
{
double z = sequence.GetZ(i);
if (!double.IsNaN(z))
writer.WriteNumberValue(sequence.GetZ(i));
writer.WriteNumberValue(z, _geometryFactory.PrecisionModel);
}

writer.WriteEndArray();

if (!multiple) break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json;
using NetTopologySuite.Geometries;

namespace NetTopologySuite.IO.Converters
Expand All @@ -10,36 +8,14 @@ internal partial class StjGeometryConverter
internal static Envelope ReadBBox(ref Utf8JsonReader reader, JsonSerializerOptions options)
{
Envelope res = null;

if (reader.TokenType == JsonTokenType.Null)
{
// #57: callers expect us to have read past the last token
reader.Read();
}
else
{
reader.ReadToken(JsonTokenType.StartArray);

double minX = reader.GetDouble();
reader.Read();
double minY = reader.GetDouble();
reader.Read();
double maxX = reader.GetDouble();
reader.Read();
double maxY = reader.GetDouble();
reader.Read();

if (reader.TokenType == JsonTokenType.Number)
{
maxX = maxY;
maxY = reader.GetDouble();
reader.Read();
reader.Read();
}

reader.ReadToken(JsonTokenType.EndArray);

res = new Envelope(minX, maxX, minY, maxY);
res = JsonSerializer.Deserialize<Envelope>(ref reader, options);
}

//reader.Read(); // move away from array end
Expand All @@ -66,13 +42,7 @@ internal static void WriteBBox(Utf8JsonWriter writer, Envelope value, JsonSerial
}

writer.WritePropertyName("bbox");

writer.WriteStartArray();
writer.WriteNumberValue(value.MinX);
writer.WriteNumberValue(value.MinY);
writer.WriteNumberValue(value.MaxX);
writer.WriteNumberValue(value.MaxY);
writer.WriteEndArray();
JsonSerializer.Serialize(writer, value, typeof(Envelope), options);
}
}
}
19 changes: 19 additions & 0 deletions src/NetTopologySuite.IO.GeoJSON4STJ/Converters/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO.Properties;

namespace NetTopologySuite.IO.Converters
Expand Down Expand Up @@ -103,5 +104,23 @@ private static string CurrentTokenAsString(in Utf8JsonReader reader)
? reader.ValueSequence.ToArray()
: reader.ValueSpan.ToArray());
}

/// <summary>
/// Parses the current JSON token value from the source as a <see cref="double"/>. Rounds a value to the <see cref="PrecisionModel"/> grid.
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="precisionModel">The precision model to round to.</param>
/// <returns>The rounded value</returns>
internal static double GetDouble(this Utf8JsonReader reader, PrecisionModel precisionModel)
=> precisionModel.MakePrecise(reader.GetDouble());

/// <summary>
/// Rounds a <see cref="double"/> value to the <see cref="PrecisionModel"/> grid. Writes the rounded value (as a JSON number) as an element of a JSON array.
/// </summary>
/// <param name="writer">The writer.</param>
/// <param name="value">The value to write.</param>
/// <param name="precisionModel">The precision model to round to.</param>
internal static void WriteNumberValue(this Utf8JsonWriter writer, double value, PrecisionModel precisionModel)
=> writer.WriteNumberValue(precisionModel.MakePrecise(value));
}
}
37 changes: 37 additions & 0 deletions test/NetTopologySuite.IO.GeoJSON4STJ.Test/Issues/Issue135.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using NetTopologySuite.Geometries;
using NetTopologySuite.IO.Converters;
using NetTopologySuite.IO.GeoJSON4STJ.Test.Converters;
using NUnit.Framework;
using System.Text.Json;

namespace NetTopologySuite.IO.GeoJSON4STJ.Test.Issues
{
internal class Issue135 : SandDTest<Geometry>
{
[Test, GeoJsonIssueNumber(135)]
public void TestOutputPrecision()
{
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)
};
var factory = new GeometryFactory(new PrecisionModel(10), 4326);
var polygon = factory.CreatePolygon(coords);

string json = JsonSerializer.Serialize(polygon, new JsonSerializerOptions
{
ReadCommentHandling = JsonCommentHandling.Skip,
Converters =
{
new GeoJsonConverterFactory(factory)
}
});

Assert.That(json, Is.EqualTo("{\"type\":\"Polygon\",\"coordinates\":[[[0,0],[10.1,0],[10,10.1],[0.1,10],[0,0]]]}"));
}
}
}