summaryrefslogtreecommitdiff
path: root/jstests/core/geo_s2intersection.js
blob: faf9304396c69f6cd3c7bc5578d00b56516a119e (plain)
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
var t = db.geo_s2intersectinglines;
t.drop();
t.ensureIndex({geo: "2dsphere"});

/* All the tests in this file are generally confirming intersections based upon
 * these three geo objects.
 */
var canonLine = {
    name: 'canonLine',
    geo: {type: "LineString", coordinates: [[0.0, 0.0], [1.0, 0.0]]}
};

var canonPoint = {name: 'canonPoint', geo: {type: "Point", coordinates: [10.0, 10.0]}};

var canonPoly = {
    name: 'canonPoly',
    geo: {
        type: "Polygon",
        coordinates: [[[50.0, 50.0], [51.0, 50.0], [51.0, 51.0], [50.0, 51.0], [50.0, 50.0]]]
    }
};

t.insert(canonLine);
t.insert(canonPoint);
t.insert(canonPoly);

// Case 1: Basic sanity intersection.
var testLine = {type: "LineString", coordinates: [[0.5, 0.5], [0.5, -0.5]]};

var result = t.find({geo: {$geoIntersects: {$geometry: testLine}}});
assert.eq(result.count(), 1);
assert.eq(result[0]['name'], 'canonLine');

// Case 2: Basic Polygon intersection.
// we expect that the canonLine should intersect with this polygon.
var testPoly = {
    type: "Polygon",
    coordinates: [[[0.4, -0.1], [0.4, 0.1], [0.6, 0.1], [0.6, -0.1], [0.4, -0.1]]]
};

result = t.find({geo: {$geoIntersects: {$geometry: testPoly}}});
assert.eq(result.count(), 1);
assert.eq(result[0]['name'], 'canonLine');

// Case 3: Intersects the vertex of a line.
// When a line intersects the vertex of a line, we expect this to
// count as a geoIntersection.
testLine = {
    type: "LineString",
    coordinates: [[0.0, 0.5], [0.0, -0.5]]
};

result = t.find({geo: {$geoIntersects: {$geometry: testLine}}});
assert.eq(result.count(), 1);
assert.eq(result[0]['name'], 'canonLine');

// Case 4: Sanity no intersection.
// This line just misses the canonLine in the negative direction.  This
// should not count as a geoIntersection.
testLine = {
    type: "LineString",
    coordinates: [[-0.1, 0.5], [-0.1, -0.5]]
};

result = t.find({geo: {$geoIntersects: {$geometry: testLine}}});
assert.eq(result.count(), 0);

// Case 5: Overlapping line - only partially overlaps.
// Undefined behaviour: does intersect
testLine = {
    type: "LineString",
    coordinates: [[-0.5, 0.0], [0.5, 0.0]]
};

var result = t.find({geo: {$geoIntersects: {$geometry: testLine}}});
assert.eq(result.count(), 1);
assert.eq(result[0]['name'], 'canonLine');

// Case 6: Contained line - this line is fully contained by the canonLine
// Undefined behaviour: doesn't intersect.
testLine = {
    type: "LineString",
    coordinates: [[0.1, 0.0], [0.9, 0.0]]
};

result = t.find({geo: {$geoIntersects: {$geometry: testLine}}});
assert.eq(result.count(), 0);

// Case 7: Identical line in the identical position.
// Undefined behaviour: does intersect.
testLine = {
    type: "LineString",
    coordinates: [[0.0, 0.0], [1.0, 0.0]]
};

result = t.find({geo: {$geoIntersects: {$geometry: testLine}}});
assert.eq(result.count(), 1);
assert.eq(result[0]['name'], 'canonLine');

// Case 8: Point intersection - we search with a line that intersects
// with the canonPoint.
testLine = {
    type: "LineString",
    coordinates: [[10.0, 11.0], [10.0, 9.0]]
};

result = t.find({geo: {$geoIntersects: {$geometry: testLine}}});
assert.eq(result.count(), 1);
assert.eq(result[0]['name'], 'canonPoint');

// Case 9: Point point intersection
// as above but with an identical point to the canonPoint.  We expect an
// intersection here.
testPoint = {
    type: "Point",
    coordinates: [10.0, 10.0]
};

result = t.find({geo: {$geoIntersects: {$geometry: testPoint}}});
assert.eq(result.count(), 1);
assert.eq(result[0]['name'], 'canonPoint');

// Case 10: Sanity point non-intersection.
var testPoint = {type: "Point", coordinates: [12.0, 12.0]};

result = t.find({geo: {$geoIntersects: {$geometry: testPoint}}});
assert.eq(result.count(), 0);

// Case 11: Point polygon intersection
// verify that a point inside a polygon $geoIntersects.
testPoint = {
    type: "Point",
    coordinates: [50.5, 50.5]
};

result = t.find({geo: {$geoIntersects: {$geometry: testPoint}}});
assert.eq(result.count(), 1);
assert.eq(result[0]['name'], 'canonPoly');

// Case 12: Make sure that we properly handle and $and of two
// $geoIntersects predicates.
t.drop();
t.ensureIndex({a: "2dsphere"});
t.insert({a: {type: "Polygon", coordinates: [[[0, 0], [3, 6], [6, 0], [0, 0]]]}});

var firstPoint = {$geometry: {type: "Point", coordinates: [3.0, 1.0]}};
var secondPoint = {$geometry: {type: "Point", coordinates: [4.0, 1.0]}};

// First point should intersect with the polygon.
result = t.find({a: {$geoIntersects: firstPoint}});
assert.eq(result.count(), 1);

// Second point also intersects with the polygon.
result = t.find({a: {$geoIntersects: secondPoint}});
assert.eq(result.count(), 1);

// Both points intersect with the polygon, so the $and of
// two $geoIntersects should as well.
result = t.find({$and: [{a: {$geoIntersects: firstPoint}}, {a: {$geoIntersects: secondPoint}}]});
assert.eq(result.count(), 1);