forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgisTypes.cs
More file actions
557 lines (444 loc) · 18.7 KB
/
PostgisTypes.cs
File metadata and controls
557 lines (444 loc) · 18.7 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
#pragma warning disable CA1710
// ReSharper disable once CheckNamespace
namespace Npgsql.LegacyPostgis
{
#pragma warning disable 1591
/// <summary>
/// Represents the identifier of the Well Known Binary representation of a geographical feature specified by the OGC.
/// http://portal.opengeospatial.org/files/?artifact_id=13227 Chapter 6.3.2.7
/// </summary>
enum WkbIdentifier : uint
{
Point = 1,
LineString = 2,
Polygon = 3,
MultiPoint = 4,
MultiLineString = 5,
MultiPolygon = 6,
GeometryCollection = 7
}
/// <summary>
/// The modifiers used by postgis to extend the geomtry's binary representation
/// </summary>
[Flags]
enum EwkbModifiers : uint
{
HasSRID = 0x20000000,
HasMDim = 0x40000000,
HasZDim = 0x80000000
}
/// <summary>
/// A structure representing a 2D double precision floating point coordinate;
/// </summary>
public struct Coordinate2D : IEquatable<Coordinate2D>
{
/// <summary>
/// X coordinate.
/// </summary>
public double X { get; }
/// <summary>
/// Y coordinate.
/// </summary>
public double Y { get; }
/// <summary>
/// Generates a new BBpoint with the specified coordinates.
/// </summary>
/// <param name="x">X coordinate</param>
/// <param name="y">Y coordinate</param>
public Coordinate2D(double x, double y) { X = x; Y = y;}
// ReSharper disable CompareOfFloatsByEqualityOperator
public bool Equals(Coordinate2D c)
=> X == c.X && Y == c.Y;
// ReSharper restore CompareOfFloatsByEqualityOperator
public override int GetHashCode()
=> X.GetHashCode() ^ Util.RotateShift(Y.GetHashCode(), Util.BitsInInt / 2);
#nullable disable
public override bool Equals(object obj) => obj is Coordinate2D coord && Equals(coord);
#nullable enable
public static bool operator ==(Coordinate2D left, Coordinate2D right)
=> Equals(left, right);
public static bool operator !=(Coordinate2D left, Coordinate2D right)
=> !Equals(left, right);
}
/// <summary>
/// Represents an Postgis feature.
/// </summary>
public abstract class PostgisGeometry
{
/// <summary>
/// returns the binary length of the data structure without header.
/// </summary>
/// <returns></returns>
protected abstract int GetLenHelper();
internal abstract WkbIdentifier Identifier { get;}
internal int GetLen(bool includeSRID)
{
// header =
// 1 byte for the endianness of the structure
// + 4 bytes for the type identifier
// (+ 4 bytes for the SRID if present and included)
return 5 + (SRID == 0 || !includeSRID ? 0 : 4) + GetLenHelper();
}
/// <summary>
/// The Spatial Reference System Identifier of the geometry (0 if unspecified).
/// </summary>
public uint SRID { get; set; }
}
/// <summary>
/// Represents an Postgis 2D Point
/// </summary>
public class PostgisPoint : PostgisGeometry, IEquatable<PostgisPoint>
{
Coordinate2D _coord;
internal override WkbIdentifier Identifier => WkbIdentifier.Point;
protected override int GetLenHelper() => 16;
public PostgisPoint(double x, double y)
{
_coord = new Coordinate2D(x, y);
}
public double X => _coord.X;
public double Y => _coord.Y;
#nullable disable
public bool Equals(PostgisPoint other)
=> !(other is null) && _coord.Equals(other._coord);
public override bool Equals(object obj) => Equals(obj as PostgisPoint);
#nullable enable
public static bool operator ==(PostgisPoint x, PostgisPoint y)
=> x is null ? y is null : x.Equals(y);
public static bool operator !=(PostgisPoint x, PostgisPoint y) => !(x == y);
public override int GetHashCode() => X.GetHashCode() ^ Util.RotateShift(Y.GetHashCode(), Util.BitsInInt / 2);
}
/// <summary>
/// Represents an Ogc 2D LineString
/// </summary>
public class PostgisLineString : PostgisGeometry, IEquatable<PostgisLineString>, IEnumerable<Coordinate2D>
{
readonly Coordinate2D[] _points;
internal override WkbIdentifier Identifier => WkbIdentifier.LineString;
protected override int GetLenHelper() => 4 + _points.Length * 16;
public IEnumerator<Coordinate2D> GetEnumerator()
=> ((IEnumerable<Coordinate2D>)_points).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public Coordinate2D this[int index] => _points[index];
public PostgisLineString(IEnumerable<Coordinate2D> points)
{
_points = points.ToArray();
}
public PostgisLineString(Coordinate2D[] points)
{
_points = points;
}
public int PointCount => _points.Length;
#nullable disable
public bool Equals(PostgisLineString other)
{
if (ReferenceEquals(other , null))
return false ;
if (_points.Length != other._points.Length)
return false;
for (var i = 0; i < _points.Length; i++)
if (!_points[i].Equals(other._points[i]))
return false;
return true;
}
public override bool Equals(object obj) => Equals(obj as PostgisLineString);
public static bool operator ==(PostgisLineString x, PostgisLineString y)
=> x is null ? y is null : x.Equals(y);
#nullable enable
public static bool operator !=(PostgisLineString x, PostgisLineString y) => !(x == y);
public override int GetHashCode()
{
var ret = 266370105;//seed with something other than zero to make paths of all zeros hash differently.
foreach (var t in _points)
ret ^= Util.RotateShift(t.GetHashCode(), ret % Util.BitsInInt);
return ret;
}
}
/// <summary>
/// Represents an Postgis 2D Polygon.
/// </summary>
public class PostgisPolygon : PostgisGeometry, IEquatable<PostgisPolygon>, IEnumerable<IEnumerable<Coordinate2D>>
{
readonly Coordinate2D[][] _rings;
internal override WkbIdentifier Identifier => WkbIdentifier.Polygon;
protected override int GetLenHelper() => 4 + _rings.Length * 4 + TotalPointCount * 16;
public Coordinate2D this[int ringIndex, int pointIndex] => _rings[ringIndex][pointIndex];
public Coordinate2D[] this[int ringIndex] => _rings[ringIndex];
public PostgisPolygon(Coordinate2D[][] rings)
{
_rings = rings;
}
public PostgisPolygon(IEnumerable<IEnumerable<Coordinate2D>> rings)
{
_rings = rings.Select(x => x.ToArray()).ToArray();
}
public IEnumerator<IEnumerable<Coordinate2D>> GetEnumerator()
=> ((IEnumerable<IEnumerable<Coordinate2D>>)_rings).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#nullable disable
public bool Equals(PostgisPolygon other)
{
if (other is null)
return false;
if (_rings.Length != other._rings.Length)
return false;
for (var i = 0; i < _rings.Length; i++)
{
if (_rings[i].Length != other._rings[i].Length)
return false;
for (var j = 0; j < _rings[i].Length; j++)
if (!_rings[i][j].Equals (other._rings[i][j]))
return false;
}
return true;
}
public override bool Equals(object obj)
=> Equals(obj as PostgisPolygon);
public static bool operator ==(PostgisPolygon x, PostgisPolygon y)
=> x is null ? y is null : x.Equals(y);
#nullable enable
public static bool operator !=(PostgisPolygon x, PostgisPolygon y) => !(x == y);
public int RingCount => _rings.Length;
public int TotalPointCount => _rings.Sum(r => r.Length);
public override int GetHashCode()
{
var ret = 266370105;//seed with something other than zero to make paths of all zeros hash differently.
for (var i = 0; i < _rings.Length; i++)
for (var j = 0; j < _rings[i].Length; j++)
ret ^= Util.RotateShift(_rings[i][j].GetHashCode(), ret % Util.BitsInInt);
return ret;
}
}
/// <summary>
/// Represents a Postgis 2D MultiPoint
/// </summary>
public class PostgisMultiPoint : PostgisGeometry, IEquatable<PostgisMultiPoint>, IEnumerable<Coordinate2D>
{
readonly Coordinate2D[] _points;
internal override WkbIdentifier Identifier => WkbIdentifier.MultiPoint;
//each point of a multipoint is a postgispoint, not a building block point.
protected override int GetLenHelper() => 4 + _points.Length * 21;
public IEnumerator<Coordinate2D> GetEnumerator() => ((IEnumerable<Coordinate2D>)_points).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public PostgisMultiPoint (Coordinate2D[] points)
{
_points = points;
}
public PostgisMultiPoint(IEnumerable<PostgisPoint> points)
{
_points = points.Select(x => new Coordinate2D(x.X, x.Y)).ToArray();
}
public PostgisMultiPoint(IEnumerable<Coordinate2D> points)
{
_points = points.ToArray();
}
public Coordinate2D this[int indexer] => _points[indexer];
#nullable disable
public bool Equals(PostgisMultiPoint other)
{
if (ReferenceEquals(other ,null))
return false ;
if (_points.Length != other._points.Length)
return false;
for (var i = 0; i < _points.Length; i++)
if (!_points[i].Equals(other._points[i]))
return false;
return true;
}
public override bool Equals(object obj) => Equals(obj as PostgisMultiPoint);
public static bool operator ==(PostgisMultiPoint x, PostgisMultiPoint y)
=> x is null ? y is null : x.Equals(y);
#nullable enable
public static bool operator !=(PostgisMultiPoint x, PostgisMultiPoint y) => !(x == y);
public override int GetHashCode()
{
var ret = 266370105;//seed with something other than zero to make paths of all zeros hash differently.
for (var i = 0; i < _points.Length; i++)
ret ^= Util.RotateShift(_points[i].GetHashCode(), ret % Util.BitsInInt);
return ret;
}
public int PointCount => _points.Length;
}
/// <summary>
/// Represents a Postgis 2D MultiLineString
/// </summary>
public sealed class PostgisMultiLineString : PostgisGeometry,
IEquatable<PostgisMultiLineString>, IEnumerable<PostgisLineString>
{
readonly PostgisLineString[] _lineStrings;
internal PostgisMultiLineString(Coordinate2D[][] pointArray)
{
_lineStrings = new PostgisLineString[pointArray.Length];
for (var i = 0; i < pointArray.Length; i++)
_lineStrings[i] = new PostgisLineString(pointArray[i]);
}
internal override WkbIdentifier Identifier => WkbIdentifier.MultiLineString;
protected override int GetLenHelper()
{
var n = 4;
for (var i = 0; i < _lineStrings.Length; i++)
n += _lineStrings[i].GetLen(false);
return n;
}
public IEnumerator<PostgisLineString> GetEnumerator() => ((IEnumerable<PostgisLineString>)_lineStrings).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public PostgisMultiLineString(PostgisLineString[] linestrings)
{
_lineStrings = linestrings;
}
public PostgisMultiLineString(IEnumerable<PostgisLineString> linestrings)
{
_lineStrings = linestrings.ToArray();
}
public PostgisLineString this[int index] => _lineStrings[index];
public PostgisMultiLineString(IEnumerable<IEnumerable<Coordinate2D>> pointList)
{
_lineStrings = pointList.Select(x => new PostgisLineString(x)).ToArray();
}
#nullable disable
public bool Equals(PostgisMultiLineString other)
{
if (other is null)
return false ;
if (_lineStrings.Length != other._lineStrings.Length) return false;
for (var i = 0; i < _lineStrings.Length; i++)
{
if (_lineStrings[i] != other._lineStrings[i]) return false;
}
return true;
}
public override bool Equals(object obj)
=> obj is PostgisMultiLineString multiLineString && Equals(multiLineString);
public static bool operator ==(PostgisMultiLineString x, PostgisMultiLineString y)
=> x is null ? y is null : x.Equals(y);
#nullable enable
public static bool operator !=(PostgisMultiLineString x, PostgisMultiLineString y) => !(x == y);
public override int GetHashCode()
{
var ret = 266370105;//seed with something other than zero to make paths of all zeros hash differently.
for (var i = 0; i < _lineStrings.Length; i++)
ret ^= Util.RotateShift(_lineStrings[i].GetHashCode(), ret % Util.BitsInInt);
return ret;
}
public int LineCount => _lineStrings.Length;
}
/// <summary>
/// Represents a Postgis 2D MultiPolygon.
/// </summary>
public class PostgisMultiPolygon : PostgisGeometry, IEquatable<PostgisMultiPolygon>, IEnumerable<PostgisPolygon>
{
readonly PostgisPolygon[] _polygons;
public IEnumerator<PostgisPolygon> GetEnumerator() => ((IEnumerable<PostgisPolygon>)_polygons).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
internal override WkbIdentifier Identifier => WkbIdentifier.MultiPolygon;
public PostgisPolygon this[int index] => _polygons[index];
public PostgisMultiPolygon(PostgisPolygon[] polygons)
{
_polygons = polygons;
}
public PostgisMultiPolygon(IEnumerable<PostgisPolygon> polygons)
{
_polygons = polygons.ToArray();
}
public PostgisMultiPolygon(IEnumerable<IEnumerable<IEnumerable<Coordinate2D>>> ringList)
{
_polygons = ringList.Select(x => new PostgisPolygon(x)).ToArray();
}
#nullable disable
public bool Equals(PostgisMultiPolygon other)
{
if (other is null)
return false;
if (_polygons.Length != other._polygons.Length)
return false;
for (var i = 0; i < _polygons.Length; i++)
if (_polygons[i] != other._polygons[i]) return false;
return true;
}
public override bool Equals(object obj)
=> obj is PostgisMultiPolygon polygon && Equals(polygon);
#nullable enable
public static bool operator ==(PostgisMultiPolygon x, PostgisMultiPolygon y)
=> x is null ? y is null : x.Equals(y);
public static bool operator !=(PostgisMultiPolygon x, PostgisMultiPolygon y) => !(x == y);
public override int GetHashCode()
{
var ret = 266370105;//seed with something other than zero to make paths of all zeros hash differently.
for (var i = 0; i < _polygons.Length; i++)
ret ^= Util.RotateShift(_polygons[i].GetHashCode(), ret % Util.BitsInInt);
return ret;
}
protected override int GetLenHelper()
{
var n = 4;
for (var i = 0; i < _polygons.Length; i++)
n += _polygons[i].GetLen(false);
return n;
}
public int PolygonCount => _polygons.Length;
}
/// <summary>
/// Represents a collection of Postgis feature.
/// </summary>
public class PostgisGeometryCollection : PostgisGeometry, IEquatable<PostgisGeometryCollection>, IEnumerable<PostgisGeometry>
{
readonly PostgisGeometry[] _geometries;
public PostgisGeometry this[int index] => _geometries[index];
internal override WkbIdentifier Identifier => WkbIdentifier.GeometryCollection;
public IEnumerator<PostgisGeometry> GetEnumerator() => ((IEnumerable<PostgisGeometry>)_geometries).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public PostgisGeometryCollection(PostgisGeometry[] geometries)
{
_geometries = geometries;
}
public PostgisGeometryCollection(IEnumerable<PostgisGeometry> geometries)
{
_geometries = geometries.ToArray();
}
#nullable disable
public bool Equals(PostgisGeometryCollection other)
{
if (other is null)
return false;
if (_geometries.Length != other._geometries.Length)
return false;
for (var i = 0; i < _geometries.Length; i++)
if (!_geometries[i].Equals(other._geometries[i]))
return false;
return true;
}
public override bool Equals(object obj)
=> obj is PostgisGeometryCollection collection && Equals(collection);
public static bool operator ==(PostgisGeometryCollection x, PostgisGeometryCollection y)
=> x is null ? y is null : x.Equals(y);
#nullable enable
public static bool operator !=(PostgisGeometryCollection x, PostgisGeometryCollection y) => !(x == y);
public override int GetHashCode()
{
var ret = 266370105;//seed with something other than zero to make paths of all zeros hash differently.
for (var i = 0; i < _geometries.Length; i++)
ret ^= Util.RotateShift(_geometries[i].GetHashCode(), ret % Util.BitsInInt);
return ret;
}
protected override int GetLenHelper()
{
var n = 4;
for (var i = 0; i < _geometries.Length; i++)
n += _geometries[i].GetLen(true);
return n;
}
public int GeometryCount => _geometries.Length;
}
static class Util
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int RotateShift(int val, int shift)
=> (val << shift) | (val >> (BitsInInt - shift));
internal const int BitsInInt = sizeof(int) * 8;
}
}