summaryrefslogtreecommitdiff
path: root/jstests/core/geo_polygon1.js
blob: 11f17910306a773850fed3049686469885fa5f8e (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
//
// Tests for N-dimensional polygon querying
//

t = db.geo_polygon1;
t.drop();

num = 0;
for ( x=1; x < 9; x++ ){
    for ( y= 1; y < 9; y++ ){
        o = { _id : num++ , loc : [ x , y ] };
        t.save( o );
    }
}

t.ensureIndex( { loc : "2d" } );

triangle = [[0,0], [1,1], [0,2]];

// Look at only a small slice of the data within a triangle
assert.eq( 1 , t.find( { loc: { "$within": { "$polygon" : triangle }}} ).count() , "Triangle Test" );

boxBounds = [ [0,0], [0,10], [10,10], [10,0] ];

assert.eq( num , t.find( { loc : { "$within" : { "$polygon" : boxBounds } } } ).count() , "Bounding Box Test" );

//Make sure we can add object-based polygons
assert.eq( num, t.find( { loc : { $within : { $polygon : { a : [-10, -10], b : [-10, 10], c : [10, 10], d : [10, -10] } } } } ).count() )

// Look in a box much bigger than the one we have data in
boxBounds = [[-100,-100], [-100, 100], [100,100], [100,-100]];
assert.eq( num , t.find( { loc : { "$within" : { "$polygon" : boxBounds } } } ).count() , "Big Bounding Box Test" );

t.drop();

pacman = [
           [0,2], [0,4], [2,6], [4,6], // Head
           [6,4], [4,3], [6,2], // Mouth
           [4,0], [2,0] // Bottom
         ];

t.save({loc: [1,3] }); // Add a point that's in
assert.writeOK(t.ensureIndex( { loc : "2d" } ));

assert.eq( 1 , t.find({loc : { $within : { $polygon : pacman }}} ).count() , "Pacman single point" );

t.save({ loc : [5, 3] })  // Add a point that's out right in the mouth opening
t.save({ loc : [3, 7] })  // Add a point above the center of the head
t.save({ loc : [3,-1] })  // Add a point below the center of the bottom

assert.eq( 1 , t.find({loc : { $within : { $polygon : pacman }}} ).count() , "Pacman double point" );

// Make sure we can't add bad polygons
okay = true
try{ 
	t.find( { loc : { $within : { $polygon : [1, 2] } } } ).toArray()
    okay = false
}
catch(e){}
assert(okay)
try{ 
	t.find( { loc : { $within : { $polygon : [[1, 2]] } } } ).toArray()
    okay = false
}
catch(e){}
assert(okay)
try{ 
	t.find( { loc : { $within : { $polygon : [[1, 2], [2, 3]] } } } ).toArray()
    okay = false
}
catch(e){}
assert(okay)