/
WKTWriterTest.cs
235 lines (220 loc) · 10.1 KB
/
WKTWriterTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using NetTopologySuite.Geometries.Implementation;
using NetTopologySuite.IO;
using NUnit.Framework;
namespace NetTopologySuite.Tests.NUnit.IO
{
/// <summary>
/// Test for <see cref="WKTWriter"/>.
/// </summary>
/// <version>1.7</version>
public class WKTWriterTest
{
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()
{
var point = _factory.CreatePoint(new Coordinate(10, 10));
Assert.AreEqual("POINT (10 10)", _writer.Write(point));
}
[TestAttribute]
public void TestWriteLineString()
{
Coordinate[] coordinates =
{
new CoordinateZ(10, 10, 0),
new CoordinateZ(20, 20, 0),
new CoordinateZ(30, 40, 0)
};
var lineString = _factory.CreateLineString(coordinates);
Assert.AreEqual("LINESTRING (10 10, 20 20, 30 40)", _writer.Write(lineString));
}
[TestAttribute]
public void TestWritePolygon()
{
Coordinate[] coordinates =
{
new CoordinateZ(10, 10, 0),
new CoordinateZ(10, 20, 0),
new CoordinateZ(20, 20, 0),
new CoordinateZ(20, 15, 0),
new CoordinateZ(10, 10, 0)
};
var linearRing = _factory.CreateLinearRing(coordinates);
var polygon = _factory.CreatePolygon(linearRing, new LinearRing[] { });
Assert.AreEqual("POLYGON ((10 10, 10 20, 20 20, 20 15, 10 10))", _writer.Write(polygon));
}
[TestAttribute]
public void TestWriteMultiPoint()
{
IPoint[] points =
{
_factory.CreatePoint(new CoordinateZ(10, 10, 0)),
_factory.CreatePoint(new CoordinateZ(20, 20, 0))
};
var multiPoint = _factory.CreateMultiPoint(points);
Assert.AreEqual("MULTIPOINT ((10 10), (20 20))", _writer.Write(multiPoint));
}
[TestAttribute]
public void TestWriteMultiLineString()
{
Coordinate[] coordinates1 =
{
new CoordinateZ(10, 10, 0),
new CoordinateZ(20, 20, 0)
};
var lineString1 = _factory.CreateLineString(coordinates1);
Coordinate[] coordinates2 =
{
new CoordinateZ(15, 15, 0),
new CoordinateZ(30, 15, 0)
};
var lineString2 = _factory.CreateLineString(coordinates2);
ILineString[] lineStrings = { lineString1, lineString2 };
var multiLineString = _factory.CreateMultiLineString(lineStrings);
Assert.AreEqual(
"MULTILINESTRING ((10 10, 20 20), (15 15, 30 15))",
_writer.Write(multiLineString));
}
[TestAttribute]
public void TestWriteMultiPolygon()
{
Coordinate[] coordinates1 =
{
new CoordinateZ(10, 10, 0),
new CoordinateZ(10, 20, 0),
new CoordinateZ(20, 20, 0),
new CoordinateZ(20, 15, 0),
new CoordinateZ(10, 10, 0)
};
var linearRing1 = _factory.CreateLinearRing(coordinates1);
var polygon1 = _factory.CreatePolygon(linearRing1, new LinearRing[] { });
Coordinate[] coordinates2 =
{
new CoordinateZ(60, 60, 0),
new CoordinateZ(70, 70, 0),
new CoordinateZ(80, 60, 0),
new CoordinateZ(60, 60, 0)
};
var linearRing2 = _factory.CreateLinearRing(coordinates2);
var polygon2 = _factory.CreatePolygon(linearRing2, new LinearRing[] { });
IPolygon[] polygons = { polygon1, polygon2 };
var multiPolygon = _factory.CreateMultiPolygon(polygons);
Assert.AreEqual(
"MULTIPOLYGON (((10 10, 10 20, 20 20, 20 15, 10 10)), ((60 60, 70 70, 80 60, 60 60)))",
_writer.Write(multiPolygon));
}
[TestAttribute]
public void TestWriteGeometryCollection()
{
var point1 = _factory.CreatePoint(new Coordinate(10, 10));
var point2 = _factory.CreatePoint(new Coordinate(30, 30));
Coordinate[] coordinates =
{
new CoordinateZ(15, 15, 0),
new CoordinateZ(20, 20, 0)
};
var lineString1 = _factory.CreateLineString(coordinates);
IGeometry[] geometries = { point1, point2, lineString1 };
var geometryCollection = _factory.CreateGeometryCollection(geometries);
Assert.AreEqual(
"GEOMETRYCOLLECTION (POINT (10 10), POINT (30 30), LINESTRING (15 15, 20 20))",
_writer.Write(geometryCollection));
}
/// <summary>
/// When writing "large numbers" (i.e: more than 15 digits), some precision is lost.
/// </summary>
/// <seealso href="https://code.google.com/p/nettopologysuite/issues/detail?id=171"/>
/// <seealso href="http://stackoverflow.com/questions/2105096/why-is-tostring-rounding-my-double-value"/>
[TestAttribute]
public void TestWriteLargeNumbers1()
{
IPrecisionModel precisionModel = new PrecisionModel(1E9);
IGeometryFactory geometryFactory = new GeometryFactory(precisionModel, 0);
var point1 = geometryFactory.CreatePoint(new Coordinate(123456789012345678d, 10E9));
Assert.AreEqual(123456789012345680d, point1.X);
Assert.AreEqual(10000000000d, point1.Y);
string actual = point1.AsText();
Assert.AreNotEqual("POINT (123456789012345680 10000000000)", actual);
Assert.AreEqual("POINT (123456789012346000 10000000000)", actual);
}
[TestAttribute]
public void TestWriteLargeNumbers2()
{
IPrecisionModel precisionModel = new PrecisionModel(1E9);
IGeometryFactory geometryFactory = new GeometryFactory(precisionModel, 0);
var point1 = geometryFactory.CreatePoint(new Coordinate(1234d, 10E9));
Assert.AreEqual("POINT (1234 10000000000)", point1.AsText());
}
/// <summary>
/// When writing "large numbers" (i.e: more than 15 digits), some precision is lost.
/// </summary>
/// <seealso href="https://code.google.com/p/nettopologysuite/issues/detail?id=171"/>
/// <seealso href="http://stackoverflow.com/questions/2105096/why-is-tostring-rounding-my-double-value"/>
[TestAttribute]
public void TestWriteLargeNumbers3()
{
IPrecisionModel precisionModel = new PrecisionModel(1E9);
IGeometryFactory geometryFactory = new GeometryFactory(precisionModel, 0);
var point1 = geometryFactory.CreatePoint(new Coordinate(123456789012345678000000E9d, 10E9));
Assert.AreEqual(123456789012345690000000000000000d, point1.X);
Assert.AreEqual(10000000000d, point1.Y);
Assert.AreNotEqual("POINT (123456789012345690000000000000000 10000000000)", point1.AsText());
Assert.AreEqual("POINT (123456789012346000000000000000000 10000000000)", point1.AsText());
}
[TestAttribute]
public void TestWrite3D()
{
IGeometryFactory geometryFactory = new GeometryFactory();
var point = geometryFactory.CreatePoint(new CoordinateZ(1, 1, 1));
string wkt = _writer3D.Write(point);
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()
{
IGeometryFactory geometryFactory = new GeometryFactory();
Coordinate[] coordinates = { new Coordinate(1, 1),
new CoordinateZ(2, 2, 2) };
var line = geometryFactory.CreateLineString(coordinates);
string wkt = _writer3D.Write(line);
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));
}
}
}