# Code coverage report for browser/rtree.js

## Statements: 67.83% (291 / 429) Branches: 59.6% (118 / 198) Functions: 55.81% (24 / 43) Lines: 67.83% (291 / 429)

All files » browser/ » rtree.js

    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
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    9021

    1

    1
    1

    1

    1

    1
    50

    1

    12
    12
    12
    1
    1

    12
    12

    12

    12

    12

    12

    12

    288
    288

    288
    288

    12
    1
    1
    1
    1

    1

    1

    1
    1

    1
    1
    1

    1
    1
    2
    2
    1

    1

    1

    1
    1
    1
    1

    1

    1

    1

    1

    12
    33
    33
    33

    33

    33
    33

    33
    44
    11
    11
    11

    44
    57
    57

    33
    33

    24

    24
    24

    24

    24
    11
    11

    33

    12
    6
    6
    30

    6

    12

    30
    30
    30
    30
    30

    30
    90
    90
    90
    90
    90
    90
    90

    90
    90
    90
    90
    90
    90

    90
    79
    79
    79

    30
    30
    4
    4
    26

    26
    26

    12
    6
    6
    6
    6
    6

    6
    36
    36
    6
    30
    27

    36
    6
    30
    27

    6
    6
    6

    6

    6
    6

    6

    12
    1
    1
    1
    1
    1
    1

    12
    9
    9
    3

    6

    6

    6
    7

    7
    13
    13
    7
    1
    6
    6
    5

    1

    6

    12
    1
    1

    1

    1

    1
    3

    3
    9
    9
    7
    2
    5
    5
    5

    1

    12
    43

    43
    10
    10
    10
    10
    10
    10

    33
    33

    33

    47
    3
    3
    3
    6
    3
    3

    44

    47

    39
    3
    6

    3

    36
    36

    39
    33

    6
    6
    6
    3
    3
    3

    8
    8

    12
    2

    12

    1

    1

    1
    1

    1

    1

    12
    8
    8

    8

    8

    8

    8

    8
    8

    12
    1
    1

    1

    1

    1

    1

    1
    1

    12
    1

    1

    1

    1

    1

    1

    1
    1

    12
    43
    43
    1
    1

    42

    43

    43

    43

    1

    1
    35

    1

    1
    80

    80
    74

    6

    80
    74

    6

    80
    23

    57

    80
    23

    57

    80
    80

    80

    1
    1

    1

    1
    1
    1
    1

    1

    1

    1

    1
     (function (root, factory) {

      // Node.
      Iif(typeof module === 'object' && typeof module.exports === 'object') {
        exports = module.exports = factory(require("terraformer"));
      }

      Eif(typeof root.navigator === "object") {
        Iif (!root.Terraformer){
          throw new Error("Terraformer.RTree requires the core Terraformer library. https://github.com/esri/Terraformer");
        }
        root.Terraformer.RTree = factory(root.Terraformer).RTree;
      }

    }(this, function(Terraformer) {
      var exports = { };

      function isArray(obj) {
        return Object.prototype.toString.call(obj) === '[object Array]';
      }

      /******************************************************************************
     rtree.js - General-Purpose Non-Recursive Javascript R-Tree Library
     Version 0.6.2, December 5st 2009
     Copyright (c) 2009 Jon-Carlos Rivera
     Permission is hereby granted, free of charge, to any person obtaining
     a copy of this software and associated documentation files (the
     "Software"), to deal in the Software without restriction, including
     without limitation the rights to use, copy, modify, merge, publish,
     distribute, sublicense, and/or sell copies of the Software, and to
     permit persons to whom the Software is furnished to do so, subject to
     the following conditions:
     The above copyright notice and this permission notice shall be
     included in all copies or substantial portions of the Software.
     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     Jon-Carlos Rivera - imbcmdth@hotmail.com
     ******************************************************************************/

    /*
     * RTree - A simple r-tree structure for great results.
     * @constructor
     */
    var RTree = function (width) {
        // Variables to control tree-dimensions
        var _Min_Width = 3; // Minimum width of any node before a merge
        var _Max_Width = 6; // Maximum width of any node before a split
        if (!isNaN(width)) {
          _Min_Width = Math.floor(width / 2.0);
          _Max_Width = width;
        }

        this.min_width = _Min_Width;
        this.max_width = _Max_Width;

        // Start with an empty root-tree
        var _T = {
          x: 0,
          y: 0,
          w: 0,
          h: 0,
          id: "root",
          nodes: []
        };

       /* @function
        * @description Function to generate unique strings for element IDs
        * @param {String} n      The prefix to use for the IDs generated.
        * @return {String}        A guarenteed unique ID.
        */
        var _name_to_id = (function() {
          // hide our idCache inside this closure
          var idCache = {};

          // return the api: our function that returns a unique string with incrementing number appended to given idPrefix
          return function(idPrefix) {
            var idVal = 0;
            if (idPrefix in idCache) {
              idVal = idCache[idPrefix]++;
            } else {
              idCache[idPrefix] = 0;
            }
            return idPrefix + "_" + idVal;
          };
        })();

        // This is my special addition to the world of r-trees
        // every other (simple) method I found produced crap trees
        // this skews insertions to prefering squarer and emptier nodes
        RTree.Rectangle.squarified_ratio = function(l, w, fill) {
          // Area of new enlarged rectangle
          var lperi = (l + w) / 2.0; // Average size of a side of the new rectangle
          var larea = l * w; // Area of new rectangle
          // return the ratio of the perimeter to the area - the closer to 1 we are,
          // the more "square" a rectangle is. conversly, when approaching zero the
          // more elongated a rectangle is
          var lgeo = larea / (lperi * lperi);
          return (larea * fill / lgeo);
        };

       /* find the best specific node(s) for object to be deleted from
        * [ leaf node parent ] = _remove_subtree(rectangle, object, root)
        * @private
        */
        var _remove_subtree = function(rect, obj, root) {
            var hit_stack = []; // Contains the elements that overlap
            var count_stack = []; // Contains the elements that overlap
            var ret_array = [];
            var current_depth = 1;

            Iif (!rect || !RTree.Rectangle.overlap_rectangle(rect, root)) {
              return ret_array;
            }

            var ret_obj = {
              x: rect.x,
              y: rect.y,
              w: rect.w,
              h: rect.h,
              target: obj
            };

            count_stack.push(root.nodes.length);
            hit_stack.push(root);

            do {
              var tree = hit_stack.pop();
              var i = count_stack.pop() - 1;

              Eif ("target" in ret_obj) { // We are searching for a target
                while (i >= 0) {
                  var ltree = tree.nodes[i];
                  if (RTree.Rectangle.overlap_rectangle(ret_obj, ltree)) {
                    Eif ((ret_obj.target && "leaf" in ltree && ltree.leaf === ret_obj.target) || (!ret_obj.target && ("leaf" in ltree || RTree.Rectangle.contains_rectangle(ltree, ret_obj)))) { // A Match !!
                      // Yup we found a match...
                      // we can cancel search and start walking up the list
                      Iif ("nodes" in ltree) { // If we are deleting a node not a leaf...
                        ret_array = _search_subtree(ltree, true, [], ltree);
                        tree.nodes.splice(i, 1);
                      } else {
                        ret_array = tree.nodes.splice(i, 1);
                      }
                      // Resize MBR down...
                      RTree.Rectangle.make_MBR(tree.nodes, tree);
                      delete ret_obj.target;
                      Eif (tree.nodes.length < _Min_Width) { // Underflow
                        ret_obj.nodes = _search_subtree(tree, true, [], tree);
                      }
                      break;
                    }
                    /*  else if("load" in ltree) { // A load
                    }*/
                    else if ("nodes" in ltree) { // Not a Leaf
                      current_depth += 1;
                      count_stack.push(i);
                      hit_stack.push(tree);
                      tree = ltree;
                      i = ltree.nodes.length;
                    }
                  }
                  i -= 1;
                }
              } else if ("nodes" in ret_obj) { // We are unsplitting
                tree.nodes.splice(i + 1, 1); // Remove unsplit node
                // ret_obj.nodes contains a list of elements removed from the tree so far
                if (tree.nodes.length > 0) {
                  RTree.Rectangle.make_MBR(tree.nodes, tree);
                }
                for (var t = 0; t < ret_obj.nodes.length; t++) {
                  _insert_subtree(ret_obj.nodes[t], tree);
                }
                ret_obj.nodes.length = 0;
                if (hit_stack.length === 0 && tree.nodes.length <= 1) { // Underflow..on root!
                  ret_obj.nodes = _search_subtree(tree, true, ret_obj.nodes, tree);
                  tree.nodes.length = 0;
                  hit_stack.push(tree);
                  count_stack.push(1);
                } else if (hit_stack.length > 0 && tree.nodes.length < _Min_Width) { // Underflow..AGAIN!
                  ret_obj.nodes = _search_subtree(tree, true, ret_obj.nodes, tree);
                  tree.nodes.length = 0;
                } else {
                  delete ret_obj.nodes; // Just start resizing
                }
              } else { // we are just resizing
                RTree.Rectangle.make_MBR(tree.nodes, tree);
              }
              current_depth -= 1;
            } while (hit_stack.length > 0);

            return (ret_array);
            };

       /* choose the best damn node for rectangle to be inserted into
        * [ leaf node parent ] = _choose_leaf_subtree(rectangle, root to start search at)
        * @private
        */
        var _choose_leaf_subtree = function(rect, root) {
            var best_choice_index = -1;
            var best_choice_stack = [];
            var best_choice_area;

            var load_callback = function(local_tree, local_node) {
                return function(data) {
                  local_tree._attach_data(local_node, data);
                };
            };

            best_choice_stack.push(root);
            var nodes = root.nodes;

            do {
              if (best_choice_index !== -1) {
                best_choice_stack.push(nodes[best_choice_index]);
                nodes = nodes[best_choice_index].nodes;
                best_choice_index = -1;
              }

              for (var i = nodes.length - 1; i >= 0; i--) {
                var ltree = nodes[i];
                if ("leaf" in ltree) {
                  // Bail out of everything and start inserting
                  best_choice_index = -1;
                  break;
                }

                // Area of new enlarged rectangle
                var old_lratio = RTree.Rectangle.squarified_ratio(ltree.w, ltree.h, ltree.nodes.length + 1);

                // Enlarge rectangle to fit new rectangle
                var nw = Math.max(ltree.x + ltree.w, rect.x + rect.w) - Math.min(ltree.x, rect.x);
                var nh = Math.max(ltree.y + ltree.h, rect.y + rect.h) - Math.min(ltree.y, rect.y);

                // Area of new enlarged rectangle
                var lratio = RTree.Rectangle.squarified_ratio(nw, nh, ltree.nodes.length + 2);

                if (best_choice_index < 0 || Math.abs(lratio - old_lratio) < best_choice_area) {
                  best_choice_area = Math.abs(lratio - old_lratio);
                  best_choice_index = i;
                }
              }
            } while (best_choice_index !== -1);

            return (best_choice_stack);
            };

       /* split a set of nodes into two roughly equally-filled nodes
        * [ an array of two new arrays of nodes ] = linear_split(array of nodes)
        * @private
        */
        var _linear_split = function(nodes) {
            var n = _pick_linear(nodes);
            while (nodes.length > 0) {
              _pick_next(nodes, n[0], n[1]);
            }
            return (n);
            };

       /* insert the best source rectangle into the best fitting parent node: a or b
        * [] = pick_next(array of source nodes, target node array a, target node array b)
        * @private
        */
        var _pick_next = function(nodes, a, b) {
            // Area of new enlarged rectangle
            var area_a = RTree.Rectangle.squarified_ratio(a.w, a.h, a.nodes.length + 1);
            var area_b = RTree.Rectangle.squarified_ratio(b.w, b.h, b.nodes.length + 1);
            var high_area_delta;
            var high_area_node;
            var lowest_growth_group;

            for (var i = nodes.length - 1; i >= 0; i--) {
              var l = nodes[i];
              var new_area_a = {};
              new_area_a.x = Math.min(a.x, l.x);
              new_area_a.y = Math.min(a.y, l.y);
              new_area_a.w = Math.max(a.x + a.w, l.x + l.w) - new_area_a.x;
              new_area_a.h = Math.max(a.y + a.h, l.y + l.h) - new_area_a.y;
              var change_new_area_a = Math.abs(RTree.Rectangle.squarified_ratio(new_area_a.w, new_area_a.h, a.nodes.length + 2) - area_a);

              var new_area_b = {};
              new_area_b.x = Math.min(b.x, l.x);
              new_area_b.y = Math.min(b.y, l.y);
              new_area_b.w = Math.max(b.x + b.w, l.x + l.w) - new_area_b.x;
              new_area_b.h = Math.max(b.y + b.h, l.y + l.h) - new_area_b.y;
              var change_new_area_b = Math.abs(RTree.Rectangle.squarified_ratio(new_area_b.w, new_area_b.h, b.nodes.length + 2) - area_b);

              if (!high_area_node || !high_area_delta || Math.abs(change_new_area_b - change_new_area_a) < high_area_delta) {
                high_area_node = i;
                high_area_delta = Math.abs(change_new_area_b - change_new_area_a);
                lowest_growth_group = change_new_area_b < change_new_area_a ? b : a;
              }
            }
            var temp_node = nodes.splice(high_area_node, 1)[0];
            if (a.nodes.length + nodes.length + 1 <= _Min_Width) {
              a.nodes.push(temp_node);
              RTree.Rectangle.expand_rectangle(a, temp_node);
            } else Iif (b.nodes.length + nodes.length + 1 <= _Min_Width) {
              b.nodes.push(temp_node);
              RTree.Rectangle.expand_rectangle(b, temp_node);
            } else {
              lowest_growth_group.nodes.push(temp_node);
              RTree.Rectangle.expand_rectangle(lowest_growth_group, temp_node);
            }
            };

       /* pick the "best" two starter nodes to use as seeds using the "linear" criteria
        * [ an array of two new arrays of nodes ] = pick_linear(array of source nodes)
        * @private
        */
        var _pick_linear = function(nodes) {
            var lowest_high_x = nodes.length - 1;
            var highest_low_x = 0;
            var lowest_high_y = nodes.length - 1;
            var highest_low_y = 0;
            var t1, t2;

            for (var i = nodes.length - 2; i >= 0; i--) {
              var l = nodes[i];
              if (l.x > nodes[highest_low_x].x) {
                highest_low_x = i;
              } else if (l.x + l.w < nodes[lowest_high_x].x + nodes[lowest_high_x].w) {
                lowest_high_x = i;
              }
              if (l.y > nodes[highest_low_y].y) {
                highest_low_y = i;
              } else if (l.y + l.h < nodes[lowest_high_y].y + nodes[lowest_high_y].h) {
                lowest_high_y = i;
              }
            }
            var dx = Math.abs((nodes[lowest_high_x].x + nodes[lowest_high_x].w) - nodes[highest_low_x].x);
            var dy = Math.abs((nodes[lowest_high_y].y + nodes[lowest_high_y].h) - nodes[highest_low_y].y);
            Iif (dx > dy) {
              if (lowest_high_x > highest_low_x) {
                t1 = nodes.splice(lowest_high_x, 1)[0];
                t2 = nodes.splice(highest_low_x, 1)[0];
              } else {
                t2 = nodes.splice(highest_low_x, 1)[0];
                t1 = nodes.splice(lowest_high_x, 1)[0];
              }
            } else {
              Iif (lowest_high_y > highest_low_y) {
                t1 = nodes.splice(lowest_high_y, 1)[0];
                t2 = nodes.splice(highest_low_y, 1)[0];
              } else {
                t2 = nodes.splice(highest_low_y, 1)[0];
                t1 = nodes.splice(lowest_high_y, 1)[0];
              }
            }
            return ([{
              x: t1.x,
              y: t1.y,
              w: t1.w,
              h: t1.h,
              nodes: [t1]
            }, {
              x: t2.x,
              y: t2.y,
              w: t2.w,
              h: t2.h,
              nodes: [t2]
            }]);
            };

        var _attach_data = function(node, more_tree) {
            node.nodes = more_tree.nodes;
            node.x = more_tree.x;
            node.y = more_tree.y;
            node.w = more_tree.w;
            node.h = more_tree.h;
            return (node);
        };

       /* non-recursive internal search function
        * [ nodes | objects ] = _search_subtree(rectangle, [return node data], [array to fill], root to begin search at)
        * @private
        */
        var _search_subtree = function(rect, return_node, return_array, root) {
          var hit_stack = []; // Contains the elements that overlap
          if (!RTree.Rectangle.overlap_rectangle(rect, root)) {
            return return_array;
          }

          var load_callback = function(local_tree, local_node) {
              return function(data) {
                local_tree._attach_data(local_node, data);
              };
          };

          hit_stack.push(root.nodes);

          do {
            var nodes = hit_stack.pop();

            for (var i = nodes.length - 1; i >= 0; i--) {
              var ltree = nodes[i];
              if (RTree.Rectangle.overlap_rectangle(rect, ltree)) {
                if ("nodes" in ltree) { // Not a Leaf
                  hit_stack.push(ltree.nodes);
                } else Eif ("leaf" in ltree) { // A Leaf !!
                  if (!return_node) {
                    return_array.push(ltree.leaf);
                  } else {
                    return_array.push(ltree);
                  }
                }
              }
            }
          } while (hit_stack.length > 0);

          return return_array;
        };

        var _search_subtree_reverse = function(rect, return_node, return_array, root) {
          var hit_stack = []; // Contains the elements that overlap
          Iif (!RTree.Rectangle.overlap_rectangle(root, rect)) {
            return return_array;
          }

          var load_callback = function(local_tree, local_node) {
              return function(data) {
                local_tree._attach_data(local_node, data);
              };
          };

          hit_stack.push(root.nodes);

          do {
            var nodes = hit_stack.pop();

            for (var i = nodes.length - 1; i >= 0; i--) {
              var ltree = nodes[i];
              if (RTree.Rectangle.overlap_rectangle(ltree, rect)) {
                if ("nodes" in ltree) { // Not a Leaf
                  hit_stack.push(ltree.nodes);
                } else Eif ("leaf" in ltree) { // A Leaf !!
                  Eif (!return_node) {
                    return_array.push(ltree.leaf);
                  } else {
                    return_array.push(ltree);
                  }
                }
              }
            }
          } while (hit_stack.length > 0);

          return return_array;
        };

       /* non-recursive internal insert function
        * [] = _insert_subtree(rectangle, object to insert, root to begin insertion at)
        * @private
        */
        var _insert_subtree = function(node, root) {
          var bc; // Best Current node
          // Initial insertion is special because we resize the Tree and we don't
          // care about any overflow (seriously, how can the first object overflow?)
          if (root.nodes.length === 0) {
            root.x = node.x;
            root.y = node.y;
            root.w = node.w;
            root.h = node.h;
            root.nodes.push(node);
            return;
          }

          // Find the best fitting leaf node
          // choose_leaf returns an array of all tree levels (including root)
          // that were traversed while trying to find the leaf
          var tree_stack = _choose_leaf_subtree(node, root);
          var ret_obj = node; //{x:rect.x,y:rect.y,w:rect.w,h:rect.h, leaf:obj};
          // Walk back up the tree resizing and inserting as needed
          do {
            //handle the case of an empty node (from a split)
            if (bc && "nodes" in bc && bc.nodes.length === 0) {
              var pbc = bc; // Past bc
              bc = tree_stack.pop();
              for (var t = 0; t < bc.nodes.length; t++) {
                if (bc.nodes[t] === pbc || bc.nodes[t].nodes.length === 0) {
                  bc.nodes.splice(t, 1);
                  break;
                }
              }
            } else {
              bc = tree_stack.pop();
            }

            // If there is data attached to this ret_obj
            if ("leaf" in ret_obj || "nodes" in ret_obj || isArray(ret_obj)) {
              // Do Insert
              if (isArray(ret_obj)) {
                for (var ai = 0; ai < ret_obj.length; ai++) {
                  RTree.Rectangle.expand_rectangle(bc, ret_obj[ai]);
                }
                bc.nodes = bc.nodes.concat(ret_obj);
              } else {
                RTree.Rectangle.expand_rectangle(bc, ret_obj);
                bc.nodes.push(ret_obj); // Do Insert
              }

              if (bc.nodes.length <= _Max_Width) { // Start Resizeing Up the Tree
                ret_obj = {
                  x: bc.x,
                  y: bc.y,
                  w: bc.w,
                  h: bc.h
                };
              } else { // Otherwise Split this Node
                // linear_split() returns an array containing two new nodes
                // formed from the split of the previous node's overflow
                var a = _linear_split(bc.nodes);
                ret_obj = a; //[1];
                if (tree_stack.length < 1) { // If are splitting the root..
                  bc.nodes.push(a[0]);
                  tree_stack.push(bc); // Reconsider the root element
                  ret_obj = a[1];
                }
              }
            } else { // Otherwise Do Resize
              //Just keep applying the new bounding rectangle to the parents..
              RTree.Rectangle.expand_rectangle(bc, ret_obj);
              ret_obj = {
                x: bc.x,
                y: bc.y,
                w: bc.w,
                h: bc.h
              };
            }
          } while (tree_stack.length > 0);
        };

       /* returns a JSON representation of the tree
        * @public
        */
        this.serialize = function(callback) {
          callback(null, _T);
        };

       /* accepts a JSON representation of the tree and inserts it
        * @public
        */
        this.deserialize = function(new_tree, where, callback) {

          var args = Array.prototype.slice.call(arguments);

          switch (args.length) {
          case 1:
            where = _T;
            break;
          case 2:
            if(typeof args[1] === "function"){
              where = _T;
              callback = args[1];
            }
            break;
          }

          var deserialized = _attach_data(where, new_tree);

          Iif(callback){
            callback(null, deserialized);
          }
        };

       /* non-recursive search function
        * [ nodes | objects ] = RTree.search(rectangle, [return node data], [array to fill])
        * @public
        */

        this.search = function(shape, callback) {
          var rect;
          Iif(shape.type){
            var b = Terraformer.Tools.calculateBounds(shape);
            rect = {
              x: b[0],
              y: b[1],
              w: Math.abs(b[0] - b[2]),
              h: Math.abs(b[1] - b[3])
            };
          } else {
            rect = shape;
          }

          var args = [ rect, false, [ ], _T ];

          Iif (rect === undefined) {
            throw "Wrong number of arguments. RT.Search requires at least a bounding rectangle.";
          }

          var results = _search_subtree.apply(this, args);

          Eif(callback){
            callback(null, results);
          }
        };

        this.within = function(shape, callback) {
          var rect;
          Iif(shape.type){
            var b = Terraformer.Tools.calculateBounds(shape);
            rect = {
              x: b[0],
              y: b[1],
              w: Math.abs(b[0] - b[2]),
              h: Math.abs(b[1] - b[3])
            };
          } else {
            rect = shape;
          }

          var args = [ rect, false, [ ], _T ];

          Iif (rect === undefined) {
            throw "Wrong number of arguments. RT.Search requires at least a bounding rectangle.";
          }

          var search = _search_subtree_reverse.apply(this, args);

          Eif(callback){
            callback(null, search);
          }
        };

       /* non-recursive function that deletes a specific
        * [ number ] = RTree.remove(rectangle, obj)
        */
        this.remove = function(shape, obj, callback) {
          var args = Array.prototype.slice.call(arguments);

          // you only passed shape
          Iif(args.length === 1){
            // so make the args (shape, false)
            args.push(false);
          }

          // you passed (shape, obj, callback)
          // pop the callback off the args list
          Iif(args.length === 3){
            callback = args.pop();
          }

          // convert shape (the first arg) to a bbox if its geojson
          Iif(args && args[0] && args[0].type){
            var b = Terraformer.Tools.calculateBounds(args[0]);
            args[0] = {
              x: b[0],
              y: b[1],
              w: Math.abs(b[0] - b[2]),
              h: Math.abs(b[1] - b[3])
            };
          }

          // push a new root node onto the args stack
          args.push(_T);

          Iif(obj === false) { // Do area-wide delete
            var numberdeleted = 0;
            var ret_array = [];
            do {
              numberdeleted = ret_array.length;
              ret_array = ret_array.concat(_remove_subtree.apply(this, args));
            } while( numberdeleted !==  ret_array.length);
            if(callback){
              callback(null, ret_array);
            }
          } else { // Delete a specific item
            var removal = _remove_subtree.apply(this, args);
            Iif(callback){
              callback(null, removal);
            }
          }
        };

       /* non-recursive insert function
        * [] = RTree.insert(rectangle, object to insert)
        */
        this.insert = function(shape, obj, callback) {
          var rect;
          if(shape.type){
            var b = Terraformer.Tools.calculateBounds(shape);
            rect = {
              x: b[0],
              y: b[1],
              w: Math.abs(b[0] - b[2]),
              h: Math.abs(b[1] - b[3])
            };
          } else {
            rect = shape;
          }

          Iif (arguments.length < 2) {
            throw "Wrong number of arguments. RT.Insert requires at least a bounding rectangle or GeoJSON and an object.";
          }

          var insert = _insert_subtree({
            x: rect.x,
            y: rect.y,
            w: rect.w,
            h: rect.h,
            leaf: obj
          }, _T);

          Iif(callback){
            callback(null, insert);
          }
        };

       /* non-recursive delete function
        * [deleted object] = RTree.remove(rectangle, [object to delete])
        */

        //End of RTree
      };

    /* Rectangle - Generic rectangle object - Not yet used */
    RTree.Rectangle = function(ix, iy, iw, ih) { // new Rectangle(bounds) or new Rectangle(x, y, w, h)
      var x, x2, y, y2, w, h;

      if (ix.x) {
        x = ix.x;
        y = ix.y;
        if (ix.w !== 0 && !ix.w && ix.x2) {
          w = ix.x2 - ix.x;
          h = ix.y2 - ix.y;
        } else {
          w = ix.w;
          h = ix.h;
        }
        x2 = x + w;
        y2 = y + h; // For extra fastitude
      } else {
        x = ix;
        y = iy;
        w = iw;
        h = ih;
        x2 = x + w;
        y2 = y + h; // For extra fastitude
      }

      this.x1 = this.x = function() {
        return x;
      };
      this.y1 = this.y = function() {
        return y;
      };
      this.x2 = function() {
        return x2;
      };
      this.y2 = function() {
        return y2;
      };
      this.w = function() {
        return w;
      };
      this.h = function() {
        return h;
      };

      this.toJSON = function() {
        return ('{"x":' + x.toString() + ', "y":' + y.toString() + ', "w":' + w.toString() + ', "h":' + h.toString() + '}');
      };

      this.overlap = function(a) {
        return (this.x() < a.x2() && this.x2() > a.x() && this.y() < a.y2() && this.y2() > a.y());
      };

      this.expand = function(a) {
        var nx = Math.min(this.x(), a.x());
        var ny = Math.min(this.y(), a.y());
        w = Math.max(this.x2(), a.x2()) - nx;
        h = Math.max(this.y2(), a.y2()) - ny;
        x = nx;
        y = ny;
        return (this);
      };

      this.setRect = function(ix, iy, iw, ih) {
        var x, x2, y, y2, w, h;
        if (ix.x) {
          x = ix.x;
          y = ix.y;
          if (ix.w !== 0 && !ix.w && ix.x2) {
            w = ix.x2 - ix.x;
            h = ix.y2 - ix.y;
          } else {
            w = ix.w;
            h = ix.h;
          }
          x2 = x + w;
          y2 = y + h; // For extra fastitude
        } else {
          x = ix;
          y = iy;
          w = iw;
          h = ih;
          x2 = x + w;
          y2 = y + h; // For extra fastitude
        }
      };
      //End of RTree.Rectangle
    };

    /* returns true if rectangle 1 overlaps rectangle 2
     * [ boolean ] = overlap_rectangle(rectangle a, rectangle b)
     * @static function
     */
    RTree.Rectangle.overlap_rectangle = function(a, b) {
      return (a.x < (b.x + b.w) && (a.x + a.w) > b.x && a.y < (b.y + b.h) && (a.y + a.h) > b.y);
    };

    /* returns true if rectangle a is contained in rectangle b
     * [ boolean ] = contains_rectangle(rectangle a, rectangle b)
     * @static function
     */
    RTree.Rectangle.contains_rectangle = function(a, b) {
      return ((a.x + a.w) <= (b.x + b.w) && a.x >= b.x && (a.y + a.h) <= (b.y + b.h) && a.y >= b.y);
    };

    /* expands rectangle A to include rectangle B, rectangle B is untouched
     * [ rectangle a ] = expand_rectangle(rectangle a, rectangle b)
     * @static function
     */
    RTree.Rectangle.expand_rectangle = function(a, b) {
      var nx, ny;

      // unintuitively, this is way way faster than max/min
      if (a.x < b.x) {
        nx = a.x;
      } else {
        nx = b.x;
      }

      if (a.y < b.y) {
        ny = a.y;
      } else {
        ny = b.y;
      }

      if (a.x + a.w > b.x + b.w) {
        a.w = (a.x + a.w) - nx;
      } else {
        a.w = (b.x + b.w) - nx;
      }

      if (a.y + a.h > b.y + b.h) {
        a.h = (a.y + a.h) - ny;
      } else {
        a.h = (b.y + b.h) - ny;
      }

      a.x = nx;
      a.y = ny;

      return (a);
    };

    /* generates a minimally bounding rectangle for all rectangles in
     * array "nodes". If rect is set, it is modified into the MBR. Otherwise,
     * a new rectangle is generated and returned.
     * [ rectangle a ] = make_MBR(rectangle array nodes, rectangle rect)
     * @static function
     */
    RTree.Rectangle.make_MBR = function(nodes, rect) {
      Iif (nodes.length < 1) {
        return ({
          x: 0,
          y: 0,
          w: 0,
          h: 0
        });
      }
      //throw "make_MBR: nodes must contain at least one rectangle!";
      Iif (!rect) {
        rect = {
          x: nodes[0].x,
          y: nodes[0].y,
          w: nodes[0].w,
          h: nodes[0].h
        };
      } else {
        rect.x = nodes[0].x;
        rect.y = nodes[0].y;
        rect.w = nodes[0].w;
        rect.h = nodes[0].h;
      }

      for (var i = nodes.length - 1; i > 0; i--) {
        RTree.Rectangle.expand_rectangle(rect, nodes[i]);
      }

      return (rect);
    };

      exports.RTree = RTree;

      return exports;
    }));