Skip to content
Permalink
Browse files

Changed WKTReader/-Writer to work on CoordinateSequences

Changes to WKTReader
* added ALLOW_OLD_JTS_COORDINATE_SYNTAX configuration, including
  member variable that can be overridden
* added member variable to ALLOW_OLD_JTS_MULTIPOINT_SYNTAX
* added `measureToZ` flag value, that indicates for 'GEOMETRY M'
  entities the measure value is stored as z-ordinate value.
* cleaned up class

Changes to WKTWriter
* replaced StringBuffer with StringBuilder
* added zToMeasure flag to indicate that the writer should write
  'GEOMETRY M' entities in case output dimension is 3.
* re-enabled, added function parameters to make WKTWriter more
  re-entrant.
* added setter for PrecisionModel to use

Other changes:
* marginally adjusted unit tests to make them work.

JTS' commit
locationtech/jts@b464d54

NTS notes:
- lots of the unit test updates were to tests that NTS doesn't have, so I skipped those.
- I think there are still some errors in the tests, but because the original commit looks to have been rebased, nobody ever saw them at this point in time, so I'm going to wait until the end of the locationtech/jts#291 series before actually worrying about them.
  • Loading branch information...
airbreather committed Feb 28, 2019
1 parent a427e63 commit 2fc2ef6f6383a276af38bd0431541f8301214399
@@ -1,7 +1,10 @@
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using NetTopologySuite.Geometries.Implementation;
using NetTopologySuite.IO;
using NetTopologySuite.Tests.NUnit.TestData;
using NUnit.Framework;
@@ -91,8 +94,15 @@ private static void CheckWkbGeometry(byte[] wkb, string expectedWKT)
var wkbReader = new WKBReader();
var g2 = wkbReader.Read(wkb);

var reader = new WKTReader();
var expected = reader.Read(expectedWKT);
// JTS deviation: our default reader doesn't do Z by default, so in addition to XYM and
// XYZM, we also need to use a special reader for XYZ.
var useReader = new WKTReader();
if (Regex.IsMatch(expectedWKT, "(Z|(Z?M)) ?\\("))
{
useReader = new WKTReader(NtsGeometryServices.Instance.CreateGeometryFactory(PackedCoordinateSequenceFactory.DoubleFactory));
}

var expected = useReader.Read(expectedWKT);

bool isEqual = (expected.CompareTo(g2 /*, Comp2*/) == 0);
Assert.IsTrue(isEqual);
@@ -1,6 +1,7 @@
using System;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using NetTopologySuite.Geometries.Implementation;
using NetTopologySuite.IO;
using NUnit.Framework;

@@ -12,9 +13,10 @@ namespace NetTopologySuite.Tests.NUnit.IO
/// <version>1.7</version>
public class WKTWriterTest
{
private readonly IGeometryFactory _factory = new GeometryFactory(new PrecisionModel(1), 0);
private readonly IGeometryFactory _factory = new GeometryFactory(new PrecisionModel(1), 0, PackedCoordinateSequenceFactory.DoubleFactory);
private readonly WKTWriter _writer = new WKTWriter();
private readonly WKTWriter _writer3D = new WKTWriter(3);
private readonly WKTWriter _writer2DM = new WKTWriter(3) { ZIsMeasure = true };

[TestAttribute]
public void TestWritePoint()
@@ -183,7 +185,9 @@ public void TestWrite3D()
IGeometryFactory geometryFactory = new GeometryFactory();
var point = geometryFactory.CreatePoint(new CoordinateZ(1, 1, 1));
string wkt = _writer3D.Write(point);
Assert.AreEqual("POINT (1 1 1)", wkt);
Assert.AreEqual("POINT Z(1 1 1)", wkt);
wkt = _writer2DM.Write(point);
Assert.AreEqual("POINT M(1 1 1)", wkt);
}
[TestAttribute]
public void testWrite3D_withNaN()
@@ -193,7 +197,39 @@ public void testWrite3D_withNaN()
new CoordinateZ(2, 2, 2) };
var line = geometryFactory.CreateLineString(coordinates);
string wkt = _writer3D.Write(line);
Assert.AreEqual("LINESTRING (1 1, 2 2 2)", wkt);
Assert.AreEqual("LINESTRING Z(1 1, 2 2 2)", wkt);
wkt = _writer2DM.Write(line);
Assert.AreEqual("LINESTRING M(1 1, 2 2 2)", wkt);
}

