summaryrefslogtreecommitdiff
path: root/jstests/core/geo_s2near.js
blob: 2cd1df5cbbc7ddfff416ae7244080bf73620c687 (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
// @tags: [
//   requires_getmore,
//   sbe_incompatible,
// ]

// Test 2dsphere near search, called via find and $geoNear.
(function() {
t = db.geo_s2near;
t.drop();

// Make sure that geoNear gives us back loc
goldenPoint = {
    type: "Point",
    coordinates: [31.0, 41.0]
};
t.insert({geo: goldenPoint});
t.createIndex({geo: "2dsphere"});
resNear =
    t.aggregate([
         {$geoNear: {near: [30, 40], distanceField: "d", spherical: true, includeLocs: "loc"}},
         {$limit: 1}
     ]).toArray();
assert.eq(resNear.length, 1, tojson(resNear));
assert.eq(resNear[0].loc, goldenPoint);

// FYI:
// One degree of long @ 0 is 111km or so.
// One degree of lat @ 0 is 110km or so.
lat = 0;
lng = 0;
points = 10;
for (var x = -points; x < points; x += 1) {
    for (var y = -points; y < points; y += 1) {
        t.insert({geo: {"type": "Point", "coordinates": [lng + x / 1000.0, lat + y / 1000.0]}});
    }
}

origin = {
    "type": "Point",
    "coordinates": [lng, lat]
};

t.createIndex({geo: "2dsphere"});

// Near only works when the query is a point.
someline = {
    "type": "LineString",
    "coordinates": [[40, 5], [41, 6]]
};
somepoly = {
    "type": "Polygon",
    "coordinates": [[[40, 5], [40, 6], [41, 6], [41, 5], [40, 5]]]
};
assert.throws(function() {
    return t.find({"geo": {"$near": {"$geometry": someline}}}).count();
});
assert.throws(function() {
    return t.find({"geo": {"$near": {"$geometry": somepoly}}}).count();
});
assert.throws(function() {
    return t.aggregate({$geoNear: {near: someline, distanceField: "dis", spherical: true}});
});
assert.throws(function() {
    return t.aggregate({$geoNear: {near: somepoly, distanceField: "dis", spherical: true}});
});

// Do some basic near searches.
res = t.find({"geo": {"$near": {"$geometry": origin, $maxDistance: 2000}}}).limit(10);
resNear = t.aggregate([
    {$geoNear: {near: [0, 0], distanceField: "dis", maxDistance: Math.PI, spherical: true}},
    {$limit: 10},
]);
assert.eq(res.itcount(), resNear.itcount(), "10");

res = t.find({"geo": {"$near": {"$geometry": origin}}}).limit(10);
resNear = t.aggregate([
    {$geoNear: {near: [0, 0], distanceField: "dis", spherical: true}},
    {$limit: 10},
]);
assert.eq(res.itcount(), resNear.itcount(), "10");

// Find all the points!
res = t.find({"geo": {"$near": {"$geometry": origin}}}).limit(10000);
resNear = t.aggregate([
    {$geoNear: {near: [0, 0], distanceField: "dis", spherical: true}},
    {$limit: 10000},
]);
assert.eq(res.itcount(), resNear.itcount(), ((2 * points) * (2 * points)).toString());

// longitude goes -180 to 180
// latitude goes -90 to 90
// Let's put in some perverse (polar) data and make sure we get it back.
// Points go long, lat.
t.insert({geo: {"type": "Point", "coordinates": [-180, -90]}});
t.insert({geo: {"type": "Point", "coordinates": [180, -90]}});
t.insert({geo: {"type": "Point", "coordinates": [180, 90]}});
t.insert({geo: {"type": "Point", "coordinates": [-180, 90]}});
res = t.find({"geo": {"$near": {"$geometry": origin}}}).limit(10000);
resNear = t.aggregate([
    {$geoNear: {near: [0, 0], distanceField: "dis", spherical: true}},
    {$limit: 10000},
]);
assert.eq(res.itcount(), resNear.itcount(), ((2 * points) * (2 * points) + 4).toString());

function testRadAndDegreesOK(distance) {
    // Distance for old style points is radians.
    resRadians = t.find({geo: {$nearSphere: [0, 0], $maxDistance: (distance / (6378.1 * 1000))}});
    // Distance for new style points is meters.
    resMeters = t.find({"geo": {"$near": {"$geometry": origin, $maxDistance: distance}}});
    // And we should get the same # of results no matter what.
    assert.eq(resRadians.itcount(), resMeters.itcount());

    // Also, $geoNear should behave the same way.
    resGNMeters = t.aggregate({
                       $geoNear: {
                           near: origin,
                           distanceField: "dis",
                           maxDistance: distance,
                           spherical: true,
                       }
                   }).toArray();
    resGNRadians = t.aggregate({
                        $geoNear: {
                            near: [0, 0],
                            distanceField: "dis",
                            maxDistance: (distance / (6378.1 * 1000)),
                            spherical: true,
                        }
                    }).toArray();
    const errmsg = `$geoNear using meter distances returned ${tojson(resGNMeters)}, but ` +
        `$geoNear using radian distances returned ${tojson(resGNRadians)}`;
    assert.eq(resGNRadians.length, resGNMeters.length, errmsg);
    for (var i = 0; i < resGNRadians.length; ++i) {
        // Radius of earth * radians = distance in meters.
        assert.close(resGNRadians[i].dis * 6378.1 * 1000, resGNMeters[i].dis);
    }
}

testRadAndDegreesOK(1);
testRadAndDegreesOK(10);
testRadAndDegreesOK(50);
testRadAndDegreesOK(10000);

// SERVER-13666 legacy coordinates must be in bounds for spherical near queries.
assert.commandFailedWithCode(db.runCommand({
    aggregate: t.getName(),
    cursor: {},
    pipeline: [{
        $geoNear: {
            near: [1210.466, 31.2051],
            distanceField: "dis",
            spherical: true,
        }
    }]
}),
                             17444);
}());