Skip to content

Commit 676753f

Browse files
author
rstam
committed
CSHARP-685: Initial implementation of GeoJson object model.
1 parent a672a75 commit 676753f

File tree

74 files changed

+5363
-24
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+5363
-24
lines changed

MongoDB.Bson/IO/BsonReader.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,6 @@ protected void VerifyBsonType(string methodName, BsonType requiredBsonType)
947947
/// <param name="expectedName">The expected name.</param>
948948
protected void VerifyName(string expectedName)
949949
{
950-
ReadBsonType();
951950
var actualName = ReadName();
952951
if (actualName != expectedName)
953952
{

MongoDB.Bson/Serialization/Attributes/BsonSerializerAttribute.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,22 @@ internal IBsonSerializer CreateSerializer(Type type)
7979
throw new InvalidOperationException(message);
8080
}
8181

82-
if (_serializerType.ContainsGenericParameters)
82+
if (_serializerType.ContainsGenericParameters && !type.IsGenericType)
8383
{
84-
var message = "Cannot create a serializer because the serializer type is an open generic type.";
84+
var message = "Cannot create a serializer because the serializer type is an open generic type and the type to serialize is not generic.";
8585
throw new InvalidOperationException(message);
8686
}
8787

88-
return (IBsonSerializer)Activator.CreateInstance(_serializerType);
88+
if (_serializerType.ContainsGenericParameters)
89+
{
90+
var genericArguments = type.GetGenericArguments();
91+
var closedSerializerType = _serializerType.MakeGenericType(genericArguments);
92+
return (IBsonSerializer)Activator.CreateInstance(closedSerializerType);
93+
}
94+
else
95+
{
96+
return (IBsonSerializer)Activator.CreateInstance(_serializerType);
97+
}
8998
}
9099
}
91100
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson;
17+
18+
namespace MongoDB.Driver.GeoJsonObjectModel
19+
{
20+
public static class GeoJson
21+
{
22+
// public static methods
23+
public static GeoJsonBoundingBox<TCoordinates> BoundingBox<TCoordinates>(TCoordinates min, TCoordinates max) where TCoordinates : GeoJsonCoordinates
24+
{
25+
return new GeoJsonBoundingBox<TCoordinates>(min, max);
26+
}
27+
28+
public static GeoJsonFeature<TCoordinates> Feature<TCoordinates>(GeoJsonGeometry<TCoordinates> geometry) where TCoordinates : GeoJsonCoordinates
29+
{
30+
return new GeoJsonFeature<TCoordinates>(geometry);
31+
}
32+
33+
public static GeoJsonFeature<TCoordinates> Feature<TCoordinates>(GeoJsonFeatureArgs<TCoordinates> args, GeoJsonGeometry<TCoordinates> geometry) where TCoordinates : GeoJsonCoordinates
34+
{
35+
return new GeoJsonFeature<TCoordinates>(args, geometry);
36+
}
37+
38+
public static GeoJsonFeatureCollection<TCoordinates> FeatureCollection<TCoordinates>(GeoJsonObjectArgs<TCoordinates> args, params GeoJsonFeature<TCoordinates>[] features) where TCoordinates : GeoJsonCoordinates
39+
{
40+
return new GeoJsonFeatureCollection<TCoordinates>(args, features);
41+
}
42+
43+
public static GeoJsonFeatureCollection<TCoordinates> FeatureCollection<TCoordinates>(params GeoJsonFeature<TCoordinates>[] features) where TCoordinates : GeoJsonCoordinates
44+
{
45+
return new GeoJsonFeatureCollection<TCoordinates>(features);
46+
}
47+
48+
public static GeoJson2DGeographicCoordinates Geographic(double longitude, double latitude)
49+
{
50+
return new GeoJson2DGeographicCoordinates(longitude, latitude);
51+
}
52+
53+
public static GeoJson3DGeographicCoordinates Geographic(double longitude, double latitude, double altitude)
54+
{
55+
return new GeoJson3DGeographicCoordinates(longitude, latitude, altitude);
56+
}
57+
58+
public static GeoJsonGeometryCollection<TCoordinates> GeometryCollection<TCoordinates>(GeoJsonObjectArgs<TCoordinates> args, params GeoJsonGeometry<TCoordinates>[] geometries) where TCoordinates : GeoJsonCoordinates
59+
{
60+
return new GeoJsonGeometryCollection<TCoordinates>(args, geometries);
61+
}
62+
63+
public static GeoJsonGeometryCollection<TCoordinates> GeometryCollection<TCoordinates>(params GeoJsonGeometry<TCoordinates>[] geometries) where TCoordinates : GeoJsonCoordinates
64+
{
65+
return new GeoJsonGeometryCollection<TCoordinates>(geometries);
66+
}
67+
68+
public static GeoJsonLinearRingCoordinates<TCoordinates> LinearRingCoordinates<TCoordinates>(params TCoordinates[] positions) where TCoordinates : GeoJsonCoordinates
69+
{
70+
return new GeoJsonLinearRingCoordinates<TCoordinates>(positions);
71+
}
72+
73+
public static GeoJsonLineString<TCoordinates> LineString<TCoordinates>(GeoJsonObjectArgs<TCoordinates> args, params TCoordinates[] positions) where TCoordinates : GeoJsonCoordinates
74+
{
75+
var coordinates = new GeoJsonLineStringCoordinates<TCoordinates>(positions);
76+
return new GeoJsonLineString<TCoordinates>(args, coordinates);
77+
}
78+
79+
public static GeoJsonLineString<TCoordinates> LineString<TCoordinates>(params TCoordinates[] positions) where TCoordinates : GeoJsonCoordinates
80+
{
81+
var coordinates = new GeoJsonLineStringCoordinates<TCoordinates>(positions);
82+
return new GeoJsonLineString<TCoordinates>(coordinates);
83+
}
84+
85+
public static GeoJsonLineStringCoordinates<TCoordinates> LineStringCoordinates<TCoordinates>(params TCoordinates[] positions) where TCoordinates : GeoJsonCoordinates
86+
{
87+
return new GeoJsonLineStringCoordinates<TCoordinates>(positions);
88+
}
89+
90+
public static GeoJsonMultiLineString<TCoordinates> MultiLineString<TCoordinates>(GeoJsonObjectArgs<TCoordinates> args, params GeoJsonLineStringCoordinates<TCoordinates>[] lineStrings) where TCoordinates : GeoJsonCoordinates
91+
{
92+
var coordinates = new GeoJsonMultiLineStringCoordinates<TCoordinates>(lineStrings);
93+
return new GeoJsonMultiLineString<TCoordinates>(args, coordinates);
94+
}
95+
96+
public static GeoJsonMultiLineString<TCoordinates> MultiLineString<TCoordinates>(params GeoJsonLineStringCoordinates<TCoordinates>[] lineStrings) where TCoordinates : GeoJsonCoordinates
97+
{
98+
var coordinates = new GeoJsonMultiLineStringCoordinates<TCoordinates>(lineStrings);
99+
return new GeoJsonMultiLineString<TCoordinates>(coordinates);
100+
}
101+
102+
public static GeoJsonMultiPoint<TCoordinates> MultiPoint<TCoordinates>(GeoJsonObjectArgs<TCoordinates> args, params TCoordinates[] positions) where TCoordinates : GeoJsonCoordinates
103+
{
104+
var coordinates = new GeoJsonMultiPointCoordinates<TCoordinates>(positions);
105+
return new GeoJsonMultiPoint<TCoordinates>(args, coordinates);
106+
}
107+
108+
public static GeoJsonMultiPoint<TCoordinates> MultiPoint<TCoordinates>(params TCoordinates[] positions) where TCoordinates : GeoJsonCoordinates
109+
{
110+
var coordinates = new GeoJsonMultiPointCoordinates<TCoordinates>(positions);
111+
return new GeoJsonMultiPoint<TCoordinates>(coordinates);
112+
}
113+
114+
public static GeoJsonMultiPolygon<TCoordinates> MultiPolygon<TCoordinates>(GeoJsonObjectArgs<TCoordinates> args, params GeoJsonPolygonCoordinates<TCoordinates>[] polygons) where TCoordinates : GeoJsonCoordinates
115+
{
116+
var coordinates = new GeoJsonMultiPolygonCoordinates<TCoordinates>(polygons);
117+
return new GeoJsonMultiPolygon<TCoordinates>(args, coordinates);
118+
}
119+
120+
public static GeoJsonMultiPolygon<TCoordinates> MultiPolygon<TCoordinates>(params GeoJsonPolygonCoordinates<TCoordinates>[] polygons) where TCoordinates : GeoJsonCoordinates
121+
{
122+
var coordinates = new GeoJsonMultiPolygonCoordinates<TCoordinates>(polygons);
123+
return new GeoJsonMultiPolygon<TCoordinates>(coordinates);
124+
}
125+
126+
public static GeoJsonPoint<TCoordinates> Point<TCoordinates>(GeoJsonObjectArgs<TCoordinates> args, TCoordinates coordinates) where TCoordinates : GeoJsonCoordinates
127+
{
128+
return new GeoJsonPoint<TCoordinates>(args, coordinates);
129+
}
130+
131+
public static GeoJsonPoint<TCoordinates> Point<TCoordinates>(TCoordinates coordinates) where TCoordinates : GeoJsonCoordinates
132+
{
133+
return new GeoJsonPoint<TCoordinates>(coordinates);
134+
}
135+
136+
public static GeoJsonPolygon<TCoordinates> Polygon<TCoordinates>(GeoJsonObjectArgs<TCoordinates> args, params TCoordinates[] positions) where TCoordinates : GeoJsonCoordinates
137+
{
138+
var exterior = new GeoJsonLinearRingCoordinates<TCoordinates>(positions);
139+
var coordinates = new GeoJsonPolygonCoordinates<TCoordinates>(exterior);
140+
return new GeoJsonPolygon<TCoordinates>(args, coordinates);
141+
}
142+
143+
public static GeoJsonPolygon<TCoordinates> Polygon<TCoordinates>(GeoJsonObjectArgs<TCoordinates> args, GeoJsonPolygonCoordinates<TCoordinates> coordinates) where TCoordinates : GeoJsonCoordinates
144+
{
145+
return new GeoJsonPolygon<TCoordinates>(args, coordinates);
146+
}
147+
148+
public static GeoJsonPolygon<TCoordinates> Polygon<TCoordinates>(GeoJsonPolygonCoordinates<TCoordinates> coordinates) where TCoordinates : GeoJsonCoordinates
149+
{
150+
return new GeoJsonPolygon<TCoordinates>(coordinates);
151+
}
152+
153+
public static GeoJsonPolygon<TCoordinates> Polygon<TCoordinates>(params TCoordinates[] positions) where TCoordinates : GeoJsonCoordinates
154+
{
155+
var exterior = new GeoJsonLinearRingCoordinates<TCoordinates>(positions);
156+
var coordinates = new GeoJsonPolygonCoordinates<TCoordinates>(exterior);
157+
return new GeoJsonPolygon<TCoordinates>(coordinates);
158+
}
159+
160+
public static GeoJsonPolygonCoordinates<TCoordinates> PolygonCoordinates<TCoordinates>(params TCoordinates[] positions) where TCoordinates : GeoJsonCoordinates
161+
{
162+
var exterior = new GeoJsonLinearRingCoordinates<TCoordinates>(positions);
163+
return new GeoJsonPolygonCoordinates<TCoordinates>(exterior);
164+
}
165+
166+
public static GeoJsonPolygonCoordinates<TCoordinates> PolygonCoordinates<TCoordinates>(GeoJsonLinearRingCoordinates<TCoordinates> exterior, params GeoJsonLinearRingCoordinates<TCoordinates>[] holes) where TCoordinates : GeoJsonCoordinates
167+
{
168+
return new GeoJsonPolygonCoordinates<TCoordinates>(exterior, holes);
169+
}
170+
171+
public static GeoJson2DCoordinates Position(double x, double y)
172+
{
173+
return new GeoJson2DCoordinates(x, y);
174+
}
175+
176+
public static GeoJson3DCoordinates Position(double x, double y, double z)
177+
{
178+
return new GeoJson3DCoordinates(x, y, z);
179+
}
180+
181+
public static GeoJson2DProjectedCoordinates Projected(double easting, double northing)
182+
{
183+
return new GeoJson2DProjectedCoordinates(easting, northing);
184+
}
185+
186+
public static GeoJson3DProjectedCoordinates Projected(double easting, double northing, double altitude)
187+
{
188+
return new GeoJson3DProjectedCoordinates(easting, northing, altitude);
189+
}
190+
}
191+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Collections.ObjectModel;
17+
using MongoDB.Bson.Serialization.Attributes;
18+
using MongoDB.Driver.GeoJsonObjectModel.Serializers;
19+
20+
namespace MongoDB.Driver.GeoJsonObjectModel
21+
{
22+
[BsonSerializer(typeof(GeoJson2DCoordinatesSerializer))]
23+
public class GeoJson2DCoordinates : GeoJsonCoordinates
24+
{
25+
// private fields
26+
private ReadOnlyCollection<double> _values;
27+
28+
// constructors
29+
public GeoJson2DCoordinates(double x, double y)
30+
{
31+
_values = new ReadOnlyCollection<double>(new[] { x, y });
32+
}
33+
34+
// public properties
35+
public override ReadOnlyCollection<double> Values
36+
{
37+
get { return _values; }
38+
}
39+
40+
public double X
41+
{
42+
get { return _values[0]; }
43+
}
44+
45+
public double Y
46+
{
47+
get { return _values[1]; }
48+
}
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Collections.ObjectModel;
17+
using MongoDB.Bson.Serialization.Attributes;
18+
using MongoDB.Driver.GeoJsonObjectModel.Serializers;
19+
20+
namespace MongoDB.Driver.GeoJsonObjectModel
21+
{
22+
[BsonSerializer(typeof(GeoJson2DGeographicCoordinatesSerializer))]
23+
public class GeoJson2DGeographicCoordinates : GeoJsonCoordinates
24+
{
25+
// private fields
26+
private ReadOnlyCollection<double> _values;
27+
28+
// constructors
29+
public GeoJson2DGeographicCoordinates(double longitude, double latitude)
30+
{
31+
_values = new ReadOnlyCollection<double>(new[] { longitude, latitude });
32+
}
33+
34+
// public properties
35+
public override ReadOnlyCollection<double> Values
36+
{
37+
get { return _values; }
38+
}
39+
40+
public double Longitude
41+
{
42+
get { return _values[0]; }
43+
}
44+
45+
public double Latitude
46+
{
47+
get { return _values[1]; }
48+
}
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Copyright 2010-2013 10gen Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Collections.ObjectModel;
17+
using MongoDB.Bson.Serialization.Attributes;
18+
using MongoDB.Driver.GeoJsonObjectModel.Serializers;
19+
20+
namespace MongoDB.Driver.GeoJsonObjectModel
21+
{
22+
[BsonSerializer(typeof(GeoJson2DProjectedCoordinatesSerializer))]
23+
public class GeoJson2DProjectedCoordinates : GeoJsonCoordinates
24+
{
25+
// private fields
26+
private ReadOnlyCollection<double> _values;
27+
28+
// constructors
29+
public GeoJson2DProjectedCoordinates(double easting, double northing)
30+
{
31+
_values = new ReadOnlyCollection<double>(new[] { easting, northing });
32+
}
33+
34+
// public properties
35+
public override ReadOnlyCollection<double> Values
36+
{
37+
get { return _values; }
38+
}
39+
40+
public double Easting
41+
{
42+
get { return _values[0]; }
43+
}
44+
45+
public double Northing
46+
{
47+
get { return _values[1]; }
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)