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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
|
// @tags: [
// requires_fastcount,
// requires_non_retryable_writes,
// sbe_incompatible,
// ]
//
// Big Polygon related tests
// - Tests the capability for a geo query with a big polygon object (strictCRS)
// $geoWithin & $geoIntersects
// - Big polygon objects cannot be stored
// Try all different shapes queries against various stored geo points, line & polygons
(function() {
const crs84CRS = {
type: "name",
properties: {name: "urn:ogc:def:crs:OGC:1.3:CRS84"}
};
const epsg4326CRS = {
type: "name",
properties: {name: "EPSG:4326"}
};
const strictCRS = {
type: "name",
properties: {name: "urn:x-mongodb:crs:strictwinding:EPSG:4326"}
};
// invalid CRS name
const badCRS = {
type: "name",
properties: {name: "urn:x-mongodb:crs:invalid:EPSG:4326"}
};
// helper to generate a line along a longitudinal
function genLonLine(lon, startLat, endLat, latStep) {
let line = [];
for (let lat = startLat; lat <= endLat; lat += latStep) {
line.push([lon, lat]);
}
return line;
}
// Main program
// Clear out collection
const coll = db.geo_bigpoly;
coll.drop();
// GeoJson Objects to be inserted into collection
// coordinates are longitude, latitude
// strictCRS (big polygon) cannot be stored in the collection
const objects = [
{name: "boat ramp", geo: {type: "Point", coordinates: [-97.927117, 30.327376]}},
{name: "on equator", geo: {type: "Point", coordinates: [-97.9, 0]}},
{name: "just north of equator", geo: {type: "Point", coordinates: [-97.9, 0.1]}},
{name: "just south of equator", geo: {type: "Point", coordinates: [-97.9, -0.1]}},
{
name: "north pole - crs84CRS",
geo: {type: "Point", coordinates: [-97.9, 90.0], crs: crs84CRS}
},
{
name: "south pole - epsg4326CRS",
geo: {type: "Point", coordinates: [-97.9, -90.0], crs: epsg4326CRS}
},
{
name: "short line string: PA, LA, 4corners, ATX, Mansfield, FL, Reston, NYC",
geo: {
type: "LineString",
coordinates: [
[-122.1611953, 37.4420407],
[-118.283638, 34.028517],
[-109.045223, 36.9990835],
[-97.850404, 30.3921555],
[-97.904187, 30.395457],
[-86.600836, 30.398147],
[-77.357837, 38.9589935],
[-73.987723, 40.7575074]
]
}
},
{
name: "1024 point long line string from south pole to north pole",
geo: {type: "LineString", coordinates: genLonLine(2.349902, -90.0, 90.0, 180.0 / 1024)}
},
{
name: "line crossing equator - epsg4326CRS",
geo: {
type: "LineString",
coordinates: [[-77.0451853, -12.0553442], [-76.7784557, 18.0098528]],
crs: epsg4326CRS
}
},
{
name: "GeoJson polygon",
geo: {
type: "Polygon",
coordinates:
[[[-80.0, 30.0], [-40.0, 30.0], [-40.0, 60.0], [-80.0, 60.0], [-80.0, 30.0]]]
}
},
{
name: "polygon w/ hole",
geo: {
type: "Polygon",
coordinates: [
[[-80.0, 30.0], [-40.0, 30.0], [-40.0, 60.0], [-80.0, 60.0], [-80.0, 30.0]],
[[-70.0, 40.0], [-60.0, 40.0], [-60.0, 50.0], [-70.0, 50.0], [-70.0, 40.0]]
]
}
},
{
name: "polygon w/ two holes",
geo: {
type: "Polygon",
coordinates: [
[[-80.0, 30.0], [-40.0, 30.0], [-40.0, 60.0], [-80.0, 60.0], [-80.0, 30.0]],
[[-70.0, 40.0], [-60.0, 40.0], [-60.0, 50.0], [-70.0, 50.0], [-70.0, 40.0]],
[[-55.0, 40.0], [-45.0, 40.0], [-45.0, 50.0], [-55.0, 50.0], [-55.0, 40.0]]
]
}
},
{
name: "polygon covering North pole",
geo: {
type: "Polygon",
coordinates: [[[-120.0, 89.0], [0.0, 89.0], [120.0, 89.0], [-120.0, 89.0]]]
}
},
{
name: "polygon covering South pole",
geo: {
type: "Polygon",
coordinates: [[[-120.0, -89.0], [0.0, -89.0], [120.0, -89.0], [-120.0, -89.0]]]
}
},
{
name: "big polygon/rectangle covering both poles",
geo: {
type: "Polygon",
coordinates: [
[[-130.0, 89.0], [-120.0, 89.0], [-120.0, -89.0], [-130.0, -89.0], [-130.0, 89.0]]
],
crs: strictCRS
}
},
{
name: "polygon (triangle) w/ hole at North pole",
geo: {
type: "Polygon",
coordinates: [
[[-120.0, 80.0], [0.0, 80.0], [120.0, 80.0], [-120.0, 80.0]],
[[-120.0, 88.0], [0.0, 88.0], [120.0, 88.0], [-120.0, 88.0]]
]
}
},
{
name: "polygon with edge on equator",
geo: {
type: "Polygon",
coordinates: [[[-120.0, 0.0], [120.0, 0.0], [0.0, 90.0], [-120.0, 0.0]]]
}
},
{
name: "polygon just inside single hemisphere (Northern) - China, California, Europe",
geo: {
type: "Polygon",
coordinates:
[[[120.0, 0.000001], [-120.0, 0.000001], [0.0, 0.000001], [120.0, 0.000001]]]
}
},
{
name: "polygon inside Northern hemisphere",
geo: {
type: "Polygon",
coordinates: [[[120.0, 80.0], [-120.0, 80.0], [0.0, 80.0], [120.0, 80.0]]]
}
},
{
name: "polygon just inside a single hemisphere (Southern) - Pacific, Indonesia, Africa",
geo: {
type: "Polygon",
coordinates:
[[[-120.0, -0.000001], [120.0, -0.000001], [0.0, -0.000001], [-120.0, -0.000001]]]
}
},
{
name: "polygon inside Southern hemisphere",
geo: {
type: "Polygon",
coordinates: [[[-120.0, -80.0], [120.0, -80.0], [0.0, -80.0], [-120.0, -80.0]]]
}
},
{
name: "single point (MultiPoint): Palo Alto",
geo: {type: "MultiPoint", coordinates: [[-122.1611953, 37.4420407]]}
},
{
name: "multiple points(MultiPoint): PA, LA, 4corners, ATX, Mansfield, FL, Reston, NYC",
geo: {
type: "MultiPoint",
coordinates: [
[-122.1611953, 37.4420407],
[-118.283638, 34.028517],
[-109.045223, 36.9990835],
[-97.850404, 30.3921555],
[-97.904187, 30.395457],
[-86.600836, 30.398147],
[-77.357837, 38.9589935],
[-73.987723, 40.7575074]
]
}
},
{
name: "two points (MultiPoint): Shenzhen, Guangdong, China",
geo: {type: "MultiPoint", coordinates: [[114.0538788, 22.5551603], [114.022837, 22.44395]]}
},
{
name: "two points (MultiPoint) but only one in: Shenzhen, Guangdong, China",
geo: {type: "MultiPoint", coordinates: [[114.0538788, 22.5551603], [113.743858, 23.025815]]}
},
{
name: "multi line string: new zealand bays",
geo: {
type: "MultiLineString",
coordinates: [
[
[172.803869, -43.592789],
[172.659335, -43.620348],
[172.684038, -43.636528],
[172.820922, -43.605325]
],
[
[172.830497, -43.607768],
[172.813263, -43.656319],
[172.823096, -43.660996],
[172.850943, -43.607609]
],
[
[172.912056, -43.623148],
[172.887696, -43.670897],
[172.900469, -43.676178],
[172.931735, -43.622839]
]
]
}
},
{
name: "multi polygon: new zealand north and south islands",
geo: {
type: "MultiPolygon",
coordinates: [
[[
[165.773255, -45.902933],
[169.398419, -47.261538],
[174.672744, -41.767722],
[172.288845, -39.897992],
[165.773255, -45.902933]
]],
[[
[173.166448, -39.778262],
[175.342744, -42.677333],
[179.913373, -37.224362],
[171.475953, -32.688871],
[173.166448, -39.778262]
]]
]
}
},
{
name: "geometry collection: point in Australia and triangle around Australia",
geo: {
type: "GeometryCollection",
geometries: [
{name: "center of Australia", type: "Point", coordinates: [133.985885, -27.240790]},
{
name: "Triangle around Australia",
type: "Polygon",
coordinates: [[
[97.423178, -44.735405],
[169.845050, -38.432287],
[143.824366, 15.966509],
[97.423178, -44.735405]
]]
}
]
}
}
];
// Test various polygons which are not queryable
const badPolys = [
{
name: "Polygon with bad CRS",
type: "Polygon",
coordinates: [[
[114.0834046, 22.6648202],
[113.8293457, 22.3819359],
[114.2736054, 22.4047911],
[114.0834046, 22.6648202]
]],
crs: badCRS
},
{
name: "Open polygon < 3 sides",
type: "Polygon",
coordinates: [[[114.0834046, 22.6648202], [113.8293457, 22.3819359]]],
crs: strictCRS
},
{
name: "Open polygon > 3 sides",
type: "Polygon",
coordinates: [[
[114.0834046, 22.6648202],
[113.8293457, 22.3819359],
[114.2736054, 22.4047911],
[114.1, 22.5]
]],
crs: strictCRS
},
{
name: "duplicate non-adjacent points",
type: "Polygon",
coordinates: [[
[114.0834046, 22.6648202],
[113.8293457, 22.3819359],
[114.2736054, 22.4047911],
[113.8293457, 22.3819359],
[-65.9165954, 22.6648202],
[114.0834046, 22.6648202]
]],
crs: strictCRS
},
{
name: "One hole in polygon",
type: "Polygon",
coordinates: [
[[-80.0, 30.0], [-40.0, 30.0], [-40.0, 60.0], [-80.0, 60.0], [-80.0, 30.0]],
[[-70.0, 40.0], [-60.0, 40.0], [-60.0, 50.0], [-70.0, 50.0], [-70.0, 40.0]]
],
crs: strictCRS
},
{
name: "2 holes in polygon",
type: "Polygon",
coordinates: [
[[-80.0, 30.0], [-40.0, 30.0], [-40.0, 60.0], [-80.0, 60.0], [-80.0, 30.0]],
[[-70.0, 40.0], [-60.0, 40.0], [-60.0, 50.0], [-70.0, 50.0], [-70.0, 40.0]],
[[-55.0, 40.0], [-45.0, 40.0], [-45.0, 50.0], [-55.0, 50.0], [-55.0, 40.0]]
],
crs: strictCRS
},
{
name: "complex polygon (edges cross)",
type: "Polygon",
coordinates: [[[10.0, 10.0], [20.0, 10.0], [10.0, 20.0], [20.0, 20.0], [10.0, 10.0]]],
crs: strictCRS
}
];
// Closed polygons used in query (3, 4, 5, 6-sided)
const polys = [
{
name: "3 sided closed polygon",
type: "Polygon", // triangle
coordinates: [[[10.0, 10.0], [20.0, 10.0], [15.0, 17.0], [10.0, 10.0]]],
crs: strictCRS,
nW: 0,
nI: 1
},
{
name: "3 sided closed polygon (non-big)",
type: "Polygon", // triangle
coordinates: [[[10.0, 10.0], [20.0, 10.0], [15.0, 17.0], [10.0, 10.0]]],
nW: 0,
nI: 1
},
{
name: "4 sided closed polygon",
type: "Polygon", // rectangle
coordinates: [[[10.0, 10.0], [20.0, 10.0], [20.0, 20.0], [10.0, 20.0], [10.0, 10.0]]],
crs: strictCRS,
nW: 0,
nI: 1
},
{
name: "4 sided closed polygon (non-big)",
type: "Polygon", // rectangle
coordinates: [[[10.0, 10.0], [20.0, 10.0], [20.0, 20.0], [10.0, 20.0], [10.0, 10.0]]],
nW: 0,
nI: 1
},
{
name: "5 sided closed polygon",
type: "Polygon", // pentagon
coordinates:
[[[10.0, 10.0], [20.0, 10.0], [25.0, 18.0], [15.0, 25.0], [5.0, 18.0], [10.0, 10.0]]],
crs: strictCRS,
nW: 0,
nI: 1
},
{
name: "5 sided closed polygon (non-big)",
type: "Polygon", // pentagon
coordinates:
[[[10.0, 10.0], [20.0, 10.0], [25.0, 18.0], [15.0, 25.0], [5.0, 18.0], [10.0, 10.0]]],
nW: 0,
nI: 1
},
{
name: "6 sided closed polygon",
type: "Polygon", // hexagon
coordinates: [[
[10.0, 10.0],
[15.0, 10.0],
[22.0, 15.0],
[15.0, 20.0],
[10.0, 20.0],
[7.0, 15.0],
[10.0, 10.0]
]],
crs: strictCRS,
nW: 0,
nI: 1
},
{
name: "6 sided closed polygon (non-big)",
type: "Polygon", // hexagon
coordinates: [[
[10.0, 10.0],
[15.0, 10.0],
[22.0, 15.0],
[15.0, 20.0],
[10.0, 20.0],
[7.0, 15.0],
[10.0, 10.0]
]],
nW: 0,
nI: 1
}
];
// helper function to create n-sided polygon
function nGonGenerator(N, D, clockwise, LON, LAT) {
// compute N points on a circle centered at LAT,LON
// with diameter = D
// and lat*lat + lon*lon = (D/2)*(D/2)
// lat range is -10 to +10
// lon = sqrt( (D/2)*(D/2) - lat*lat )
// N = number of edges
// N must be even!
// edge lengths will be uneven with this quick & dirty approach
N = (N % 2 == 1) ? N + 1 : N;
let eps = 2 * D / N;
let lat = 0;
let lon = 0;
let pts = [];
let i = 0;
// produce longitude values in pairs
// traverse with left foot outside the circle (clockwise) to define the big polygon
for (i = 0, lat = D / 2; i <= N / 2; ++i, lat -= eps) {
if (lat < (-D / 2)) {
// set fixing lat
lat = (-D / 2);
}
lon = Math.sqrt((D / 2) * (D / 2) - (lat * lat));
newlat = lat + LAT;
newlon = lon + LON;
conjugateLon = LON - lon;
pts[i] = [newlon, newlat];
pts[N - i] = [conjugateLon, newlat];
}
// Reverse points if counterclockwise
if (!clockwise) {
pts = pts.reverse();
}
// ensure we connected the dots
assert(tojson(pts[0]) == tojson(pts[N]));
return pts;
}
// helper function to return number of valid objects
function getNumberOfValidObjects(objects) {
let i = 0;
objects.forEach(function(o) {
// strictCRS cannot be stored
if (!o.geo.crs || o.geo.crs != strictCRS) {
i++;
}
});
return i;
}
let totalObjects = getNumberOfValidObjects(objects);
// test various n-sided polygons in query, (n: <number of sides>, d: <diameter>)
// Try them in both clockwise & counterclockwise order
const nsidedPolys = [
// Big Polygon centered on 0, 0
{
name: "4 sided polygon centered on 0, 0",
type: "Polygon",
coordinates: [nGonGenerator(4, 30, true, 0, 0)],
crs: strictCRS,
nW: totalObjects - 3,
nI: totalObjects
},
// Non-big polygons have counterclockwise coordinates
{
name: "4 sided polygon centered on 0, 0 (non-big)",
type: "Polygon",
coordinates: [nGonGenerator(4, 30, false, 0, 0)],
nW: 0,
nI: 3
},
{
name: "100 sided polygon centered on 0, 0",
type: "Polygon",
coordinates: [nGonGenerator(100, 20, true, 0, 0)],
crs: strictCRS,
nW: totalObjects - 3,
nI: totalObjects
},
{
name: "100 sided polygon centered on 0, 0 (non-big)",
type: "Polygon",
coordinates: [nGonGenerator(100, 20, false, 0, 0)],
nW: 0,
nI: 3
},
{
name: "5000 sided polygon centered on 0, 0 (non-big)",
type: "Polygon",
coordinates: [nGonGenerator(5000, 89.99, false, 0, 0)],
nW: 0,
nI: 3
},
{
name: "25000 sided polygon centered on 0, 0",
type: "Polygon",
coordinates: [nGonGenerator(25000, 89.99, true, 0, 0)],
crs: strictCRS,
nW: totalObjects - 3,
nI: totalObjects
},
// Big polygon centered on Shenzen
{
name: "4 sided polygon centered on Shenzen",
type: "Polygon",
coordinates: [nGonGenerator(4, 5, true, 114.1, 22.55)],
crs: strictCRS,
nW: totalObjects - 3,
nI: totalObjects - 2
},
{
name: "4 sided polygon centered on Shenzen (non-big)",
type: "Polygon",
coordinates: [nGonGenerator(4, 5, false, 114.1, 22.55)],
crs: strictCRS,
nW: 2,
nI: 3
}
];
// Populate with 2dsphere index
assert.commandWorked(coll.ensureIndex({geo: "2dsphere"}), "create 2dsphere index");
// Insert objects into collection
objects.forEach(function(o) {
// strictCRS objects cannot be stored
if (o.geo.crs && o.geo.crs == strictCRS) {
assert.writeError(coll.insert(o), "insert " + o.name);
} else {
assert.commandWorked(coll.insert(o), "insert " + o.name);
}
});
// Try creating other index types
assert.commandWorked(coll.ensureIndex({geo: "2dsphere", a: 1}), "compound index, geo");
// These other index types will fail because of the GeoJSON documents
assert.commandFailed(coll.ensureIndex({geo: "2dsphere", a: "text"}), "compound index, geo & text");
assert.commandFailed(coll.ensureIndex({geo: "2d"}), "2d index");
totalObjects = coll.count();
// Test with none & 2dsphere index
const indexes = ["none", "2dsphere"];
indexes.forEach(function(index) {
// Reset indexes on collection
assert.commandWorked(coll.dropIndexes(), "drop indexes");
if (index != "none") {
// Create index
assert.commandWorked(coll.ensureIndex({geo: index}), "create " + index + " index");
}
// These polygons should not be queryable
badPolys.forEach(function(p) {
// within
assert.throws(function() {
coll.count({geo: {$geoWithin: {$geometry: p}}});
}, [], "within " + p.name);
// intersection
assert.throws(function() {
coll.count({geo: {$geoIntersects: {$geometry: p}}});
}, [], "intersects " + p.name);
});
// Tests for closed polygons
polys.forEach(function(p) {
// geoWithin query
let docArray = [];
let q = {geo: {$geoWithin: {$geometry: p}}};
// Test query in aggregate
docArray = coll.aggregate({$match: q}).toArray();
assert.eq(p.nW, docArray.length, "aggregate within " + p.name);
docArray = coll.find(q).toArray();
assert.eq(p.nW, docArray.length, "within " + p.name);
// geoIntersects query
q = {geo: {$geoIntersects: {$geometry: p}}};
// Test query in aggregate
docArray = coll.aggregate({$match: q}).toArray();
assert.eq(p.nI, docArray.length, "aggregate intersects " + p.name);
docArray = coll.find(q).toArray();
assert.eq(p.nI, docArray.length, p.name + " intersects");
// Update on matching docs
let result = coll.update(q, {$set: {stored: ObjectId()}}, {multi: true});
// only check nModified if write commands are enabled
if (coll.getMongo().writeMode() == "commands") {
assert.eq(p.nI, result.nModified, "update " + p.name);
}
// Remove & restore matching docs
assert.eq(p.nI, coll.remove(q).nRemoved, "remove " + p.name);
let bulk = coll.initializeUnorderedBulkOp();
docArray.forEach(function(doc) {
bulk.insert(doc);
});
assert.eq(docArray.length, bulk.execute().nInserted, "reinsert " + p.name);
});
// test the n-sided closed polygons
nsidedPolys.forEach(function(p) {
// within
assert.eq(p.nW, coll.count({geo: {$geoWithin: {$geometry: p}}}), "within " + p.name);
// intersects
assert.eq(
p.nI, coll.count({geo: {$geoIntersects: {$geometry: p}}}), "intersection " + p.name);
});
});
})();
|