-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathGeoBoundingBox.cs
More file actions
227 lines (196 loc) · 6.56 KB
/
GeoBoundingBox.cs
File metadata and controls
227 lines (196 loc) · 6.56 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
using System;
using System.Text;
namespace Waher.Runtime.Geo
{
/// <summary>
/// Contains information about a geo-spatial bounding box using the Mercator Projection.
/// </summary>
public sealed class GeoBoundingBox : IGeoBoundingBox
{
private readonly GeoPosition min;
private readonly GeoPosition max;
private readonly string boxId;
private readonly bool includeMin;
private readonly bool includeMax;
/// <summary>
/// Contains information about a position in a geo-spatial coordinate system.
/// </summary>
/// <param name="Min">Lower-left corner of bounding box.</param>
/// <param name="Max">Upper-right corner of bounding box.</param>
public GeoBoundingBox(GeoPosition Min, GeoPosition Max)
: this(Min, Max, true, true)
{
}
/// <summary>
/// Contains information about a position in a geo-spatial coordinate system.
/// </summary>
/// <param name="Min">Lower-left corner of bounding box.</param>
/// <param name="Max">Upper-right corner of bounding box.</param>
/// <param name="IncludeMin">If the min latitude/longitude/altitude should be considered as included in the boundoing box.</param>
/// <param name="IncludeMax">If the max latitude/longitude/altitude should be considered as included in the boundoing box.</param>
public GeoBoundingBox(GeoPosition Min, GeoPosition Max, bool IncludeMin, bool IncludeMax)
: this(Guid.NewGuid().ToString(), Min, Max, IncludeMin, IncludeMax)
{
}
/// <summary>
/// Contains information about a position in a geo-spatial coordinate system.
/// </summary>
/// <param name="BoxId">The ID of the geo-spatial object.</param>
/// <param name="Min">Lower-left corner of bounding box.</param>
/// <param name="Max">Upper-right corner of bounding box.</param>
public GeoBoundingBox(string BoxId, GeoPosition Min, GeoPosition Max)
: this(BoxId, Min, Max, true, true)
{
}
/// <summary>
/// Contains information about a position in a geo-spatial coordinate system.
/// </summary>
/// <param name="BoxId">The ID of the geo-spatial object.</param>
/// <param name="Min">Lower-left corner of bounding box.</param>
/// <param name="Max">Upper-right corner of bounding box.</param>
/// <param name="IncludeMin">If the min latitude/longitude/altitude should be considered as included in the boundoing box.</param>
/// <param name="IncludeMax">If the max latitude/longitude/altitude should be considered as included in the boundoing box.</param>
public GeoBoundingBox(string BoxId, GeoPosition Min, GeoPosition Max, bool IncludeMin, bool IncludeMax)
{
this.boxId = BoxId;
this.min = Min;
this.max = Max;
this.includeMin = IncludeMin;
this.includeMax = IncludeMax;
}
/// <summary>
/// The ID of the geo-spatial bounding box.
/// </summary>
public string BoxId => this.boxId;
/// <summary>
/// Lower-left corner of bounding box.
/// </summary>
public GeoPosition Min => this.min;
/// <summary>
/// Upper-right corner of bounding box.
/// </summary>
public GeoPosition Max => this.max;
/// <summary>
/// If the min latitude/longitude/altitude should be considered as included in the boundoing box.
/// </summary>
public bool IncludeMin => this.includeMin;
/// <summary>
/// If the max latitude/longitude/altitude should be considered as included in the boundoing box.
/// </summary>
public bool IncludeMax => this.includeMax;
/// <summary>
/// If the bounding box is longitude-wrapped, i.e. if the box passes the +-180 degrees
/// longitude.
/// </summary>
public bool LongitudeWrapped
{
get
{
return
!(this.min is null) &&
!(this.max is null) &&
this.min.Longitude > this.max.Longitude;
}
}
/// <inheritdoc/>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (this.includeMin)
sb.Append('[');
else
sb.Append('(');
sb.Append(this.min?.ToString());
sb.Append(" - ");
sb.Append(this.max?.ToString());
if (this.includeMax)
sb.Append(']');
else
sb.Append(')');
return sb.ToString();
}
/// <inheritdoc/>
public string ToNormalizedString()
{
StringBuilder sb = new StringBuilder();
if (this.includeMin)
sb.Append('[');
else
sb.Append('(');
sb.Append(this.min?.NormalizedValue);
sb.Append(" - ");
sb.Append(this.max?.NormalizedValue);
if (this.includeMax)
sb.Append(']');
else
sb.Append(')');
return sb.ToString();
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
return
obj is GeoBoundingBox Obj &&
(this.min?.Equals(Obj.Min) ?? Obj.Min is null) &&
(this.max?.Equals(Obj.Max) ?? Obj.Max is null) &&
this.includeMin == Obj.includeMin &&
this.includeMax == Obj.includeMax;
}
/// <inheritdoc/>
public override int GetHashCode()
{
int Result = this.min?.GetHashCode() ?? 0;
Result ^= Result << 5 ^ (this.max?.GetHashCode() ?? 0);
Result ^= Result << 5 ^ this.includeMin.GetHashCode();
Result ^= Result << 5 ^ this.includeMax.GetHashCode();
return Result;
}
/// <summary>
/// Checks if two geo-spatial bounding boxes are equal.
/// </summary>
/// <param name="A">Geo-position A</param>
/// <param name="B">Geo-position B</param>
/// <returns>If they are equal</returns>
public static bool operator ==(GeoBoundingBox A, GeoBoundingBox B)
{
if (A is null ^ B is null)
return false;
if (A is null)
return true;
return A.Equals(B);
}
/// <summary>
/// Checks if two geo-spatial bounding boxes are not equal.
/// </summary>
/// <param name="A">Geo-position A</param>
/// <param name="B">Geo-position B</param>
/// <returns>If they are equal</returns>
public static bool operator !=(GeoBoundingBox A, GeoBoundingBox B)
{
if (A is null ^ B is null)
return true;
if (A is null)
return false;
return !A.Equals(B);
}
/// <summary>
/// If the bounding box contains a location.
/// </summary>
/// <param name="Location">Location</param>
/// <returns>If the bounding box contains the location.</returns>
public bool Contains(GeoPosition Location)
{
return !Location.LiesOutside(this.min, this.max, !this.includeMin, !this.includeMax);
}
/// <summary>
/// If the bounding box contains a location.
/// </summary>
/// <param name="Location">Location</param>
/// <param name="IgnoreAltitude">If altitude component should be ignored.</param>
/// <returns>If the bounding box contains the location.</returns>
public bool Contains(GeoPosition Location, bool IgnoreAltitude)
{
return !Location.LiesOutside(this.min, this.max, !this.includeMin, !this.includeMax, IgnoreAltitude);
}
}
}