/
GeoLocation.cs
189 lines (164 loc) · 6.08 KB
/
GeoLocation.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
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
using System;
using System.Globalization;
using System.Runtime.Serialization;
using OpenSearch.Net.Utf8Json;
/*
* Taken from SolrNet https://github.com/mausch/SolrNet/blob/master/SolrNet/Location.cs
*/
namespace OpenSearch.Client
{
/// <summary>
/// Represents a Latitude/Longitude as a 2 dimensional point that gets serialized as { lat, lon }
/// </summary>
[JsonFormatter(typeof(GeoLocationFormatter))]
public class GeoLocation : IEquatable<GeoLocation>, IFormattable
{
/// <summary>
/// Represents a Latitude/Longitude as a 2 dimensional point.
/// </summary>
public GeoLocation(double latitude, double longitude)
{
Latitude = latitude;
Longitude = longitude;
}
/// <summary>
/// Latitude
/// </summary>
[DataMember(Name = "lat")]
public double Latitude { get; }
/// <summary>
/// Longitude
/// </summary>
[DataMember(Name = "lon")]
public double Longitude { get; }
[IgnoreDataMember]
internal GeoFormat Format { get; set; }
public bool Equals(GeoLocation other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return Latitude.Equals(other.Latitude) && Longitude.Equals(other.Longitude);
}
public string ToString(string format, IFormatProvider formatProvider) => ToString();
/// <summary>
/// Checks if <paramref name="latitude" /> is a valid latitude between -90 and 90, inclusive.
/// </summary>
/// <param name="latitude"></param>
/// <returns></returns>
public static bool IsValidLatitude(double latitude) => latitude >= -90 && latitude <= 90;
/// <summary>
/// Checks if <paramref name="longitude" /> is a valid longitude between -180 and 180, inclusive.
/// </summary>
/// <param name="longitude"></param>
/// <returns></returns>
public static bool IsValidLongitude(double longitude) => longitude >= -180 && longitude <= 180;
/// <summary>
/// Try to create a <see cref="GeoLocation" />.
/// </summary>
/// <param name="latitude"></param>
/// <param name="longitude"></param>
/// <returns></returns>
public static GeoLocation TryCreate(double latitude, double longitude) => new GeoLocation(latitude, longitude);
public override string ToString() =>
Latitude.ToString("#0.0#######", CultureInfo.InvariantCulture) + "," +
Longitude.ToString("#0.0#######", CultureInfo.InvariantCulture);
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != GetType())
return false;
return Equals((GeoLocation)obj);
}
public override int GetHashCode() =>
unchecked((Latitude.GetHashCode() * 397) ^ Longitude.GetHashCode());
public static implicit operator GeoLocation(string latLon)
{
if (string.IsNullOrEmpty(latLon))
throw new ArgumentNullException(nameof(latLon));
var parts = latLon.Split(',');
if (parts.Length != 2) throw new ArgumentException("Invalid format: string must be in the form of lat,lon");
if (!double.TryParse(parts[0], NumberStyles.Any, CultureInfo.InvariantCulture, out var lat))
throw new ArgumentException("Invalid latitude value");
if (!double.TryParse(parts[1], NumberStyles.Any, CultureInfo.InvariantCulture, out var lon))
throw new ArgumentException("Invalid longitude value");
return new GeoLocation(lat, lon);
}
public static implicit operator GeoLocation(double[] lonLat) => lonLat.Length != 2
? null
: new GeoLocation(lonLat[1], lonLat[0]);
}
/// <summary>
/// Represents a Latitude/Longitude and optional Z value as a 2 or 3 dimensional point
/// that gets serialized as new [] { lon, lat, [z] }
/// </summary>
[JsonFormatter(typeof(GeoCoordinateFormatter))]
public class GeoCoordinate : GeoLocation
{
/// <summary>
/// Creates a new instance of <see cref="GeoCoordinate" />
/// </summary>
public GeoCoordinate(double latitude, double longitude) : base(latitude, longitude) { }
/// <summary>
/// Creates a new instance of <see cref="GeoCoordinate" />
/// </summary>
public GeoCoordinate(double latitude, double longitude, double z) : base(latitude, longitude) =>
Z = z;
/// <summary>
/// Gets or sets the Z value
/// </summary>
public double? Z { get; set; }
/// <summary>
/// Creates a new instance of <see cref="GeoCoordinate" /> from an array
/// of 2 or 3 doubles, in the order Latitude, Longitude, and optional Z value.
/// Returns null if coordinates are null
/// <exception cref="ArgumentOutOfRangeException">If the array does not contain 2 or 3 values</exception>
/// </summary>
public static implicit operator GeoCoordinate(double[] coordinates)
{
if (coordinates == null) return null;
switch (coordinates.Length)
{
case 2:
return new GeoCoordinate(coordinates[0], coordinates[1]);
case 3:
return new GeoCoordinate(coordinates[0], coordinates[1], coordinates[2]);
default:
throw new ArgumentOutOfRangeException(
nameof(coordinates),
$"Cannot create a {nameof(GeoCoordinate)} from an array that does not contain 2 or 3 values");
}
}
}
}