# all files / ol/ extent.js

100% Statements 283/283
99.17% Branches 120/121
100% Functions 45/45
100% Lines 283/283

    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
    7931×

    1×
    1×
    1×

    1×
    4580×
    4580×
    17864×

    4580×

    1×
    576×
    576×
    576×
    576×
    576×

    1×
    352×
    150×
    150×
    150×
    150×
    150×

    202×

    1×
    328×
    231×
    231×
    231×
    231×
    231×

    97×

    1×
    10×
    10×
    1×
    9×
    3×

    6×

    10×
    1×
    9×
    2×

    7×

    10×

    1×
    332×

    1×
    387×

    1×
    344×

    1×
    47598×
    47598×
    47598×
    47598×
    47598×
    47598×
    47598×
    47598×
    8585×
    39013×
    6758×

    47598×
    5800×
    41798×
    9755×

    47598×
    30263×

    47598×

    1×
    10042×

    1×
    3015×
    1908×
    1908×
    1908×
    1908×
    1908×

    1107×

    1×
    804×

    1×
    453×
    453×
    453×

    1×
    5×
    5×

    1×
    554×
    554×

    1×
    4×
    4×

    1×
    540×

    1×
    41×
    30×

    41×
    35×

    41×
    29×

    41×
    35×

    41×

    1×
    26258×
    5270×

    26258×
    9172×

    26258×
    9272×

    26258×
    5369×

    1×
    11×
    11×
    38×

    11×

    1×
    600×
    7591×

    600×

    1×
    4×
    4×
    6×

    4×

    1×
    7591×
    7591×
    7591×
    7591×

    1×
    32×
    32×
    32×
    15×

    17×
    17×
    2×

    15×
    15×
    2×

    13×
    13×
    2×

    11×

    1×
    59×
    59×
    57×

    59×

    1×
    78×

    1×
    59×

    1×
    233×

    1×
    69×
    69×
    2×
    67×
    2×
    65×
    62×
    3×
    2×

    1×

    68×

    1×
    1×
    1×
    1×
    1×
    1×

    1×
    557×
    557×
    557×
    557×
    557×
    557×
    557×
    557×
    557×
    557×
    557×
    557×
    557×
    557×
    557×
    557×
    557×
    557×
    557×

    1×
    14210×

    1×
    2×
    2×

    1×
    123×
    123×
    119×
    31×

    88×

    119×
    35×

    84×

    119×
    32×

    87×

    119×
    34×

    85×

    123×

    1×
    1×

    1×
    9×

    1×
    306×

    1×
    58×

    1×
    5461×

    1×
    3165×

    1×
    459×

    1×
    1577×
    9×
    9×
    9×
    9×
    9×

    1568×

    1×
    4×
    4×
    4×
    4×
    4×
    4×

    1×
    74×
    74×
    74×
    74×

    15×

    59×
    59×
    59×
    59×
    59×
    59×
    59×
    59×
    59×
    59×
    59×

    21×
    21×

    59×

    21×
    21×

    59×

    16×
    16×

    59×

    6×
    6×

    74×

    1×
    576×

    576×
    576×
    576×
    576×

     goog.provide('ol.extent');

    goog.require('ol.asserts');
    goog.require('ol.extent.Corner');
    goog.require('ol.extent.Relationship');

    /**
     * Build an extent that includes all given coordinates.
     *
     * @param {Array.<ol.Coordinate>} coordinates Coordinates.
     * @return {ol.Extent} Bounding extent.
     * @api
     */
    ol.extent.boundingExtent = function(coordinates) {
      var extent = ol.extent.createEmpty();
      for (var i = 0, ii = coordinates.length; i < ii; ++i) {
        ol.extent.extendCoordinate(extent, coordinates[i]);
      }
      return extent;
    };

    /**
     * @param {Array.<number>} xs Xs.
     * @param {Array.<number>} ys Ys.
     * @param {ol.Extent=} opt_extent Destination extent.
     * @private
     * @return {ol.Extent} Extent.
     */
    ol.extent.boundingExtentXYs_ = function(xs, ys, opt_extent) {
      var minX = Math.min.apply(null, xs);
      var minY = Math.min.apply(null, ys);
      var maxX = Math.max.apply(null, xs);
      var maxY = Math.max.apply(null, ys);
      return ol.extent.createOrUpdate(minX, minY, maxX, maxY, opt_extent);
    };

    /**
     * Return extent increased by the provided value.
     * @param {ol.Extent} extent Extent.
     * @param {number} value The amount by which the extent should be buffered.
     * @param {ol.Extent=} opt_extent Extent.
     * @return {ol.Extent} Extent.
     * @api
     */
    ol.extent.buffer = function(extent, value, opt_extent) {
      if (opt_extent) {
        opt_extent[0] = extent[0] - value;
        opt_extent[1] = extent[1] - value;
        opt_extent[2] = extent[2] + value;
        opt_extent[3] = extent[3] + value;
        return opt_extent;
      } else {
        return [
          extent[0] - value,
          extent[1] - value,
          extent[2] + value,
          extent[3] + value
        ];
      }
    };

    /**
     * Creates a clone of an extent.
     *
     * @param {ol.Extent} extent Extent to clone.
     * @param {ol.Extent=} opt_extent Extent.
     * @return {ol.Extent} The clone.
     */
    ol.extent.clone = function(extent, opt_extent) {
      if (opt_extent) {
        opt_extent[0] = extent[0];
        opt_extent[1] = extent[1];
        opt_extent[2] = extent[2];
        opt_extent[3] = extent[3];
        return opt_extent;
      } else {
        return extent.slice();
      }
    };

    /**
     * @param {ol.Extent} extent Extent.
     * @param {number} x X.
     * @param {number} y Y.
     * @return {number} Closest squared distance.
     */
    ol.extent.closestSquaredDistanceXY = function(extent, x, y) {
      var dx, dy;
      if (x < extent[0]) {
        dx = extent[0] - x;
      } else if (extent[2] < x) {
        dx = x - extent[2];
      } else {
        dx = 0;
      }
      if (y < extent[1]) {
        dy = extent[1] - y;
      } else if (extent[3] < y) {
        dy = y - extent[3];
      } else {
        dy = 0;
      }
      return dx * dx + dy * dy;
    };

    /**
     * Check if the passed coordinate is contained or on the edge of the extent.
     *
     * @param {ol.Extent} extent Extent.
     * @param {ol.Coordinate} coordinate Coordinate.
     * @return {boolean} The coordinate is contained in the extent.
     * @api
     */
    ol.extent.containsCoordinate = function(extent, coordinate) {
      return ol.extent.containsXY(extent, coordinate[0], coordinate[1]);
    };

    /**
     * Check if one extent contains another.
     *
     * An extent is deemed contained if it lies completely within the other extent,
     * including if they share one or more edges.
     *
     * @param {ol.Extent} extent1 Extent 1.
     * @param {ol.Extent} extent2 Extent 2.
     * @return {boolean} The second extent is contained by or on the edge of the
     *     first.
     * @api
     */
    ol.extent.containsExtent = function(extent1, extent2) {
      return extent1[0] <= extent2[0] && extent2[2] <= extent1[2] &&
          extent1[1] <= extent2[1] && extent2[3] <= extent1[3];
    };

    /**
     * Check if the passed coordinate is contained or on the edge of the extent.
     *
     * @param {ol.Extent} extent Extent.
     * @param {number} x X coordinate.
     * @param {number} y Y coordinate.
     * @return {boolean} The x, y values are contained in the extent.
     * @api
     */
    ol.extent.containsXY = function(extent, x, y) {
      return extent[0] <= x && x <= extent[2] && extent[1] <= y && y <= extent[3];
    };

    /**
     * Get the relationship between a coordinate and extent.
     * @param {ol.Extent} extent The extent.
     * @param {ol.Coordinate} coordinate The coordinate.
     * @return {number} The relationship (bitwise compare with
     *     ol.extent.Relationship).
     */
    ol.extent.coordinateRelationship = function(extent, coordinate) {
      var minX = extent[0];
      var minY = extent[1];
      var maxX = extent[2];
      var maxY = extent[3];
      var x = coordinate[0];
      var y = coordinate[1];
      var relationship = ol.extent.Relationship.UNKNOWN;
      if (x < minX) {
        relationship = relationship | ol.extent.Relationship.LEFT;
      } else if (x > maxX) {
        relationship = relationship | ol.extent.Relationship.RIGHT;
      }
      if (y < minY) {
        relationship = relationship | ol.extent.Relationship.BELOW;
      } else if (y > maxY) {
        relationship = relationship | ol.extent.Relationship.ABOVE;
      }
      if (relationship === ol.extent.Relationship.UNKNOWN) {
        relationship = ol.extent.Relationship.INTERSECTING;
      }
      return relationship;
    };

    /**
     * Create an empty extent.
     * @return {ol.Extent} Empty extent.
     * @api
     */
    ol.extent.createEmpty = function() {
      return [Infinity, Infinity, -Infinity, -Infinity];
    };

    /**
     * Create a new extent or update the provided extent.
     * @param {number} minX Minimum X.
     * @param {number} minY Minimum Y.
     * @param {number} maxX Maximum X.
     * @param {number} maxY Maximum Y.
     * @param {ol.Extent=} opt_extent Destination extent.
     * @return {ol.Extent} Extent.
     */
    ol.extent.createOrUpdate = function(minX, minY, maxX, maxY, opt_extent) {
      if (opt_extent) {
        opt_extent[0] = minX;
        opt_extent[1] = minY;
        opt_extent[2] = maxX;
        opt_extent[3] = maxY;
        return opt_extent;
      } else {
        return [minX, minY, maxX, maxY];
      }
    };

    /**
     * Create a new empty extent or make the provided one empty.
     * @param {ol.Extent=} opt_extent Extent.
     * @return {ol.Extent} Extent.
     */
    ol.extent.createOrUpdateEmpty = function(opt_extent) {
      return ol.extent.createOrUpdate(
          Infinity, Infinity, -Infinity, -Infinity, opt_extent);
    };

    /**
     * @param {ol.Coordinate} coordinate Coordinate.
     * @param {ol.Extent=} opt_extent Extent.
     * @return {ol.Extent} Extent.
     */
    ol.extent.createOrUpdateFromCoordinate = function(coordinate, opt_extent) {
      var x = coordinate[0];
      var y = coordinate[1];
      return ol.extent.createOrUpdate(x, y, x, y, opt_extent);
    };

    /**
     * @param {Array.<ol.Coordinate>} coordinates Coordinates.
     * @param {ol.Extent=} opt_extent Extent.
     * @return {ol.Extent} Extent.
     */
    ol.extent.createOrUpdateFromCoordinates = function(coordinates, opt_extent) {
      var extent = ol.extent.createOrUpdateEmpty(opt_extent);
      return ol.extent.extendCoordinates(extent, coordinates);
    };

    /**
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {number} end End.
     * @param {number} stride Stride.
     * @param {ol.Extent=} opt_extent Extent.
     * @return {ol.Extent} Extent.
     */
    ol.extent.createOrUpdateFromFlatCoordinates = function(flatCoordinates, offset, end, stride, opt_extent) {
      var extent = ol.extent.createOrUpdateEmpty(opt_extent);
      return ol.extent.extendFlatCoordinates(
          extent, flatCoordinates, offset, end, stride);
    };

    /**
     * @param {Array.<Array.<ol.Coordinate>>} rings Rings.
     * @param {ol.Extent=} opt_extent Extent.
     * @return {ol.Extent} Extent.
     */
    ol.extent.createOrUpdateFromRings = function(rings, opt_extent) {
      var extent = ol.extent.createOrUpdateEmpty(opt_extent);
      return ol.extent.extendRings(extent, rings);
    };

    /**
     * Determine if two extents are equivalent.
     * @param {ol.Extent} extent1 Extent 1.
     * @param {ol.Extent} extent2 Extent 2.
     * @return {boolean} The two extents are equivalent.
     * @api
     */
    ol.extent.equals = function(extent1, extent2) {
      return extent1[0] == extent2[0] && extent1[2] == extent2[2] &&
          extent1[1] == extent2[1] && extent1[3] == extent2[3];
    };

    /**
     * Modify an extent to include another extent.
     * @param {ol.Extent} extent1 The extent to be modified.
     * @param {ol.Extent} extent2 The extent that will be included in the first.
     * @return {ol.Extent} A reference to the first (extended) extent.
     * @api
     */
    ol.extent.extend = function(extent1, extent2) {
      if (extent2[0] < extent1[0]) {
        extent1[0] = extent2[0];
      }
      if (extent2[2] > extent1[2]) {
        extent1[2] = extent2[2];
      }
      if (extent2[1] < extent1[1]) {
        extent1[1] = extent2[1];
      }
      if (extent2[3] > extent1[3]) {
        extent1[3] = extent2[3];
      }
      return extent1;
    };

    /**
     * @param {ol.Extent} extent Extent.
     * @param {ol.Coordinate} coordinate Coordinate.
     */
    ol.extent.extendCoordinate = function(extent, coordinate) {
      if (coordinate[0] < extent[0]) {
        extent[0] = coordinate[0];
      }
      if (coordinate[0] > extent[2]) {
        extent[2] = coordinate[0];
      }
      if (coordinate[1] < extent[1]) {
        extent[1] = coordinate[1];
      }
      if (coordinate[1] > extent[3]) {
        extent[3] = coordinate[1];
      }
    };

    /**
     * @param {ol.Extent} extent Extent.
     * @param {Array.<ol.Coordinate>} coordinates Coordinates.
     * @return {ol.Extent} Extent.
     */
    ol.extent.extendCoordinates = function(extent, coordinates) {
      var i, ii;
      for (i = 0, ii = coordinates.length; i < ii; ++i) {
        ol.extent.extendCoordinate(extent, coordinates[i]);
      }
      return extent;
    };

    /**
     * @param {ol.Extent} extent Extent.
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {number} end End.
     * @param {number} stride Stride.
     * @return {ol.Extent} Extent.
     */
    ol.extent.extendFlatCoordinates = function(extent, flatCoordinates, offset, end, stride) {
      for (; offset < end; offset += stride) {
        ol.extent.extendXY(
            extent, flatCoordinates[offset], flatCoordinates[offset + 1]);
      }
      return extent;
    };

    /**
     * @param {ol.Extent} extent Extent.
     * @param {Array.<Array.<ol.Coordinate>>} rings Rings.
     * @return {ol.Extent} Extent.
     */
    ol.extent.extendRings = function(extent, rings) {
      var i, ii;
      for (i = 0, ii = rings.length; i < ii; ++i) {
        ol.extent.extendCoordinates(extent, rings[i]);
      }
      return extent;
    };

    /**
     * @param {ol.Extent} extent Extent.
     * @param {number} x X.
     * @param {number} y Y.
     */
    ol.extent.extendXY = function(extent, x, y) {
      extent[0] = Math.min(extent[0], x);
      extent[1] = Math.min(extent[1], y);
      extent[2] = Math.max(extent[2], x);
      extent[3] = Math.max(extent[3], y);
    };

    /**
     * This function calls `callback` for each corner of the extent. If the
     * callback returns a truthy value the function returns that value
     * immediately. Otherwise the function returns `false`.
     * @param {ol.Extent} extent Extent.
     * @param {function(this:T, ol.Coordinate): S} callback Callback.
     * @param {T=} opt_this Value to use as `this` when executing `callback`.
     * @return {S|boolean} Value.
     * @template S, T
     */
    ol.extent.forEachCorner = function(extent, callback, opt_this) {
      var val;
      val = callback.call(opt_this, ol.extent.getBottomLeft(extent));
      if (val) {
        return val;
      }
      val = callback.call(opt_this, ol.extent.getBottomRight(extent));
      if (val) {
        return val;
      }
      val = callback.call(opt_this, ol.extent.getTopRight(extent));
      if (val) {
        return val;
      }
      val = callback.call(opt_this, ol.extent.getTopLeft(extent));
      if (val) {
        return val;
      }
      return false;
    };

    /**
     * Get the size of an extent.
     * @param {ol.Extent} extent Extent.
     * @return {number} Area.
     * @api
     */
    ol.extent.getArea = function(extent) {
      var area = 0;
      if (!ol.extent.isEmpty(extent)) {
        area = ol.extent.getWidth(extent) * ol.extent.getHeight(extent);
      }
      return area;
    };

    /**
     * Get the bottom left coordinate of an extent.
     * @param {ol.Extent} extent Extent.
     * @return {ol.Coordinate} Bottom left coordinate.
     * @api
     */
    ol.extent.getBottomLeft = function(extent) {
      return [extent[0], extent[1]];
    };

    /**
     * Get the bottom right coordinate of an extent.
     * @param {ol.Extent} extent Extent.
     * @return {ol.Coordinate} Bottom right coordinate.
     * @api
     */
    ol.extent.getBottomRight = function(extent) {
      return [extent[2], extent[1]];
    };

    /**
     * Get the center coordinate of an extent.
     * @param {ol.Extent} extent Extent.
     * @return {ol.Coordinate} Center.
     * @api
     */
    ol.extent.getCenter = function(extent) {
      return [(extent[0] + extent[2]) / 2, (extent[1] + extent[3]) / 2];
    };

    /**
     * Get a corner coordinate of an extent.
     * @param {ol.Extent} extent Extent.
     * @param {ol.extent.Corner} corner Corner.
     * @return {ol.Coordinate} Corner coordinate.
     */
    ol.extent.getCorner = function(extent, corner) {
      var coordinate;
      if (corner === ol.extent.Corner.BOTTOM_LEFT) {
        coordinate = ol.extent.getBottomLeft(extent);
      } else if (corner === ol.extent.Corner.BOTTOM_RIGHT) {
        coordinate = ol.extent.getBottomRight(extent);
      } else if (corner === ol.extent.Corner.TOP_LEFT) {
        coordinate = ol.extent.getTopLeft(extent);
      } else if (corner === ol.extent.Corner.TOP_RIGHT) {
        coordinate = ol.extent.getTopRight(extent);
      } else {
        ol.asserts.assert(false, 13); // Invalid corner
      }
      return /** @type {!ol.Coordinate} */ (coordinate);
    };

    /**
     * @param {ol.Extent} extent1 Extent 1.
     * @param {ol.Extent} extent2 Extent 2.
     * @return {number} Enlarged area.
     */
    ol.extent.getEnlargedArea = function(extent1, extent2) {
      var minX = Math.min(extent1[0], extent2[0]);
      var minY = Math.min(extent1[1], extent2[1]);
      var maxX = Math.max(extent1[2], extent2[2]);
      var maxY = Math.max(extent1[3], extent2[3]);
      return (maxX - minX) * (maxY - minY);
    };

    /**
     * @param {ol.Coordinate} center Center.
     * @param {number} resolution Resolution.
     * @param {number} rotation Rotation.
     * @param {ol.Size} size Size.
     * @param {ol.Extent=} opt_extent Destination extent.
     * @return {ol.Extent} Extent.
     */
    ol.extent.getForViewAndSize = function(center, resolution, rotation, size, opt_extent) {
      var dx = resolution * size[0] / 2;
      var dy = resolution * size[1] / 2;
      var cosRotation = Math.cos(rotation);
      var sinRotation = Math.sin(rotation);
      var xCos = dx * cosRotation;
      var xSin = dx * sinRotation;
      var yCos = dy * cosRotation;
      var ySin = dy * sinRotation;
      var x = center[0];
      var y = center[1];
      var x0 = x - xCos + ySin;
      var x1 = x - xCos - ySin;
      var x2 = x + xCos - ySin;
      var x3 = x + xCos + ySin;
      var y0 = y - xSin - yCos;
      var y1 = y - xSin + yCos;
      var y2 = y + xSin + yCos;
      var y3 = y + xSin - yCos;
      return ol.extent.createOrUpdate(
          Math.min(x0, x1, x2, x3), Math.min(y0, y1, y2, y3),
          Math.max(x0, x1, x2, x3), Math.max(y0, y1, y2, y3),
          opt_extent);
    };

    /**
     * Get the height of an extent.
     * @param {ol.Extent} extent Extent.
     * @return {number} Height.
     * @api
     */
    ol.extent.getHeight = function(extent) {
      return extent[3] - extent[1];
    };

    /**
     * @param {ol.Extent} extent1 Extent 1.
     * @param {ol.Extent} extent2 Extent 2.
     * @return {number} Intersection area.
     */
    ol.extent.getIntersectionArea = function(extent1, extent2) {
      var intersection = ol.extent.getIntersection(extent1, extent2);
      return ol.extent.getArea(intersection);
    };

    /**
     * Get the intersection of two extents.
     * @param {ol.Extent} extent1 Extent 1.
     * @param {ol.Extent} extent2 Extent 2.
     * @param {ol.Extent=} opt_extent Optional extent to populate with intersection.
     * @return {ol.Extent} Intersecting extent.
     * @api
     */
    ol.extent.getIntersection = function(extent1, extent2, opt_extent) {
      var intersection = opt_extent ? opt_extent : ol.extent.createEmpty();
      if (ol.extent.intersects(extent1, extent2)) {
        if (extent1[0] > extent2[0]) {
          intersection[0] = extent1[0];
        } else {
          intersection[0] = extent2[0];
        }
        if (extent1[1] > extent2[1]) {
          intersection[1] = extent1[1];
        } else {
          intersection[1] = extent2[1];
        }
        if (extent1[2] < extent2[2]) {
          intersection[2] = extent1[2];
        } else {
          intersection[2] = extent2[2];
        }
        if (extent1[3] < extent2[3]) {
          intersection[3] = extent1[3];
        } else {
          intersection[3] = extent2[3];
        }
      }
      return intersection;
    };

    /**
     * @param {ol.Extent} extent Extent.
     * @return {number} Margin.
     */
    ol.extent.getMargin = function(extent) {
      return ol.extent.getWidth(extent) + ol.extent.getHeight(extent);
    };

    /**
     * Get the size (width, height) of an extent.
     * @param {ol.Extent} extent The extent.
     * @return {ol.Size} The extent size.
     * @api
     */
    ol.extent.getSize = function(extent) {
      return [extent[2] - extent[0], extent[3] - extent[1]];
    };

    /**
     * Get the top left coordinate of an extent.
     * @param {ol.Extent} extent Extent.
     * @return {ol.Coordinate} Top left coordinate.
     * @api
     */
    ol.extent.getTopLeft = function(extent) {
      return [extent[0], extent[3]];
    };

    /**
     * Get the top right coordinate of an extent.
     * @param {ol.Extent} extent Extent.
     * @return {ol.Coordinate} Top right coordinate.
     * @api
     */
    ol.extent.getTopRight = function(extent) {
      return [extent[2], extent[3]];
    };

    /**
     * Get the width of an extent.
     * @param {ol.Extent} extent Extent.
     * @return {number} Width.
     * @api
     */
    ol.extent.getWidth = function(extent) {
      return extent[2] - extent[0];
    };

    /**
     * Determine if one extent intersects another.
     * @param {ol.Extent} extent1 Extent 1.
     * @param {ol.Extent} extent2 Extent.
     * @return {boolean} The two extents intersect.
     * @api
     */
    ol.extent.intersects = function(extent1, extent2) {
      return extent1[0] <= extent2[2] &&
          extent1[2] >= extent2[0] &&
          extent1[1] <= extent2[3] &&
          extent1[3] >= extent2[1];
    };

    /**
     * Determine if an extent is empty.
     * @param {ol.Extent} extent Extent.
     * @return {boolean} Is empty.
     * @api
     */
    ol.extent.isEmpty = function(extent) {
      return extent[2] < extent[0] || extent[3] < extent[1];
    };

    /**
     * @param {ol.Extent} extent Extent.
     * @param {ol.Extent=} opt_extent Extent.
     * @return {ol.Extent} Extent.
     */
    ol.extent.returnOrUpdate = function(extent, opt_extent) {
      if (opt_extent) {
        opt_extent[0] = extent[0];
        opt_extent[1] = extent[1];
        opt_extent[2] = extent[2];
        opt_extent[3] = extent[3];
        return opt_extent;
      } else {
        return extent;
      }
    };

    /**
     * @param {ol.Extent} extent Extent.
     * @param {number} value Value.
     */
    ol.extent.scaleFromCenter = function(extent, value) {
      var deltaX = ((extent[2] - extent[0]) / 2) * (value - 1);
      var deltaY = ((extent[3] - extent[1]) / 2) * (value - 1);
      extent[0] -= deltaX;
      extent[2] += deltaX;
      extent[1] -= deltaY;
      extent[3] += deltaY;
    };

    /**
     * Determine if the segment between two coordinates intersects (crosses,
     * touches, or is contained by) the provided extent.
     * @param {ol.Extent} extent The extent.
     * @param {ol.Coordinate} start Segment start coordinate.
     * @param {ol.Coordinate} end Segment end coordinate.
     * @return {boolean} The segment intersects the extent.
     */
    ol.extent.intersectsSegment = function(extent, start, end) {
      var intersects = false;
      var startRel = ol.extent.coordinateRelationship(extent, start);
      var endRel = ol.extent.coordinateRelationship(extent, end);
      if (startRel === ol.extent.Relationship.INTERSECTING ||
          endRel === ol.extent.Relationship.INTERSECTING) {
        intersects = true;
      } else {
        var minX = extent[0];
        var minY = extent[1];
        var maxX = extent[2];
        var maxY = extent[3];
        var startX = start[0];
        var startY = start[1];
        var endX = end[0];
        var endY = end[1];
        var slope = (endY - startY) / (endX - startX);
        var x, y;
        if (!!(endRel & ol.extent.Relationship.ABOVE) &&
            !(startRel & ol.extent.Relationship.ABOVE)) {
          // potentially intersects top
          x = endX - ((endY - maxY) / slope);
          intersects = x >= minX && x <= maxX;
        }
        if (!intersects && !!(endRel & ol.extent.Relationship.RIGHT) &&
            !(startRel & ol.extent.Relationship.RIGHT)) {
          // potentially intersects right
          y = endY - ((endX - maxX) * slope);
          intersects = y >= minY && y <= maxY;
        }
        if (!intersects && !!(endRel & ol.extent.Relationship.BELOW) &&
            !(startRel & ol.extent.Relationship.BELOW)) {
          // potentially intersects bottom
          x = endX - ((endY - minY) / slope);
          intersects = x >= minX && x <= maxX;
        }
        if (!intersects && !!(endRel & ol.extent.Relationship.LEFT) &&
            !(startRel & ol.extent.Relationship.LEFT)) {
          // potentially intersects left
          y = endY - ((endX - minX) * slope);
          intersects = y >= minY && y <= maxY;
        }

      }
      return intersects;
    };

    /**
     * Apply a transform function to the extent.
     * @param {ol.Extent} extent Extent.
     * @param {ol.TransformFunction} transformFn Transform function.  Called with
     * [minX, minY, maxX, maxY] extent coordinates.
     * @param {ol.Extent=} opt_extent Destination extent.
     * @return {ol.Extent} Extent.
     * @api
     */
    ol.extent.applyTransform = function(extent, transformFn, opt_extent) {
      var coordinates = [
        extent[0], extent[1],
        extent[0], extent[3],
        extent[2], extent[1],
        extent[2], extent[3]
      ];
      transformFn(coordinates, coordinates, 2);
      var xs = [coordinates[0], coordinates[2], coordinates[4], coordinates[6]];
      var ys = [coordinates[1], coordinates[3], coordinates[5], coordinates[7]];
      return ol.extent.boundingExtentXYs_(xs, ys, opt_extent);
    };