-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
GeoNearPoint.cs
161 lines (147 loc) · 5.61 KB
/
GeoNearPoint.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
/* Copyright 2010-present MongoDB Inc.
*
* Licensed 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 MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.GeoJsonObjectModel;
using MongoDB.Driver.GeoJsonObjectModel.Serializers;
namespace MongoDB.Driver
{
/// <summary>
/// Represents a GeoNearPoint (wraps either an XYPoint or a GeoJsonPoint).
/// </summary>
public abstract class GeoNearPoint
{
// implicit conversions
/// <summary>
/// Implicit conversion to wrap an XYPoint in a GeoNearPoint value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A GeoNearPoint value.</returns>
public static implicit operator GeoNearPoint(XYPoint value)
{
return new Legacy(value);
}
/// <summary>
/// Implicit conversion to wrap a 2D GeoJson point in a GeoNearPoint value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A GeoNearPoint value.</returns>
public static implicit operator GeoNearPoint(GeoJsonPoint<GeoJson2DCoordinates> value)
{
return new GeoJson<GeoJson2DCoordinates>(value);
}
/// <summary>
/// Implicit conversion to wrap a 2D GeoJson point in a GeoNearPoint value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A GeoNearPoint value.</returns>
public static implicit operator GeoNearPoint(GeoJsonPoint<GeoJson2DGeographicCoordinates> value)
{
return new GeoJson<GeoJson2DGeographicCoordinates>(value);
}
/// <summary>
/// Implicit conversion to wrap a 2D GeoJson point in a GeoNearPoint value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A GeoNearPoint value.</returns>
public static implicit operator GeoNearPoint(GeoJsonPoint<GeoJson2DProjectedCoordinates> value)
{
return new GeoJson<GeoJson2DProjectedCoordinates>(value);
}
// internal methods
/// <summary>
/// Converts the GeoNearPoint into a BsonValue for the GeoNear command.
/// </summary>
/// <returns>A BsonValue.</returns>
internal abstract BsonValue ToGeoNearCommandValue();
// nested classes
/// <summary>
/// Represents a GeoNearPoint that wraps an XYPoint.
/// </summary>
public class Legacy : GeoNearPoint
{
// private fields
private readonly XYPoint _value;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="Legacy"/> class.
/// </summary>
/// <param name="value">The value.</param>
public Legacy(XYPoint value)
{
_value = value;
}
// public properties
/// <summary>
/// Gets the value.
/// </summary>
public XYPoint Value
{
get { return _value; }
}
// internal methods
/// <summary>
/// Converts the GeoNearPoint into a BsonValue for the GeoNear command.
/// </summary>
/// <returns>A BsonValue.</returns>
internal override BsonValue ToGeoNearCommandValue()
{
return new BsonArray { _value.X, _value.Y };
}
}
/// <summary>
/// Represents a GeoNearPoint that wraps a GeoJsonPoint.
/// </summary>
/// <typeparam name="TCoordinates">The type of the coordinates.</typeparam>
public class GeoJson<TCoordinates> : GeoNearPoint where TCoordinates : GeoJsonCoordinates
{
// private fields
private readonly GeoJsonPoint<TCoordinates> _value;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeoJson{TCoordinates}"/> class.
/// </summary>
/// <param name="value">The value.</param>
public GeoJson(GeoJsonPoint<TCoordinates> value)
{
_value = value;
}
// public properties
/// <summary>
/// Gets the value.
/// </summary>
public GeoJsonPoint<TCoordinates> Value
{
get { return _value; }
}
// internal methods
/// <summary>
/// Converts the GeoNearPoint into a BsonValue for the GeoNear command.
/// </summary>
/// <returns>A BsonValue.</returns>
internal override BsonValue ToGeoNearCommandValue()
{
var document = new BsonDocument();
using (var writer = new BsonDocumentWriter(document, BsonDocumentWriterSettings.Defaults))
{
var context = BsonSerializationContext.CreateRoot(writer);
new GeoJsonPointSerializer<TCoordinates>().Serialize(context, _value);
}
return document;
}
}
}
}