[Test]
public void TestProperties()
{
Assert.That(_writer.OutputOrdinates, Is.EqualTo(Ordinates.XY));
Assert.That(_writer3D.OutputOrdinates, Is.EqualTo(Ordinates.XYZ));
Assert.That(_writer2DM.OutputOrdinates, Is.EqualTo(Ordinates.XYM));
Assert.That(_writer2DM.ZIsMeasure, Is.True);
_writer2DM.ZIsMeasure = false;
Assert.That(_writer2DM.ZIsMeasure, Is.False);
Assert.That(_writer2DM.OutputOrdinates, Is.EqualTo(Ordinates.XYZ));
_writer2DM.ZIsMeasure = true;
Assert.That(_writer2DM.OutputOrdinates, Is.EqualTo(Ordinates.XYM));

var gf = new GeometryFactory(PackedCoordinateSequenceFactory.DoubleFactory);
var writer3DM = new WKTWriter(4);
Assert.That(writer3DM.OutputOrdinates, Is.EqualTo(Ordinates.XYZM));
Assert.That(writer3DM.ZIsMeasure, Is.False);
writer3DM.ZIsMeasure = true;
Assert.That(writer3DM.ZIsMeasure, Is.False);

writer3DM.OutputOrdinates = Ordinates.XY;
Assert.That(writer3DM.OutputOrdinates, Is.EqualTo(Ordinates.XY));
writer3DM.OutputOrdinates = Ordinates.XYZ;
Assert.That(writer3DM.OutputOrdinates, Is.EqualTo(Ordinates.XYZ));
writer3DM.OutputOrdinates = Ordinates.XYM;
Assert.That(writer3DM.OutputOrdinates, Is.EqualTo(Ordinates.XYM));
writer3DM.OutputOrdinates = Ordinates.XYZM;
Assert.That(writer3DM.OutputOrdinates, Is.EqualTo(Ordinates.XYZM));
}
}
}
@@ -178,7 +178,7 @@ private void CreateFormatter(int precision)
? new PrecisionModel(PrecisionModels.Floating)
: new PrecisionModel(precision);
_formatter = WKTWriter.CreateFormatter(precisionModel);
string digits = WKTWriter.StringOfChar('#', _formatter.NumberDecimalDigits);
string digits = new string('#', _formatter.NumberDecimalDigits);
_format = string.Format("0.{0}", digits);
}

@@ -234,7 +234,8 @@ private void StartLine(string text, int level, StringBuilder sb)
{
if (LinePrefix != null)
sb.Append(LinePrefix);
sb.Append(WKTWriter.StringOfChar(' ', IndentSize * level));
if (level > 0)
sb.Append(new string(' ', IndentSize * level));
sb.Append(text);
}

@@ -108,8 +108,7 @@ private IList<IGeometry> Read(TextReader bufferedReader)
{
var geoms = new List<IGeometry>();
var tokens = _wktReader.Tokenizer(bufferedReader);
tokens.MoveNext();
while (!IsAtEndOfTokens(tokens.Current) && !IsAtLimit(geoms))
while (!IsAtEndOfTokens(tokens.NextToken(false)) && !IsAtLimit(geoms))
{
var g = _wktReader.ReadGeometryTaggedText(tokens);
if (_count >= Offset)

0 comments on commit 2fc2ef6

Please sign in to comment.
You can’t perform that action at this time.