Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GeoExtensions.cs
1using System.Text.RegularExpressions;
2
3namespace Waher.Runtime.Geo
4{
8 public static class GeoExtensions
9 {
19 public static bool LiesOutside(this GeoPosition Position, GeoPosition Min, GeoPosition Max)
20 {
21 return Position.LiesOutside(Min, Max, false, false, false);
22 }
23
35 public static bool LiesOutside(this GeoPosition Position, GeoPosition Min, GeoPosition Max,
36 bool IncludeMin, bool IncludeMax)
37 {
38 return Position.LiesOutside(Min, Max, IncludeMin, IncludeMax, false);
39 }
40
53 public static bool LiesOutside(this GeoPosition Position, GeoPosition Min, GeoPosition Max,
54 bool IncludeMin, bool IncludeMax, bool IgnoreAltitude)
55 {
56 if (Position.Latitude.OutOfRange(Min?.Latitude, Max?.Latitude, IncludeMin, IncludeMax))
57 return true;
58
59 if (!(Min is null) && !(Max is null) && Min.Longitude > Max.Longitude)
60 {
61 if (!Position.Longitude.OutOfRange(Max.Longitude, Min.Longitude, !IncludeMin, !IncludeMax))
62 return true;
63 }
64 else
65 {
66 if (Position.Longitude.OutOfRange(Min?.Longitude, Max?.Longitude, IncludeMin, IncludeMax))
67 return true;
68 }
69
70 if (IgnoreAltitude)
71 return false;
72
73 if (Position.Altitude.HasValue &&
74 Position.Altitude.Value.OutOfRange(Min?.Altitude, Max?.Altitude, IncludeMin, IncludeMax))
75 {
76 return true;
77 }
78
79 return false;
80 }
81
82 private static bool OutOfRange(this double Value, double? Min, double? Max,
83 bool IncludeMin, bool IncludeMax)
84 {
85 int MinComp = Min.HasValue ? Value.CompareTo(Min.Value) : 1;
86 int MaxComp = Max.HasValue ? Value.CompareTo(Max.Value) : -1;
87
88 if (MinComp < 0 || IncludeMin && MinComp == 0)
89 return true;
90
91 if (MaxComp > 0 || IncludeMax && MaxComp == 0)
92 return true;
93
94 return false;
95 }
96
97 private static bool InRange(this double Value, double? Min, double? Max,
98 bool IncludeMin, bool IncludeMax)
99 {
100 int MinComp = Min.HasValue ? Value.CompareTo(Min.Value) : 1;
101 int MaxComp = Max.HasValue ? Value.CompareTo(Max.Value) : -1;
102
103 if (MinComp < 0 || (!IncludeMin && MinComp == 0))
104 return false;
105
106 if (MaxComp > 0 || (!IncludeMax && MaxComp == 0))
107 return false;
108
109 return true;
110 }
111
121 public static bool NorthOf(this GeoPosition Position, GeoPosition Reference, bool IncludeReference)
122 {
123 int i = Position.Latitude.CompareTo(Reference.Latitude);
124 return i > 0 || (IncludeReference && i == 0);
125 }
126
136 public static bool SouthOf(this GeoPosition Position, GeoPosition Reference, bool IncludeReference)
137 {
138 int i = Position.Latitude.CompareTo(Reference.Latitude);
139 return i < 0 || (IncludeReference && i == 0);
140 }
141
151 public static bool EastOf(this GeoPosition Position, GeoPosition Reference, bool IncludeReference)
152 {
153 int i = Position.Longitude.CompareTo(Reference.Longitude);
154 return i > 0 || (IncludeReference && i == 0);
155 }
156
166 public static bool WestOf(this GeoPosition Position, GeoPosition Reference, bool IncludeReference)
167 {
168 int i = Position.Longitude.CompareTo(Reference.Longitude);
169 return i < 0 || (IncludeReference && i == 0);
170 }
171
179 public static bool NorthOf(this GeoPosition Position, IGeoBoundingBox Reference)
180 {
181 return Position.NorthOf(Reference.Max, !Reference.IncludeMax);
182 }
183
191 public static bool SouthOf(this GeoPosition Position, IGeoBoundingBox Reference)
192 {
193 return Position.SouthOf(Reference.Min, !Reference.IncludeMin);
194 }
195
203 public static bool EastOf(this GeoPosition Position, IGeoBoundingBox Reference)
204 {
205 if (Reference.LongitudeWrapped)
206 return Position.EastOf(Reference.Min, !Reference.IncludeMin);
207 else
208 return Position.EastOf(Reference.Max, !Reference.IncludeMax);
209 }
210
218 public static bool WestOf(this GeoPosition Position, IGeoBoundingBox Reference)
219 {
220 if (Reference.LongitudeWrapped)
221 return Position.WestOf(Reference.Max, !Reference.IncludeMax);
222 else
223 return Position.WestOf(Reference.Min, !Reference.IncludeMin);
224 }
225
233 public static bool AltitudeCheck(this GeoPosition Position, IGeoBoundingBox Reference)
234 {
235 if (Position.Altitude.HasValue)
236 {
237 return Position.Altitude.Value.InRange(Reference.Min?.Altitude, Reference.Max?.Altitude,
238 Reference.IncludeMin, Reference.IncludeMax);
239 }
240
241 return true;
242 }
243
250 public static bool GeoIdPatternCheck(this string GeoId, Regex Pattern)
251 {
252 Match M = Pattern.Match(GeoId);
253
254 return M.Success && M.Index == 0 && M.Length == GeoId.Length;
255 }
256 }
257}
Extension methods for geo-spatial calculations.
Definition: GeoExtensions.cs:9
static bool EastOf(this GeoPosition Position, IGeoBoundingBox Reference)
If a Position is east of a bounding box Reference .
static bool SouthOf(this GeoPosition Position, GeoPosition Reference, bool IncludeReference)
If a Position is south of a reference point Reference . The reference point itself will be included ...
static bool LiesOutside(this GeoPosition Position, GeoPosition Min, GeoPosition Max, bool IncludeMin, bool IncludeMax)
Checks if a position lies outside a given bounding box, using the Mercator projection....
static bool LiesOutside(this GeoPosition Position, GeoPosition Min, GeoPosition Max, bool IncludeMin, bool IncludeMax, bool IgnoreAltitude)
Checks if a position lies outside a given bounding box, using the Mercator projection....
static bool EastOf(this GeoPosition Position, GeoPosition Reference, bool IncludeReference)
If a Position is east of a reference point Reference . The reference point itself will be included i...
static bool LiesOutside(this GeoPosition Position, GeoPosition Min, GeoPosition Max)
Checks if a position lies outside a given bounding box, using the Mercator projection....
static bool SouthOf(this GeoPosition Position, IGeoBoundingBox Reference)
If a Position is south of a bounding box Reference .
static bool NorthOf(this GeoPosition Position, IGeoBoundingBox Reference)
If a Position is north of a bounding box Reference .
static bool WestOf(this GeoPosition Position, GeoPosition Reference, bool IncludeReference)
If a Position is west of a reference point Reference . The reference point itself will be included i...
static bool AltitudeCheck(this GeoPosition Position, IGeoBoundingBox Reference)
If the altitude defined by Position (if any) lies within the scope of the bounding box Reference (i...
static bool GeoIdPatternCheck(this string GeoId, Regex Pattern)
If the Geo-spatial ID matches a given pattern, defined as a regular expression.
static bool WestOf(this GeoPosition Position, IGeoBoundingBox Reference)
If a Position is west of a bounding box Reference .
static bool NorthOf(this GeoPosition Position, GeoPosition Reference, bool IncludeReference)
If a Position is north of a reference point Reference . The reference point itself will be included ...
Contains information about a position in a geo-spatial coordinate system.
Definition: GeoPosition.cs:17
double Longitude
Longitude in degrees.
Definition: GeoPosition.cs:85
double? Altitude
Altitude in meters. Can be null.
Definition: GeoPosition.cs:100
double Latitude
Latitude in degrees.
Definition: GeoPosition.cs:70
Interface for a geo-spatial bounding box using the Mercator Projection.
bool IncludeMax
If the max latitude/longitude/altitude should be considered as included in the boundoing box.
bool LongitudeWrapped
If the bounding box is longitude-wrapped, i.e. if the box passes the +-180 degrees longitude.
GeoPosition Max
Upper-right corner of bounding box.
bool IncludeMin
If the min latitude/longitude/altitude should be considered as included in the boundoing box.
GeoPosition Min
Lower-left corner of bounding box.