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 ;
17
+ using System . Collections . Generic ;
18
+ using System . Linq ;
19
+ using System . Text ;
20
+ using MongoDB . Bson ;
21
+ using MongoDB . Bson . Serialization . Attributes ;
22
+ using MongoDB . Driver ;
23
+ using MongoDB . Driver . Builders ;
24
+ using MongoDB . Driver . GeoJsonObjectModel ;
25
+ using NUnit . Framework ;
26
+
27
+ namespace MongoDB . DriverUnitTests . Builders
28
+ {
29
+ public class QueryBuilderGeoJsonIntegrationTests
30
+ {
31
+ private MongoCollection < GeoClass > _collection ;
32
+
33
+ private class GeoClass
34
+ {
35
+ public int Id { get ; set ; }
36
+
37
+ [ BsonElement ( "loc" ) ]
38
+ public GeoJsonPoint < GeoJson2DGeographicCoordinates > Location { get ; set ; }
39
+
40
+ [ BsonElement ( "sur" ) ]
41
+ public GeoJsonPolygon < GeoJson2DGeographicCoordinates > Surrounding { get ; set ; }
42
+ }
43
+
44
+ [ SetUp ]
45
+ public void SetUp ( )
46
+ {
47
+ var db = Configuration . TestDatabase ;
48
+ _collection = db . GetCollection < GeoClass > ( "geo" ) ;
49
+
50
+ _collection . Drop ( ) ;
51
+ _collection . EnsureIndex ( IndexKeys < GeoClass > . GeoSpatialSpherical ( x => x . Location ) ) ;
52
+ _collection . EnsureIndex ( IndexKeys < GeoClass > . GeoSpatialSpherical ( x => x . Surrounding ) ) ;
53
+
54
+ var doc = new GeoClass
55
+ {
56
+ Id = 1 ,
57
+ Location = GeoJson . Point ( GeoJson . Geographic ( 40.5 , 18.5 ) ) ,
58
+ Surrounding = GeoJson . Polygon (
59
+ GeoJson . Geographic ( 40 , 18 ) ,
60
+ GeoJson . Geographic ( 40 , 19 ) ,
61
+ GeoJson . Geographic ( 41 , 19 ) ,
62
+ GeoJson . Geographic ( 40 , 18 ) )
63
+ } ;
64
+
65
+ _collection . Save ( doc ) ;
66
+ }
67
+
68
+ [ Test ]
69
+ public void TestGeoIntersects ( )
70
+ {
71
+ var server = Configuration . TestServer ;
72
+ server . Connect ( ) ;
73
+ if ( server . BuildInfo . Version >= new Version ( 2 , 3 , 2 ) )
74
+ {
75
+ var point = GeoJson . Point ( GeoJson . Geographic ( 40 , 18 ) ) ;
76
+
77
+ var query = Query < GeoClass > . GeoIntersects ( x => x . Surrounding , point ) ;
78
+
79
+ var results = _collection . Count ( query ) ;
80
+
81
+ Assert . AreEqual ( 1 , results ) ;
82
+ }
83
+ }
84
+
85
+ [ Test ]
86
+ public void TestNear ( )
87
+ {
88
+ var server = Configuration . TestServer ;
89
+ server . Connect ( ) ;
90
+ if ( server . BuildInfo . Version >= new Version ( 2 , 3 , 2 ) )
91
+ {
92
+ var point = GeoJson . Point ( GeoJson . Geographic ( 40 , 18 ) ) ;
93
+
94
+ var query = Query < GeoClass > . Near ( x => x . Location , point ) ;
95
+
96
+ var results = _collection . Count ( query ) ;
97
+
98
+ Assert . AreEqual ( 1 , results ) ;
99
+ }
100
+ }
101
+
102
+ [ Test ]
103
+ public void TestWithin ( )
104
+ {
105
+ var server = Configuration . TestServer ;
106
+ server . Connect ( ) ;
107
+ if ( server . BuildInfo . Version >= new Version ( 2 , 3 , 2 ) )
108
+ {
109
+ var polygon = GeoJson . Polygon (
110
+ GeoJson . Geographic ( 40 , 18 ) ,
111
+ GeoJson . Geographic ( 40 , 19 ) ,
112
+ GeoJson . Geographic ( 41 , 19 ) ,
113
+ GeoJson . Geographic ( 41 , 18 ) ,
114
+ GeoJson . Geographic ( 40 , 18 ) ) ;
115
+
116
+ var query = Query < GeoClass > . Within ( x => x . Location , polygon ) ;
117
+
118
+ var results = _collection . Count ( query ) ;
119
+
120
+ Assert . AreEqual ( 1 , results ) ;
121
+ }
122
+ }
123
+
124
+ [ Test ]
125
+ public void TestWithinNotFound ( )
126
+ {
127
+ var server = Configuration . TestServer ;
128
+ server . Connect ( ) ;
129
+ if ( server . BuildInfo . Version >= new Version ( 2 , 3 , 2 ) )
130
+ {
131
+ var polygon = GeoJson . Polygon (
132
+ GeoJson . Geographic ( 41 , 19 ) ,
133
+ GeoJson . Geographic ( 41 , 20 ) ,
134
+ GeoJson . Geographic ( 42 , 20 ) ,
135
+ GeoJson . Geographic ( 42 , 19 ) ,
136
+ GeoJson . Geographic ( 41 , 19 ) ) ;
137
+
138
+ var query = Query < GeoClass > . Within ( x => x . Location , polygon ) ;
139
+
140
+ var results = _collection . Count ( query ) ;
141
+
142
+ Assert . AreEqual ( 0 , results ) ;
143
+ }
144
+ }
145
+ }
146
+ }
0 commit comments