/
Coordinate.cs
427 lines (388 loc) · 16.4 KB
/
Coordinate.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace NetTopologySuite.Geometries
{
/// <summary>
/// A lightweight class used to store coordinates on the 2-dimensional Cartesian plane.
/// <para>
/// The base data object is suitable for use with coordinate sequences with
/// <c>dimension</c> = 2 and <c>measures</c> = 0.
/// </para>
/// </summary>
/// <remarks>
/// It is distinct from <see cref="Point"/>, which is a subclass of <see cref="Geometry"/>.
/// Unlike objects of type <see cref="Point"/> (which contain additional
/// information such as an envelope, a precision model, and spatial reference
/// system information), a <c>Coordinate</c> only contains ordinate values
/// and properties.
/// <para/>
/// Implementations may optionally support Z-ordinate and M-measure values
/// as appropriate for a <see cref="CoordinateSequence"/>. Use of <see cref="Z"/>
/// and <see cref="M"/> setters or <see cref="P:NetTopologySuite.Geometries.Coordinate.this[int]" /> indexer are recommended.
/// </remarks>
[Serializable]
#pragma warning disable 612,618
public class Coordinate : IComparable, IComparable<Coordinate>
{
///<summary>
/// The value used to indicate a null or missing ordinate value.
/// In particular, used for the value of ordinates for dimensions
/// greater than the defined dimension of a coordinate.
///</summary>
public const double NullOrdinate = double.NaN;
// Coordinate is auto-[Serializable], so replacing with auto properties could break compat.
#pragma warning disable IDE0032
private double _x;
private double _y;
#pragma warning restore IDE0032
/// <summary>
/// Gets or sets the X-ordinate value.
/// </summary>
public double X
{
get => _x;
set => _x = value;
}
/// <summary>
/// Gets or sets the Y-ordinate value.
/// </summary>
public double Y
{
get => _y;
set => _y = value;
}
/// <summary>
/// Gets or sets the Z-ordinate value, if supported.
/// If no Z value is present, returns <see cref="NullOrdinate"/>.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Thrown if an attempt is made to <b>set</b> the Z-ordinate value on an instance where
/// the Z-ordinate value is not supported.
/// </exception>
public virtual double Z
{
get => NullOrdinate;
set { throw new InvalidOperationException($"{GetType().Name} does not support setting Z-ordinate");}
}
/// <summary>
/// Gets or sets the value of the measure, if supported.
/// If no measure value is present, returns <see cref="NullOrdinate"/>.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Thrown if an attempt is made to <b>set</b> the measure value on an instance where
/// measures are not supported.
/// </exception>
public virtual double M
{
get => NullOrdinate;
set { throw new InvalidOperationException($"{GetType().Name} does not support setting M-measure"); }
}
/// <summary>
/// Constructs a <c>Coordinate</c> at (x,y).
/// </summary>
/// <param name="x">The X value</param>
/// <param name="y">The Y value</param>
public Coordinate(double x, double y)
{
X = x;
Y = y;
}
/// <summary>
/// Constructs a <c>Coordinate</c> at (0,0).
/// </summary>
public Coordinate() : this(0.0, 0.0) { }
/// <summary>
/// Constructs a <c>Coordinate</c> having the same (x,y,z) values as
/// <paramref name="c"/>.
/// </summary>
/// <param name="c"><c>Coordinate</c> to copy.</param>
public Coordinate(Coordinate c) : this(c.X, c.Y) { }
/// <summary>
/// Gets or sets the value for the given ordinate.
/// </summary>
/// <param name="ordinate">The ordinate.</param>
/// <returns>The ordinate value</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="ordinate"/> is not one of <see cref="Ordinate.X"/>, <see cref="Ordinate.Y"/>, <see cref="Ordinate.Z"/>, or <see cref="Ordinate.M"/>.</exception>
public virtual double this[Ordinate ordinate]
{
get
{
switch (ordinate)
{
case Ordinate.X:
return X;
case Ordinate.Y:
return Y;
case Ordinate.Z:
return Z;
case Ordinate.M:
return M;
default:
throw new ArgumentOutOfRangeException(nameof(ordinate), ordinate, "Coordinate instances only recognize X, Y, Z, and M ordinates.");
}
}
set
{
switch (ordinate)
{
case Ordinate.X:
X = value;
break;
case Ordinate.Y:
Y = value;
break;
case Ordinate.Z:
Z = value;
break;
case Ordinate.M:
M = value;
break;
default:
throw new ArgumentOutOfRangeException(nameof(ordinate), ordinate, "Coordinate instances only recognize X, Y, Z, and M ordinates.");
}
}
}
/// <summary>
/// Gets or sets the ordinate value for the given index.
/// </summary>
/// <remarks>
/// The base implementation supports 0 (X) and 1 (Y) as values for the index.
/// </remarks>
/// <param name="ordinateIndex">The ordinate index</param>
/// <returns>The ordinate value</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="ordinateIndex"/> is not in the valid range.</exception>
public virtual double this[int ordinateIndex]
{
get
{
switch (ordinateIndex)
{
case 0:
return X;
case 1:
return Y;
}
return double.NaN;
// disable for now to avoid regression issues
//throw new ArgumentOutOfRangeException(nameof(ordinateIndex));
}
set
{
switch (ordinateIndex)
{
case 0:
X = value;
return;
case 1:
Y = value;
return;
}
throw new ArgumentOutOfRangeException(nameof(ordinateIndex));
}
}
/// <summary>
/// Gets/Sets <c>Coordinate</c>s (x,y,z) values.
/// </summary>
public virtual Coordinate CoordinateValue
{
get
{
return this;
}
set
{
X = value.X;
Y = value.Y;
}
}
/// <summary>Gets a value indicating if the <c>Coordinate</c>
/// has valid x- and y ordinate values
/// <para/>
/// An ordinate value is valid if it is finite.
/// </summary>
/// <returns><c>true</c> if the coordinate is valid</returns>
/// <see cref="double.IsInfinity"/>
/// <see cref="double.IsNaN"/>
public bool IsValid
{
get
{
if (!/*double.*/IsFinite(X)) return false;
if (!/*double.*/IsFinite(Y)) return false;
return true;
}
}
/// <summary>
/// Predicate to check if a <see cref="double"/> value is finite.
/// <para/>
/// It is finite if both <see cref="double.IsInfinity"/> and <see cref="double.IsNaN"/> return <c>false</c>
/// </summary>
/// <param name="value">The value to test</param>
/// <returns><c>value</c></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsFinite(double value) => !double.IsInfinity(value) && !double.IsNaN(value);
/// <summary>
/// Returns whether the planar projections of the two <c>Coordinate</c>s are equal.
///</summary>
/// <param name="other"><c>Coordinate</c> with which to do the 2D comparison.</param>
/// <returns>
/// <c>true</c> if the x- and y-coordinates are equal;
/// the Z coordinates do not have to be equal.
/// </returns>
public bool Equals2D(Coordinate other)
{
return X == other.X && Y == other.Y;
}
/// <summary>
/// Tests if another Coordinate has the same values for the X and Y ordinates,
/// within a specified tolerance value. The Z ordinate is ignored.
/// </summary>
/// <param name="c">A <see cref="Coordinate"/>.</param>
/// <param name="tolerance">The tolerance value to use.</param>
/// <returns><c>true</c> if the X and Y ordinates are within the given tolerance.</returns>
/// <remarks>The Z ordinate is ignored.</remarks>
public bool Equals2D(Coordinate c, double tolerance)
{
if (!EqualsWithTolerance(X, c.X, tolerance))
return false;
if (!EqualsWithTolerance(Y, c.Y, tolerance))
return false;
return true;
}
protected static bool EqualsWithTolerance(double v1, double v2, double tolerance)
{
return Math.Abs(v1 - v2) <= tolerance;
}
/// <summary>
///
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool Equals(Coordinate other)
{
return Equals2D(other);
}
/// <summary>
/// Compares this object with the specified object for order.
/// Since Coordinates are 2.5D, this routine ignores the z value when making the comparison.
/// Returns
/// -1 : this.x < other.x || ((this.x == other.x) AND (this.y < other.y))
/// 0 : this.x == other.x AND this.y = other.y
/// 1 : this.x > other.x || ((this.x == other.x) AND (this.y > other.y))
/// </summary>
/// <param name="o"><c>Coordinate</c> with which this <c>Coordinate</c> is being compared.</param>
/// <returns>
/// A negative integer, zero, or a positive integer as this <c>Coordinate</c>
/// is less than, equal to, or greater than the specified <c>Coordinate</c>.
/// </returns>
public int CompareTo(object o)
{
//Like in JTS
//return CompareTo((Coordinate) o);
if (ReferenceEquals(o, null))
throw new ArgumentNullException(nameof(o));
if (!(o is Coordinate oc))
throw new ArgumentException($"Invalid type: '{o.GetType()}'", nameof(o));
return CompareTo(oc);
}
/// <summary>
/// Compares this object with the specified object for order.
/// Since Coordinates are 2.5D, this routine ignores the z value when making the comparison.
/// Returns
/// -1 : this.x < other.x || ((this.x == other.x) AND (this.y < other.y))
/// 0 : this.x == other.x AND this.y = other.y
/// 1 : this.x > other.x || ((this.x == other.x) AND (this.y > other.y))
/// </summary>
/// <param name="other"><c>Coordinate</c> with which this <c>Coordinate</c> is being compared.</param>
/// <returns>
/// A negative integer, zero, or a positive integer as this <c>Coordinate</c>
/// is less than, equal to, or greater than the specified <c>Coordinate</c>.
/// </returns>
public int CompareTo(Coordinate other)
{
if (other == null)
throw new ArgumentNullException(nameof(other));
if (X < other.X)
return -1;
if (X > other.X)
return 1;
if (Y < other.Y)
return -1;
return Y > other.Y ? 1 : 0;
}
/// <summary>
/// Create a copy of this <see cref="Coordinate"/>.
/// </summary>
/// <returns>A copy of this coordinate.</returns>
public virtual Coordinate Copy()
{
return (Coordinate)MemberwiseClone();
}
/// <summary>
/// Create a Coordinate of the same type as this Coordinate, using the provided values.
/// </summary>
/// <remarks>Depending on the actual type the following limitations are in place:
/// <list type="table">
/// <listheader><term>Coordinate (Sub-)Class</term><description>Limitation</description></listheader>
/// <item><term><c>Coordinate</c></term><description><paramref name="z"/>-parameter and <paramref name="m"/>-parameter are silently dropped.</description></item>
/// <item><term><c>CoordinateZ</c></term><description><paramref name="m"/>-parameter is silently dropped.</description></item>
/// <item><term><c>CoordinateM</c></term><description><paramref name="z"/>-parameter is silently dropped.</description></item>
/// <item><term><c>CoordinateZM</c></term><description>No parameter is dropped.</description></item>
/// </list>
/// </remarks>
/// <param name="x">The x-ordinate value, if not provided, it is <c>0d</c>.</param>
/// <param name="y">The y-ordinate value, if not provided, it is <c>0d</c>.</param>
/// <param name="z">The z-ordinate value, if not provided, it is <see cref="Coordinate.NullOrdinate"/>.</param>
/// <param name="m">The m-ordinate value, if not provided, it is <see cref="Coordinate.NullOrdinate"/>.</param>
/// <returns>A new <see cref="Coordinate"/></returns>
public virtual Coordinate Create(double x = 0, double y = 0, double z = NullOrdinate, double m = NullOrdinate) => new Coordinate(x, y);
/// <summary>
/// Computes the 2-dimensional Euclidean distance to another location.
/// </summary>
/// <param name="c">A <see cref="Coordinate"/> with which to do the distance comparison.</param>
/// <returns>the 2-dimensional Euclidean distance between the locations.</returns>
/// <remarks>The Z-ordinate is ignored.</remarks>
public double Distance(Coordinate c)
{
double dx = X - c.X;
double dy = Y - c.Y;
return Math.Sqrt(dx * dx + dy * dy);
}
#region System.Object overrides
/// <summary>
/// Returns <c>true</c> if <c>other</c> has the same values for the x and y ordinates.
/// Since Coordinates are 2.5D, this routine ignores the z value when making the comparison.
/// </summary>
/// <param name="o"><c>Coordinate</c> with which to do the comparison.</param>
/// <returns><c>true</c> if <c>other</c> is a <c>Coordinate</c> with the same values for the x and y ordinates.</returns>
public sealed override bool Equals(object o)
{
if (o is Coordinate other)
return Equals(other);
return false;
}
/// <summary>
/// Gets a hashcode for this coordinate.
/// </summary>
/// <returns>A hashcode for this coordinate.</returns>
public sealed override int GetHashCode()
{
int result = 17;
// ReSharper disable NonReadonlyFieldInGetHashCode
result = 37 * result + X.GetHashCode();
result = 37 * result + Y.GetHashCode();
// ReSharper restore NonReadonlyFieldInGetHashCode
return result;
}
/// <summary>
/// Returns a <c>string</c> of the form <I>(x,y,z)</I> .
/// </summary>
/// <returns><c>string</c> of the form <I>(x,y,z)</I></returns>
public override string ToString()
{
return string.Format(NumberFormatInfo.InvariantInfo, "({0:R}, {1:R})", X, Y);
}
#endregion
}
}