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

/*
 * Test 1: check that intersection works on the meridian.  We insert a line
 * that crosses the meridian, and then run a geoIntersect with a line
 * that runs along the meridian.
 */

meridianCrossingLine = {
    geo: {type: "LineString", coordinates: [[-178.0, 10.0], [178.0, 10.0]]}
};

assert.writeOK(t.insert(meridianCrossingLine));

lineAlongMeridian = {
    type: "LineString",
    coordinates: [[180.0, 11.0], [180.0, 9.0]]
};

result = t.find({geo: {$geoIntersects: {$geometry: lineAlongMeridian}}});
assert.eq(result.itcount(), 1);

t.drop();
t.ensureIndex({geo: "2dsphere"});
/*
 * Test 2: check that within work across the meridian.  We insert points
 * on the meridian, and immediately on either side, and confirm that a poly
 * covering all of them returns them all.
 */
pointOnNegativeSideOfMeridian = {
    geo: {type: "Point", coordinates: [-179.0, 1.0]}
};
pointOnMeridian = {
    geo: {type: "Point", coordinates: [180.0, 1.0]}
};
pointOnPositiveSideOfMeridian = {
    geo: {type: "Point", coordinates: [179.0, 1.0]}
};

t.insert(pointOnMeridian);
t.insert(pointOnNegativeSideOfMeridian);
t.insert(pointOnPositiveSideOfMeridian);

meridianCrossingPoly = {
    type: "Polygon",
    coordinates: [[[-178.0, 10.0], [178.0, 10.0], [178.0, -10.0], [-178.0, -10.0], [-178.0, 10.0]]]
};

result = t.find({geo: {$geoWithin: {$geometry: meridianCrossingPoly}}});
assert.eq(result.itcount(), 3);

t.drop();
t.ensureIndex({geo: "2dsphere"});
/*
 * Test 3: Check that near works around the meridian.  Insert two points, one
 * closer, but across the meridian, and confirm they both come back, and
 * that the order is correct.
 */
pointOnNegativeSideOfMerid = {
    name: "closer",
    geo: {type: "Point", coordinates: [-179.0, 0.0]}
};

pointOnPositiveSideOfMerid = {
    name: "farther",
    geo: {type: "Point", coordinates: [176.0, 0.0]}
};

t.insert(pointOnNegativeSideOfMerid);
t.insert(pointOnPositiveSideOfMerid);

pointOnPositiveSideOfMeridian = {
    type: "Point",
    coordinates: [179.0, 0.0]
};

result = t.find({geo: {$geoNear: pointOnPositiveSideOfMeridian}});
assert.eq(result.itcount(), 2);
result = t.find({geo: {$geoNear: pointOnPositiveSideOfMeridian}});
assert.eq(result[0].name, "closer");
assert.eq(result[1].name, "farther");