# Code coverage report for browser/terraformer.js

## Statements: 97.5% (662 / 679) Branches: 92.81% (400 / 431) Functions: 97.59% (81 / 83) Lines: 97.5% (662 / 679)

All files » browser/ » terraformer.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
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
    912
    913
    914
    915
    916
    917
    918
    919
    920
    921
    922
    923
    924
    925
    926
    927
    928
    929
    930
    931
    932
    933
    934
    935
    936
    937
    938
    939
    940
    941
    942
    943
    944
    945
    946
    947
    948
    949
    950
    951
    952
    953
    954
    955
    956
    957
    958
    959
    960
    961
    962
    963
    964
    965
    966
    967
    968
    969
    970
    971
    972
    973
    974
    975
    976
    977
    978
    979
    980
    981
    982
    983
    984
    985
    986
    987
    988
    989
    990
    991
    992
    993
    994
    995
    996
    997
    998
    999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080
    1081
    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171
    1172
    1173
    1174
    1175
    1176
    1177
    1178
    1179
    1180
    1181
    1182
    1183
    1184
    1185
    1186
    1187
    1188
    1189
    1190
    1191
    1192
    1193
    1194
    1195
    1196
    1197
    1198
    1199
    1200
    1201
    1202
    1203
    1204
    1205
    1206
    1207
    1208
    1209
    1210
    1211
    1212
    1213
    1214
    1215
    1216
    1217
    1218
    1219
    1220
    1221
    1222
    1223
    1224
    1225
    1226
    1227
    1228
    1229
    1230
    1231
    1232
    1233
    1234
    1235
    1236
    1237
    1238
    1239
    1240
    1241
    1242
    1243
    1244
    1245
    1246
    1247
    1248
    1249
    1250
    1251
    1252
    1253
    1254
    1255
    1256
    1257
    1258
    1259
    1260
    1261
    1262
    1263
    1264
    1265
    1266
    1267
    1268
    1269
    1270
    1271
    1272
    1273
    1274
    1275
    1276
    1277
    1278
    1279
    1280
    1281
    1282
    1283
    1284
    1285
    1286
    1287
    1288
    1289
    1290
    1291
    1292
    1293
    1294
    1295
    1296
    1297
    1298
    1299
    1300
    1301
    1302
    1303
    1304
    1305
    1306
    1307
    1308
    1309
    1310
    1311
    1312
    1313
    1314
    1315
    1316
    1317
    1318
    1319
    1320
    1321
    1322
    1323
    1324
    1325
    1326
    1327
    1328
    1329
    1330
    1331
    1332
    1333
    1334
    1335
    1336
    1337
    1338
    1339
    1340
    1341
    1342
    1343
    1344
    1345
    1346
    1347
    1348
    1349
    1350
    1351
    1352
    1353
    1354
    1355
    1356
    1357
    1358
    1359
    1360
    1361
    1362
    1363
    1364
    1365
    1366
    1367
    1368
    1369
    1370
    1371
    1372
    1373
    1374
    1375
    1376
    1377
    1378
    1379
    1380
    1381
    1382
    1383
    1384
    1385
    1386
    1387
    1388
    1389
    1390
    1391
    1392
    1393
    1394
    1395
    1396
    1397
    1398
    1399
    1400
    1401
    1402
    1403
    1404
    1405
    1406
    1407
    14081

    1

    1
    1

    1

    1

    1
    75
    564
    181

    75

    1

    57

    6

    6

    4

    3

    20

    3

    7

    3

    4

    1

    1
    23

    23
    31

    31
    808

    808
    808

    808
    23
    785
    175

    808
    23
    785
    266

    808
    23
    785
    125

    808
    23
    785
    240

    23

    1
    3

    3
    6

    6
    7
    7
    220

    220
    220

    220
    3
    217
    3

    220
    3
    217
    119

    220
    3
    217
    11

    220
    3
    217
    82

    3

    1
    17

    17
    71
    71
    71

    71
    17
    54
    11

    71
    17
    54
    39

    71
    17
    54
    21

    71
    17
    54
    23

    17

    1
    3
    3
    5
    5
    5

    3

    1
    4

    4
    8
    8
    8

    4

    1
    10
    10

    1
    5085

    1
    135

    1
    1764

    5164
    1712

    5164
    1740

    1764

    1
    1695
    1695
    1695

    1
    45
    45
    45

    1
    48
    14
    34
    6
    28
    2
    4

    26
    2
    4

    24

    48
    34
    10

    48
    31

    48

    1
    10

    1
    24

    1
    118
    20
    98
    52

    46

    1
    345
    100
    245
    156

    89

    1

    118

    1

    92
    92

    92

    1

    19
    19
    118
    118
    40

    19

    1

    7
    1
    6
    1

    5

    5
    19

    19
    14

    5

    1
    70
    70
    318

    41

    70

    1
    69
    68
    65

    3
    2
    2
    1

    1

    1

    1

    1
    172
    172
    172

    172
    95
    95

    95
    24

    148

    1
    3
    3
    3
    1

    2

    1
    36
    38

    38
    64
    169
    23

    13

    1
    15
    16
    10

    5

    1
    14
    14
    9

    5

    1
    11
    11
    8

    3

    1
    2
    2
    1

    1

    1
    29

    29
    29

    29
    22

    29

    29

    1
    43
    54
    98
    32

    11

    1
    28
    1

    27
    27

    27
    46
    1

    45
    137
    21

    5

    1

    1
    61
    51

    3

    1

    7

    5

    22

    8

    1

    1

    2

    1

    1

    1

    1

    10

    10

    11
    11
    2
    1

    1

    9
    3
    2

    1

    6
    3
    2
    3

    1

    3
    2
    1
    2
    2

    1

    1

    5

    16
    16
    219
    38

    16
    16

    1
    114

    114
    2
    2

    112
    9
    1
    1

    1
    1

    111
    19
    4
    13
    1

    12
    3

    107

    59

    20
    20
    20
    4

    16
    9

    7

    39
    4

    35
    22
    1

    21
    39
    13

    8

    13
    4
    5

    5
    3
    3

    1

    9
    9
    12

    12
    7

    2

    48

    25
    2
    2
    4
    4
    1

    1

    23
    6
    6
    6
    6
    1

    5
    2
    2
    4
    4
    3

    1

    2

    17
    9
    9

    9
    4

    5

    8
    3
    4

    4
    2

    1

    5
    5
    5

    5
    4

    1

    26

    1

    24

    24
    24
    1

    23
    9
    3
    6
    2
    4
    2
    2
    2

    14
    4
    1
    3
    2
    1
    1

    10
    6
    1
    5
    1
    4
    2
    2
    2

    4
    4
    1
    3
    1
    2
    2

    1
    29

    29
    3
    26
    18
    8
    7

    1

    28

    1
    1

    1
    18
    1
    17
    16

    1

    17

    1
    1
    1
    1
    2

    1

    1
    1
    1

    1
    1
    1

    1
    2
    1

    1

    2

    1
    2

    1
    48
    7
    41
    40

    1

    47

    1
    1
    1
    1
    1

    1
    1
    1

    1
    1
    1

    1
    27
    5
    22
    21

    1

    26

    1
    1
    1
    1
    2

    1
    2

    1
    68
    22
    46
    45

    1

    67

    1
    1
    1
    1
    1

    1
    1
    1

    1
    1
    1

    1

    1
    41
    8
    33
    32

    1

    40

    1
    1
    1
    1
    2

    1
    2

    1
    19
    15
    4
    4

    19

    1
    1

    1
    6
    1
    5
    5

    6

    1
    1
    1
    2
    4

    1
    2
    2
    4
    2

    2

    1
    8
    2
    6
    6

    8

    1
    1
    1
    1
    2

    1
    2

    1
    14
    14
    14

    14
    1664
    1664

    14

    1
    12
    12

    12
    1

    11

    1
    1
    1
    3
    3

    1
    2
    1
    1

    2

    1
    2
    1
    1

    2

    1
    2
    1
    1

    2

    1
    1
    1

    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1

    1
    1

    1
    1
    1
    1
    1
    1
    1

    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();
      }

      // Browser Global.
      Eif(typeof window === "object") {
        root.Terraformer = factory();
      }

    }(this, function(){
      var exports = {},
          EarthRadius = 6378137,
          DegreesPerRadian = 57.295779513082320,
          RadiansPerDegree =  0.017453292519943,
          MercatorCRS = {
            "type": "link",
            "properties": {
              "href": "http://spatialreference.org/ref/sr-org/6928/ogcwkt/",
              "type": "ogcwkt"
            }
          },
          GeographicCRS = {
            "type": "link",
            "properties": {
              "href": "http://spatialreference.org/ref/epsg/4326/ogcwkt/",
              "type": "ogcwkt"
            }
          };

      /*
      Internal: safe warning
      */
      function warn() {
        var args = Array.prototype.slice.apply(arguments);

        if (typeof console !== undefined && console.warn) {
          console.warn.apply(console, args);
        }
      }

      /*
      Internal: Extend one object with another.
      */
      function extend(destination, source) {
        for (var k in source) {
          if (source.hasOwnProperty(k)) {
            destination[k] = source[k];
          }
        }
        return destination;
      }

      /*
      Public: Calculate an bounding box for a geojson object
      */
      function calculateBounds (geojson) {

        switch (geojson.type) {
          case 'Point':
            return [ geojson.coordinates[0], geojson.coordinates[1], geojson.coordinates[0], geojson.coordinates[1]];

          case 'MultiPoint':
            return calculateBoundsFromArray(geojson.coordinates);

          case 'LineString':
            return calculateBoundsFromArray(geojson.coordinates);

          case 'MultiLineString':
            return calculateBoundsFromNestedArrays(geojson.coordinates);

          case 'Polygon':
            return calculateBoundsFromNestedArrays(geojson.coordinates);

          case 'MultiPolygon':
            return calculateBoundsFromNestedArrayOfArrays(geojson.coordinates);

          case 'Feature':
            return calculateBounds(geojson.geometry);

          case 'FeatureCollection':
            return calculateBoundsForFeatureCollection(geojson);

          case 'GeometryCollection':
            return calculateBoundsForGeometryCollection(geojson);

          default:
            throw new Error("Unknown type: " + geojson.type);
        }
      }

      /*
      Internal: Calculate an bounding box from an nested array of positions
      [
        [
          [ [lng, lat],[lng, lat],[lng, lat] ]
        ]
        [
          [lng, lat],[lng, lat],[lng, lat]
        ]
        [
          [lng, lat],[lng, lat],[lng, lat]
        ]
      ]
      */
      function calculateBoundsFromNestedArrays (array) {
        var x1 = null, x2 = null, y1 = null, y2 = null;

        for (var i = 0; i < array.length; i++) {
          var inner = array[i];

          for (var j = 0; j < inner.length; j++) {
            var lonlat = inner[j];

            var lon = lonlat[0];
            var lat = lonlat[1];

            if (x1 === null) {
              x1 = lon;
            } else if (lon < x1) {
              x1 = lon;
            }

            if (x2 === null) {
              x2 = lon;
            } else if (lon > x2) {
              x2 = lon;
            }

            if (y1 === null) {
              y1 = lat;
            } else if (lat < y1) {
              y1 = lat;
            }

            if (y2 === null) {
              y2 = lat;
            } else if (lat > y2) {
              y2 = lat;
            }
          }
        }

        return [x1, y1, x2, y2 ];
      }

      /*
      Internal: Calculate a bounding box from an array of arrays of arrays
      [
        [ [lng, lat],[lng, lat],[lng, lat] ]
        [ [lng, lat],[lng, lat],[lng, lat] ]
        [ [lng, lat],[lng, lat],[lng, lat] ]
      ]
      */
      function calculateBoundsFromNestedArrayOfArrays (array) {
        var x1 = null, x2 = null, y1 = null, y2 = null;

        for (var i = 0; i < array.length; i++) {
          var inner = array[i];

          for (var j = 0; j < inner.length; j++) {
            var innerinner = inner[j];
            for (var k = 0; k < innerinner.length; k++) {
              var lonlat = innerinner[k];

              var lon = lonlat[0];
              var lat = lonlat[1];

              if (x1 === null) {
                x1 = lon;
              } else if (lon < x1) {
                x1 = lon;
              }

              if (x2 === null) {
                x2 = lon;
              } else if (lon > x2) {
                x2 = lon;
              }

              if (y1 === null) {
                y1 = lat;
              } else if (lat < y1) {
                y1 = lat;
              }

              if (y2 === null) {
                y2 = lat;
              } else if (lat > y2) {
                y2 = lat;
              }
            }
          }
        }

        return [x1, y1, x2, y2];
      }

      /*
      Internal: Calculate a bounding box from an array of positions
      [
        [lng, lat],[lng, lat],[lng, lat]
      ]
      */
      function calculateBoundsFromArray (array) {
        var x1 = null, x2 = null, y1 = null, y2 = null;

        for (var i = 0; i < array.length; i++) {
          var lonlat = array[i];
          var lon = lonlat[0];
          var lat = lonlat[1];

          if (x1 === null) {
            x1 = lon;
          } else if (lon < x1) {
            x1 = lon;
          }

          if (x2 === null) {
            x2 = lon;
          } else if (lon > x2) {
            x2 = lon;
          }

          if (y1 === null) {
            y1 = lat;
          } else if (lat < y1) {
            y1 = lat;
          }

          if (y2 === null) {
            y2 = lat;
          } else if (lat > y2) {
            y2 = lat;
          }
        }

        return [x1, y1, x2, y2 ];
      }

      /*
      Internal: Calculate an bounding box for a feature collection
      */
      function calculateBoundsForFeatureCollection(featureCollection){
        var extents = [], extent;
        for (var i = featureCollection.features.length - 1; i >= 0; i--) {
          extent = calculateBounds(featureCollection.features[i].geometry);
          extents.push([extent[0],extent[1]]);
          extents.push([extent[2],extent[3]]);
        }

        return calculateBoundsFromArray(extents);
      }

      /*
      Internal: Calculate an bounding box for a geometry collection
      */
      function calculateBoundsForGeometryCollection(geometryCollection){
        var extents = [], extent;

        for (var i = geometryCollection.geometries.length - 1; i >= 0; i--) {
          extent = calculateBounds(geometryCollection.geometries[i]);
          extents.push([extent[0],extent[1]]);
          extents.push([extent[2],extent[3]]);
        }

        return calculateBoundsFromArray(extents);
      }

      function calculateEnvelope(geojson){
        var bounds = calculateBounds(geojson);
        return {
          x: bounds[0],
          y: bounds[1],
          w: Math.abs(bounds[0] - bounds[2]),
          h: Math.abs(bounds[1] - bounds[3])
        };
      }

      /*
      Internal: Convert radians to degrees. Used by spatial reference converters.
      */
      function radToDeg(rad) {
        return rad * DegreesPerRadian;
      }

      /*
      Internal: Convert degrees to radians. Used by spatial reference converters.
      */
      function degToRad(deg) {
        return deg * RadiansPerDegree;
      }

      /*
      Internal: Loop over each array in a geojson object and apply a function to it. Used by spatial reference converters.
      */
      function eachPosition(coordinates, func) {
        for (var i = 0; i < coordinates.length; i++) {
          // we found a number so lets convert this pair
          if(typeof coordinates[i][0] === "number"){
            coordinates[i] = func(coordinates[i]);
          }
          // we found an coordinates array it again and run THIS function against it
          if(typeof coordinates[i] === "object"){
            coordinates[i] = eachPosition(coordinates[i], func);
          }
        }
        return coordinates;
      }

      /*
      Public: Convert a GeoJSON Position object to Geographic (4326)
      */
      function positionToGeographic(position) {
        var x = position[0];
        var y = position[1];
        return [radToDeg(x / EarthRadius) - (Math.floor((radToDeg(x / EarthRadius) + 180) / 360) * 360), radToDeg((Math.PI / 2) - (2 * Math.atan(Math.exp(-1.0 * y / EarthRadius))))];
      }

      /*
      Public: Convert a GeoJSON Position object to Web Mercator (102100)
      */
      function positionToMercator(position) {
        var lng = position[0];
        var lat = Math.max(Math.min(position[1], 89.99999), -89.99999);
        return [degToRad(lng) * EarthRadius, EarthRadius/2.0 * Math.log( (1.0 + Math.sin(degToRad(lat))) / (1.0 - Math.sin(degToRad(lat))) )];
      }

      /*
      Public: Apply a function agaist all positions in a geojson object. Used by spatial reference converters.
      */
      function applyConverter(geojson, converter, noCrs){
        if(geojson.type === "Point") {
          geojson.coordinates = converter(geojson.coordinates);
        } else if(geojson.type === "Feature") {
          geojson.geometry = applyConverter(geojson.geometry, converter, true);
        } else if(geojson.type === "FeatureCollection") {
          for (var f = 0; f < geojson.features.length; f++) {
            geojson.features[f] = applyConverter(geojson.features[f], converter, true);
          }
        } else if(geojson.type === "GeometryCollection") {
          for (var g = 0; g < geojson.geometries.length; g++) {
            geojson.geometries[g] = applyConverter(geojson.geometries[g], converter, true);
          }
        } else {
          geojson.coordinates = eachPosition(geojson.coordinates, converter);
        }

        if(!noCrs){
          if(converter === positionToMercator){
            geojson.crs = MercatorCRS;
          }
        }

        if(converter === positionToGeographic){
          delete geojson.crs;
        }

        return geojson;
      }

      /*
      Public: Convert a GeoJSON object to ESRI Web Mercator (102100)
      */
      function toMercator(geojson) {
        return applyConverter(geojson, positionToMercator);
      }

      /*
      Convert a GeoJSON object to Geographic coordinates (WSG84, 4326)
      */
      function toGeographic(geojson) {
        return applyConverter(geojson, positionToGeographic);
      }

      /*
      Internal: -1,0,1 comparison function
      */
      function cmp(a, b) {
        if(a < b) {
          return -1;
        } else if(a > b) {
          return 1;
        } else {
          return 0;
        }
      }

      /*
      Internal: used for sorting
      */
      function compSort(p1, p2) {
        if(p1[0] - p2[0] > p1[1] - p2[1]) {
          return 1;
        } else if(p1[0] - p2[0] < p1[1] - p2[1]) {
          return -1;
        } else {
          return 0;
        }
      }

      /*
      Internal: used to determine turn
      */
      function turn(p, q, r) {
        // Returns -1, 0, 1 if p,q,r forms a right, straight, or left turn.
        return cmp((q[0] - p[0]) * (r[1] - p[1]) - (r[0] - p[0]) * (q[1] - p[1]), 0);
      }

      /*
      Internal: used to determine euclidean distance between two points
      */
      function euclideanDistance(p, q) {
        // Returns the squared Euclidean distance between p and q.
        var dx = q[0] - p[0];
        var dy = q[1] - p[1];

        return dx * dx + dy * dy;
      }

      function nextHullPoint(points, p) {
        // Returns the next point on the convex hull in CCW from p.
        var q = p;
        for(var r in points) {
          var t = turn(p, q, points[r]);
          if(t === -1 || t === 0 && euclideanDistance(p, points[r]) > euclideanDistance(p, q)) {
            q = points[r];
          }
        }
        return q;
      }

      function convexHull(points) {
        // implementation of the Jarvis March algorithm
        // adapted from http://tixxit.wordpress.com/2009/12/09/jarvis-march/

        if(points.length === 0) {
          return [];
        } else if(points.length === 1) {
          return points;
        }

        // Returns the points on the convex hull of points in CCW order.
        var hull = [points.sort(compSort)[0]];

        for(var p = 0; p < hull.length; p++) {
          var q = nextHullPoint(points, hull[p]);

          if(q !== hull[0]) {
            hull.push(q);
          }
        }

        return hull;
      }

      function coordinatesContainPoint(coordinates, point) {
        var contains = false;
        for(var i = -1, l = coordinates.length, j = l - 1; ++i < l; j = i) {
          if (((coordinates[i][1] <= point[1] && point[1] < coordinates[j][1]) ||
               (coordinates[j][1] <= point[1] && point[1] < coordinates[i][1])) &&
              (point[0] < (coordinates[j][0] - coordinates[i][0]) * (point[1] - coordinates[i][1]) / (coordinates[j][1] - coordinates[i][1]) + coordinates[i][0])) {
            contains = !contains;
          }
        }
        return contains;
      }

      function polygonContainsPoint(polygon, point) {
        if (polygon && polygon.length) {
          if (polygon.length === 1) { // polygon with no holes
            return coordinatesContainPoint(polygon[0], point);
          } else { // polygon with holes
            if (coordinatesContainPoint(polygon[0], point)) {
              for (var i = 1; i < polygon.length; i++) {
                if (coordinatesContainPoint(polygon[i], point)) {
                  return false; // found in hole
                }
              }

              return true;
            } else {
              return false;
            }
          }
        } else {
          return false;
        }
      }

      function vertexIntersectsVertex(a1, a2, b1, b2) {
        var ua_t = (b2[0] - b1[0]) * (a1[1] - b1[1]) - (b2[1] - b1[1]) * (a1[0] - b1[0]);
        var ub_t = (a2[0] - a1[0]) * (a1[1] - b1[1]) - (a2[1] - a1[1]) * (a1[0] - b1[0]);
        var u_b  = (b2[1] - b1[1]) * (a2[0] - a1[0]) - (b2[0] - b1[0]) * (a2[1] - a1[1]);

        if ( u_b !== 0 ) {
          var ua = ua_t / u_b;
          var ub = ub_t / u_b;

          if ( 0 <= ua && ua <= 1 && 0 <= ub && ub <= 1 ) {
            return true;
          }
        }

        return false;
      }

      function arrayIntersectsArray(a, b) {
        for (var i = 0; i < a.length - 1; i++) {
          for (var j = 0; j < b.length - 1; j++) {
            if (vertexIntersectsVertex(a[i], a[i + 1], b[j], b[j + 1])) {
              return true;
            }
          }
        }

        return false;
      }

      function arrayIntersectsMultiArray(a, b) {
        for (var i = 0; i < b.length; i++) {
          var inner = b[i];

          for (var j = 0; j < inner.length - 1; j++) {
            for (var k = 0; k < a.length - 1; k++) {
              if (vertexIntersectsVertex(inner[j], inner[j + 1], a[k], a[k + 1])) {
                return true;
              }
            }
          }
        }

        return false;
      }

      function multiArrayIntersectsMultiArray(a, b) {
        for (var i = 0; i < a.length; i++) {
          if (arrayIntersectsMultiArray(a[i], b)) {
            return true;
          }
        }

        return false;
      }

      function arrayIntersectsMultiMultiArray(a, b) {
        for (var i = 0; i < b.length; i++) {
          if (arrayIntersectsMultiArray(a, b[i])) {
            return true;
          }

          return false;
        }
      }

      function multiArrayIntersectsMultiMultiArray(a, b) {
        for (var i = 0; i < a.length; i++) {
          if (arrayIntersectsMultiMultiArray(a[i], b)) {
            return true;
          }

          return false;
        }
      }

      function multiMultiArrayIntersectsMultiMultiArray(a, b) {
        for (var i = 0; i < a.length; i++) {
          if (multiArrayIntersectsMultiMultiArray(a[i], b)) {
            return true;
          }

          return false;
        }
      }

      /*
      Internal: Returns a copy of coordinates for s closed polygon
      */
      function closedPolygon(coordinates) {
        var outer = [ ];

        for (var i = 0; i < coordinates.length; i++) {
          var inner = coordinates[i].slice();

          if (pointsEqual(inner[0], inner[inner.length - 1]) === false) {
            inner.push(inner[0]);
          }

          outer.push(inner);
        }

        return outer;
      }

      function pointsEqual(a, b) {
        for (var i = 0; i < a.length; i++) {
          for (var j = 0; j < b.length; j++) {
            if (a[i] !== b[j]) {
              return false;
            }
          }
        }

        return true;
      }

      function coordinatesEqual(a, b) {
        if (a.length !== b.length) {
          return false;
        }

        var na = a.slice().sort(compSort);
        var nb = b.slice().sort(compSort);

        for (var i = 0; i < na.length; i++) {
          if (na[i].length !== nb[i].length) {
            return false;
          }
          for (var j = 0; j < na.length; j++) {
            if (na[i][j] !== nb[i][j]) {
              return false;
            }
          }
        }

        return true;
      }

      /*
      Internal: An array of variables that will be excluded form JSON objects.
      */
      var excludeFromJSON = ["length"];

      /*
      Internal: Base GeoJSON Primitive
      */
      function Primitive(geojson){
        if(geojson){
          switch (geojson.type) {
          case 'Point':
            return new Point(geojson);

          case 'MultiPoint':
            return new MultiPoint(geojson);

          case 'LineString':
            return new LineString(geojson);

          case 'MultiLineString':
            return new MultiLineString(geojson);

          case 'Polygon':
            return new Polygon(geojson);

          case 'MultiPolygon':
            return new MultiPolygon(geojson);

          case 'Feature':
            return new Feature(geojson);

          case 'FeatureCollection':
            return new FeatureCollection(geojson);

          case 'GeometryCollection':
            return new GeometryCollection(geojson);

          default:
            throw new Error("Unknown type: " + geojson.type);
          }
        }
      }

      Primitive.prototype = {
        toMercator: function(){
          return toMercator(this);
        },
        toGeographic: function(){
          return toGeographic(this);
        },
        envelope: function(){
          return calculateEnvelope(this);
        },
        bbox: function(){
          return calculateBounds(this);
        },
        convexHull: function(){
          var coordinates = [ ], i, j;
          if (this.type === 'Point') {
            if (this.coordinates && this.coordinates.length > 0) {
              return [ this.coordinates ];
            } else {
              return [ ];
            }
          } else if (this.type === 'LineString' || this.type === 'MultiPoint') {
            if (this.coordinates && this.coordinates.length > 0) {
              coordinates = this.coordinates;
            } else {
              return [ ];
            }
          } else if (this.type === 'Polygon' || this.type === 'MultiLineString') {
            if (this.coordinates && this.coordinates.length > 0) {
              for (i = 0; i < this.coordinates.length; i++) {
                coordinates = coordinates.concat(this.coordinates[i]);
              }
            } else {
              return [ ];
            }
          } else if (this.type === 'MultiPolygon') {
            if (this.coordinates && this.coordinates.length > 0) {
              for (i = 0; i < this.coordinates.length; i++) {
                for (j = 0; j < this.coordinates[i].length; j++) {
                  coordinates = coordinates.concat(this.coordinates[i][j]);
                }
              }
            } else {
              return [ ];
            }
          } else {
            throw new Error("Unable to get convex hull of " + this.type);
          }

          return convexHull(coordinates);
        },
        toJSON: function(){
          var obj = {};
          for (var key in this) {
            if (this.hasOwnProperty(key) && this[key] && excludeFromJSON.indexOf(key)) {
              obj[key] = this[key];
            }
          }
          obj.bbox = calculateBounds(this);
          return obj;
        }
      };

      Primitive.prototype.within = function(primitive) {
        var coordinates, i, contains;

        // point.within(point) :: equality
        if (primitive.type === "Point") {
          Eif (this.type === "Point") {
            return pointsEqual(this.coordinates, primitive.coordinates);

          }
        }

        // point.within(multilinestring)
        if (primitive.type === "MultiLineString") {
          if (this.type === "Point") {
            for (i = 0; i < primitive.coordinates.length; i++) {
              var linestring = { type: "LineString", coordinates: primitive.coordinates[i] };

              Eif (this.within(linestring)) {
                return true;
              }
            }
          }
        }

        // point.within(linestring), point.within(multipoint)
        if (primitive.type === "LineString" || primitive.type === "MultiPoint") {
          if (this.type === "Point") {
            for (i = 0; i < primitive.coordinates.length; i++) {
              if (this.coordinates.length !== primitive.coordinates[i].length) {
                return false;
              }

              if (pointsEqual(this.coordinates, primitive.coordinates[i])) {
                return true;
              }
            }
          }
        }

        if (primitive.type === "Polygon") {
          // polygon.within(polygon)
          if (this.type === "Polygon") {
            // check for equal polygons
            Eif (primitive.coordinates.length === this.coordinates.length) {
              for (i = 0; i < this.coordinates.length; i++) {
                if (coordinatesEqual(this.coordinates[i], primitive.coordinates[i])) {
                  return true;
                }
              }
            }

            if (this.coordinates.length && polygonContainsPoint(primitive.coordinates, this.coordinates[0][0])) {
              return !multiArrayIntersectsMultiArray(closedPolygon(this.coordinates), closedPolygon(primitive.coordinates));
            } else {
              return false;
            }

          // point.within(polygon)
          } else if (this.type === "Point") {
            return polygonContainsPoint(primitive.coordinates, this.coordinates);

          // linestring/multipoint withing polygon
          } else if (this.type === "LineString" || this.type === "MultiPoint") {
            if (!this.coordinates || this.coordinates.length === 0) {
              return false;
            }

            for (i = 0; i < this.coordinates.length; i++) {
              if (polygonContainsPoint(primitive.coordinates, this.coordinates[i]) === false) {
                return false;
              }
            }

            return true;

          // multilinestring.within(polygon)
          } else if (this.type === "MultiLineString") {
            for (i = 0; i < this.coordinates.length; i++) {
              var ls = new LineString(this.coordinates[i]);

              if (ls.within(primitive) === false) {
                contains++;
                return false;
              }
            }

            return true;

          // multipolygon.within(polygon)
          } else Eif (this.type === "MultiPolygon") {
            for (i = 0; i < this.coordinates.length; i++) {
              var p1 = new Primitive({ type: "Polygon", coordinates: this.coordinates[i] });

              if (p1.within(primitive) === false) {
                return false;
              }
            }

            return true;
          }

        }

        if (primitive.type === "MultiPolygon") {
          // point.within(multipolygon)
          if (this.type === "Point") {
            Eif (primitive.coordinates.length) {
              for (i = 0; i < primitive.coordinates.length; i++) {
                coordinates = primitive.coordinates[i];
                if (polygonContainsPoint(coordinates, this.coordinates) && multiArrayIntersectsMultiArray(this.coordinates, primitive.coordinates) === false) {
                  return true;
                }
              }
            }

            return false;
          // polygon.within(multipolygon)
          } else if (this.type === "Polygon") {
            for (i = 0; i < this.coordinates.length; i++) {
              Eif (primitive.coordinates[i].length === this.coordinates.length) {
                for (j = 0; j < this.coordinates.length; j++) {
                  if (coordinatesEqual(this.coordinates[j], primitive.coordinates[i][j])) {
                    return true;
                  }
                }
              }
            }

            if (multiArrayIntersectsMultiMultiArray(this.coordinates, primitive.coordinates) === false) {
              Eif (primitive.coordinates.length) {
                for (i = 0; i < primitive.coordinates.length; i++) {
                  coordinates = primitive.coordinates[i];
                  if (polygonContainsPoint(coordinates, this.coordinates[0][0]) === false) {
                    contains = false;
                  } else {
                    contains = true;
                  }
                }

                return contains;
              }
            }

          // linestring.within(multipolygon), multipoint.within(multipolygon)
          } else if (this.type === "LineString" || this.type === "MultiPoint") {
            for (i = 0; i < primitive.coordinates.length; i++) {
              var p = { type: "Polygon", coordinates: primitive.coordinates[i] };

              if (this.within(p)) {
                return true;
              }

              return false;
            }

          // multilinestring.within(multipolygon)
          } else if (this.type === "MultiLineString") {
            for (i = 0; i < this.coordinates.length; i++) {
              var lines = new LineString(this.coordinates[i]);

              if (lines.within(primitive) === false) {
                return false;
              }
            }

            return true;

          // multipolygon.within(multipolygon)
          } else Eif (this.type === "MultiPolygon") {
            for (i = 0; i < primitive.coordinates.length; i++) {
              var mpoly = { type: "Polygon", coordinates: primitive.coordinates[i] };

              if (this.within(mpoly) === false) {
                return false;
              }
            }

            return true;
          }
        }

        // default to false
        return false;
      };

      Primitive.prototype.intersects = function(primitive) {
        // if we are passed a feature, use the polygon inside instead
        Iif (primitive.type === 'Feature') {
          primitive = primitive.geometry;
        }

        var p = new Primitive(primitive);
        if (this.within(primitive) || p.within(this)) {
          return true;
        }

        if (this.type === 'LineString') {
          if (primitive.type === 'LineString') {
            return arrayIntersectsArray(this.coordinates, primitive.coordinates);
          } else if (primitive.type === 'MultiLineString') {
            return arrayIntersectsMultiArray(this.coordinates, primitive.coordinates);
          } else if (primitive.type === 'Polygon') {
            return arrayIntersectsMultiArray(this.coordinates, closedPolygon(primitive.coordinates));
          } else Eif (primitive.type === 'MultiPolygon') {
            return arrayIntersectsMultiMultiArray(this.coordinates, primitive.coordinates);
          }
        } else if (this.type === 'MultiLineString') {
          if (primitive.type === 'LineString') {
            return arrayIntersectsMultiArray(primitive.coordinates, this.coordinates);
          } else if (primitive.type === 'Polygon' || primitive.type === 'MultiLineString') {
            return multiArrayIntersectsMultiArray(this.coordinates, primitive.coordinates);
          } else Eif (primitive.type === 'MultiPolygon') {
            return multiArrayIntersectsMultiMultiArray(this.coordinates, primitive.coordinates);
          }
        } else if (this.type === 'Polygon') {
          if (primitive.type === 'LineString') {
            return arrayIntersectsMultiArray(primitive.coordinates, closedPolygon(this.coordinates));
          } else if (primitive.type === 'MultiLineString') {
            return multiArrayIntersectsMultiArray(closedPolygon(this.coordinates), primitive.coordinates);
          } else if (primitive.type === 'Polygon') {
            return multiArrayIntersectsMultiArray(closedPolygon(this.coordinates), closedPolygon(primitive.coordinates));
          } else Eif (primitive.type === 'MultiPolygon') {
            return multiArrayIntersectsMultiMultiArray(closedPolygon(this.coordinates), primitive.coordinates);
          }
        } else Eif (this.type === 'MultiPolygon') {
          if (primitive.type === 'LineString') {
            return arrayIntersectsMultiMultiArray(primitive.coordinates, this.coordinates);
          } else if (primitive.type === 'Polygon' || primitive.type === 'MultiLineString') {
            return multiArrayIntersectsMultiMultiArray(closedPolygon(primitive.coordinates), this.coordinates);
          } else Eif (primitive.type === 'MultiPolygon') {
            return multiMultiArrayIntersectsMultiMultiArray(this.coordinates, primitive.coordinates);
          }
        } else if (this.type === 'Feature') {
          // in the case of a Feature, use the internal primitive for intersection
          var inner = new Primitive(this.geometry);
          return inner.intersects(primitive);
        }

        warn("Type " + this.type + " to " + primitive.type + " intersection is not supported by intersects");
        return false;
      };

      /*
      GeoJSON Point Class
        new Point();
        new Point(x,y,z,wtf);
        new Point([x,y,z,wtf]);
        new Point([x,y]);
        new Point({
          type: "Point",
          coordinates: [x,y]
        });
      */
      function Point(input){
        var args = Array.prototype.slice.call(arguments);

        if(input && input.type === "Point" && input.coordinates){
          extend(this, input);
        } else if(input && Object.prototype.toString.call(input) === "[object Array]") {
          this.coordinates = input;
        } else if(args.length >= 2) {
          this.coordinates = args;
        } else {
          throw "Terraformer: invalid input for Terraformer.Point";
        }

        this.type = "Point";
      }

      Point.prototype = new Primitive();
      Point.prototype.constructor = Point;

      /*
      GeoJSON MultiPoint Class
          new MultiPoint();
          new MultiPoint([[x,y], [x1,y1]]);
          new MultiPoint({
            type: "MultiPoint",
            coordinates: [x,y]
          });
      */
      function MultiPoint(input){
        if(input && input.type === "MultiPoint" && input.coordinates){
          extend(this, input);
        } else if(Array.isArray(input)) {
          this.coordinates = input;
        } else {
          throw "Terraformer: invalid input for Terraformer.MultiPoint";
        }

        this.type = "MultiPoint";
      }

      MultiPoint.prototype = new Primitive();
      MultiPoint.prototype.constructor = MultiPoint;
      MultiPoint.prototype.forEach = function(func){
        for (var i = 0; i < this.coordinates.length; i++) {
          func.apply(this, [this.coordinates[i], i, this.coordinates]);
        }
        return this;
      };
      MultiPoint.prototype.addPoint = function(point){
        this.coordinates.push(point);
        return this;
      };
      MultiPoint.prototype.insertPoint = function(point, index){
        this.coordinates.splice(index, 0, point);
        return this;
      };
      MultiPoint.prototype.removePoint = function(remove){
        if(typeof remove === "number"){
          this.coordinates.splice(remove, 1);
        } else {
          this.coordinates.splice(this.coordinates.indexOf(remove), 1);
        }
        return this;
      };
      MultiPoint.prototype.get = function(i){
        return new Point(this.coordinates[i]);
      };

      /*
      GeoJSON LineString Class
          new LineString();
          new LineString([[x,y], [x1,y1]]);
          new LineString({
            type: "LineString",
            coordinates: [x,y]
          });
      */
      function LineString(input){
        if(input && input.type === "LineString" && input.coordinates){
          extend(this, input);
        } else if(Array.isArray(input)) {
          this.coordinates = input;
        } else {
          throw "Terraformer: invalid input for Terraformer.LineString";
        }

        this.type = "LineString";
      }

      LineString.prototype = new Primitive();
      LineString.prototype.constructor = LineString;
      LineString.prototype.addVertex = function(point){
        this.coordinates.push(point);
        return this;
      };
      LineString.prototype.insertVertex = function(point, index){
        this.coordinates.splice(index, 0, point);
        return this;
      };
      LineString.prototype.removeVertex = function(remove){
        this.coordinates.splice(remove, 1);
        return this;
      };

      /*
      GeoJSON MultiLineString Class
          new MultiLineString();
          new MultiLineString([ [[x,y], [x1,y1]], [[x2,y2], [x3,y3]] ]);
          new MultiLineString({
            type: "MultiLineString",
            coordinates: [ [[x,y], [x1,y1]], [[x2,y2], [x3,y3]] ]
          });
      */
      function MultiLineString(input){
        if(input && input.type === "MultiLineString" && input.coordinates){
          extend(this, input);
        } else if(Array.isArray(input)) {
          this.coordinates = input;
        } else {
          throw "Terraformer: invalid input for Terraformer.MultiLineString";
        }

        this.type = "MultiLineString";
      }

      MultiLineString.prototype = new Primitive();
      MultiLineString.prototype.constructor = MultiLineString;
      MultiLineString.prototype.forEach = function(func){
        for (var i = 0; i < this.coordinates.length; i++) {
          func.apply(this, [this.coordinates[i], i, this.coordinates ]);
        }
      };
      MultiLineString.prototype.get = function(i){
        return new LineString(this.coordinates[i]);
      };

      /*
      GeoJSON Polygon Class
          new Polygon();
          new Polygon([ [[x,y], [x1,y1], [x2,y2]] ]);
          new Polygon({
            type: "Polygon",
            coordinates: [ [[x,y], [x1,y1], [x2,y2]] ]
          });
      */
      function Polygon(input){
        if(input && input.type === "Polygon" && input.coordinates){
          extend(this, input);
        } else if(Array.isArray(input)) {
          this.coordinates = input;
        } else {
          throw "Terraformer: invalid input for Terraformer.Polygon";
        }

        this.type = "Polygon";
      }

      Polygon.prototype = new Primitive();
      Polygon.prototype.constructor = Polygon;
      Polygon.prototype.addVertex = function(point){
        this.coordinates[0].push(point);
        return this;
      };
      Polygon.prototype.insertVertex = function(point, index){
        this.coordinates[0].splice(index, 0, point);
        return this;
      };
      Polygon.prototype.removeVertex = function(remove){
        this.coordinates[0].splice(remove, 1);
        return this;
      };

      Polygon.prototype.close = function() {
        this.coordinates = closedPolygon(this.coordinates);
      };

      /*
      GeoJSON MultiPolygon Class
          new MultiPolygon();
          new MultiPolygon([ [ [[x,y], [x1,y1]], [[x2,y2], [x3,y3]] ] ]);
          new MultiPolygon({
            type: "MultiPolygon",
            coordinates: [ [ [[x,y], [x1,y1]], [[x2,y2], [x3,y3]] ] ]
          });
      */
      function MultiPolygon(input){
        if(input && input.type === "MultiPolygon" && input.coordinates){
          extend(this, input);
        } else if(Array.isArray(input)) {
          this.coordinates = input;
        } else {
          throw "Terraformer: invalid input for Terraformer.MultiPolygon";
        }

        this.type = "MultiPolygon";
      }

      MultiPolygon.prototype = new Primitive();
      MultiPolygon.prototype.constructor = MultiPolygon;
      MultiPolygon.prototype.forEach = function(func){
        for (var i = 0; i < this.coordinates.length; i++) {
          func.apply(this, [this.coordinates[i], i, this.coordinates ]);
        }
      };
      MultiPolygon.prototype.get = function(i){
        return new Polygon(this.coordinates[i]);
      };

      /*
      GeoJSON Feature Class
          new Feature();
          new Feature({
            type: "Feature",
            geometry: {
              type: "Polygon",
              coordinates: [ [ [[x,y], [x1,y1]], [[x2,y2], [x3,y3]] ] ]
            }
          });
          new Feature({
            type: "Polygon",
            coordinates: [ [ [[x,y], [x1,y1]], [[x2,y2], [x3,y3]] ] ]
          });
      */
      function Feature(input){
        if(input && input.type === "Feature" && input.geometry){
          extend(this, input);
        } else Eif(input && input.type && input.coordinates) {
          this.geometry = input;
        } else {
          throw "Terraformer: invalid input for Terraformer.Feature";
        }

        this.type = "Feature";
      }

      Feature.prototype = new Primitive();
      Feature.prototype.constructor = Feature;

      /*
      GeoJSON FeatureCollection Class
          new FeatureCollection();
          new FeatureCollection([feature, feature1]);
          new FeatureCollection({
            type: "FeatureCollection",
            coordinates: [feature, feature1]
          });
      */
      function FeatureCollection(input){
        if(input && input.type === "FeatureCollection" && input.features){
          extend(this, input);
        } else Eif(Array.isArray(input)) {
          this.features = input;
        } else {
          throw "Terraformer: invalid input for Terraformer.FeatureCollection";
        }

        this.type = "FeatureCollection";
      }

      FeatureCollection.prototype = new Primitive();
      FeatureCollection.prototype.constructor = FeatureCollection;
      FeatureCollection.prototype.forEach = function(func){
        for (var i = 0; i < this.features.length; i++) {
          func.apply(this, [this.features[i], i, this.features]);
        }
      };
      FeatureCollection.prototype.get = function(id){
        var found;
        this.forEach(function(feature){
          if(feature.id === id){
            found = feature;
          }
        });
        return new Feature(found);
      };

      /*
      GeoJSON GeometryCollection Class
          new GeometryCollection();
          new GeometryCollection([geometry, geometry1]);
          new GeometryCollection({
            type: "GeometryCollection",
            coordinates: [geometry, geometry1]
          });
      */
      function GeometryCollection(input){
        if(input && input.type === "GeometryCollection" && input.geometries){
          extend(this, input);
        } else Eif(Array.isArray(input)) {
          this.geometries = input;
        } else if(input.coordinates && input.type){
          this.type = "GeometryCollection";
          this.geometries = [input];
        } else {
          throw "Terraformer: invalid input for Terraformer.GeometryCollection";
        }

        this.type = "GeometryCollection";
      }

      GeometryCollection.prototype = new Primitive();
      GeometryCollection.prototype.constructor = GeometryCollection;
      GeometryCollection.prototype.forEach = function(func){
        for (var i = 0; i < this.geometries.length; i++) {
          func.apply(this, [this.geometries[i], i, this.geometries]);
        }
      };
      GeometryCollection.prototype.get = function(i){
        return new Primitive(this.geometries[i]);
      };

      function createCircle(center, radius, interpolate){
        var mercatorPosition = positionToMercator(center);
        var steps = interpolate || 64;
        var polygon = {
          type: "Polygon",
          coordinates: [[]]
        };
        for(var i=1; i<=steps; i++) {
          var radians = i * (360/steps) * Math.PI / 180;
          polygon.coordinates[0].push([mercatorPosition[0] + radius * Math.cos(radians), mercatorPosition[1] + radius * Math.sin(radians)]);
        }
        return toGeographic(polygon);
      }

      function Circle (center, rad, interpolate) {
        var steps = interpolate || 64;
        var radius = rad || 250;

        if(!center || center.length < 2 || !radius || !steps) {
          throw new Error("Terraformer: missing parameter for Terraformer.Circle");
        }

        extend(this, new Feature({
          type: "Feature",
          geometry: createCircle(center, radius, steps),
          properties: {
            radius: radius,
            center: center,
            steps: steps
          }
        }));
      }

      Circle.prototype = new Primitive();
      Circle.prototype.constructor = Circle;
      Circle.prototype.recalculate = function(){
        this.geometry = createCircle(this.properties.center, this.properties.radius, this.properties.steps);
        return this;
      };
      Circle.prototype.center = function(coordinates){
        if(coordinates){
          this.properties.center = coordinates;
          this.recalculate();
        }
        return this.properties.center;
      };
      Circle.prototype.radius = function(radius){
        if(radius){
          this.properties.radius = radius;
          this.recalculate();
        }
        return this.properties.radius;
      };
      Circle.prototype.steps = function(steps){
        if(steps){
          this.properties.steps = steps;
          this.recalculate();
        }
        return this.properties.steps;
      };

      Circle.prototype.toJSON = function() {
        var output = Primitive.prototype.toJSON.call(this);
        return output;
      };

      exports.Primitive = Primitive;
      exports.Point = Point;
      exports.MultiPoint = MultiPoint;
      exports.LineString = LineString;
      exports.MultiLineString = MultiLineString;
      exports.Polygon = Polygon;
      exports.MultiPolygon = MultiPolygon;
      exports.Feature = Feature;
      exports.FeatureCollection = FeatureCollection;
      exports.GeometryCollection = GeometryCollection;
      exports.Circle = Circle;

      exports.toMercator = toMercator;
      exports.toGeographic = toGeographic;

      exports.Tools = {};
      exports.Tools.positionToMercator = positionToMercator;
      exports.Tools.positionToGeographic = positionToGeographic;
      exports.Tools.applyConverter = applyConverter;
      exports.Tools.toMercator = toMercator;
      exports.Tools.toGeographic = toGeographic;
      exports.Tools.createCircle = createCircle;

      exports.Tools.calculateBounds = calculateBounds;
      exports.Tools.calculateEnvelope = calculateEnvelope;
      exports.Tools.coordinatesContainPoint = coordinatesContainPoint;
      exports.Tools.polygonContainsPoint = polygonContainsPoint;
      exports.Tools.arrayIntersectsArray = arrayIntersectsArray;
      exports.Tools.coordinatesContainPoint = coordinatesContainPoint;
      exports.Tools.coordinatesEqual = coordinatesEqual;
      exports.Tools.convexHull = convexHull;

      exports.MercatorCRS = MercatorCRS;
      exports.GeographicCRS = GeographicCRS;

      return exports;
    }));