# all files / ol/geom/flat/ simplifyflatgeom.js

94.7% Statements 143/151
97.87% Branches 46/47
88.89% Functions 8/9
94.7% Lines 143/151

    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

    1×

    1×

    1×

    6×

    6×
    3×

    3×
    3×
    3×

    6×

    6×

    1×

    31×
    31×
    9×
    9×

    9×

    9×

    22×
    22×
    22×

    22×
    22×
    22×
    22×
    211×
    211×
    211×
    211×
    211×
    211×
    211×
    211×
    1730×
    1730×
    1730×

    1730×
    658×
    658×

    211×
    115×
    115×
    103×

    115×
    86×

    22×
    388×
    159×

    159×

    22×

    1×

    1×
    1×
    1×
    1×

    1×
    1×

    1×

    1×

    1×

    13×

    4×
    2×
    2×

    4×

    9×
    9×

    9×
    9×
    9×
    9×
    9×
    224×
    224×
    224×

    191×
    191×
    191×
    191×

    9×

    3×
    3×

    9×

    1×
    1218×

    1×

    40×
    1×

    39×
    39×
    39×

    39×
    39×

    39×
    39×
    39×
    39×
    39×
    39×

    2×
    2×
    2×

    37×
    97×

    97×
    97×
    97×

    97×
    8×

    89×
    89×

    89×
    89×

    89×

    10×
    10×
    10×

    79×
    79×
    79×
    79×
    79×
    79×

    37×
    37×
    37×

    1×

    32×
    32×
    32×
    32×

    32×
    32×

    32×

    1×

    2×
    2×
    3×
    3×
    3×

    3×
    3×

    2×

     // Based on simplify-js https://github.com/mourner/simplify-js
    // Copyright (c) 2012, Vladimir Agafonkin
    // All rights reserved.
    //
    // Redistribution and use in source and binary forms, with or without
    // modification, are permitted provided that the following conditions are met:
    //
    //    1. Redistributions of source code must retain the above copyright notice,
    //       this list of conditions and the following disclaimer.
    //
    //    2. Redistributions in binary form must reproduce the above copyright
    //       notice, this list of conditions and the following disclaimer in the
    //       documentation and/or other materials provided with the distribution.
    //
    // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    // POSSIBILITY OF SUCH DAMAGE.

    goog.provide('ol.geom.flat.simplify');

    goog.require('ol.math');

    /**
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {number} end End.
     * @param {number} stride Stride.
     * @param {number} squaredTolerance Squared tolerance.
     * @param {boolean} highQuality Highest quality.
     * @param {Array.<number>=} opt_simplifiedFlatCoordinates Simplified flat
     *     coordinates.
     * @return {Array.<number>} Simplified line string.
     */
    ol.geom.flat.simplify.lineString = function(flatCoordinates, offset, end,
        stride, squaredTolerance, highQuality, opt_simplifiedFlatCoordinates) {
      var simplifiedFlatCoordinates = opt_simplifiedFlatCoordinates !== undefined ?
          opt_simplifiedFlatCoordinates : [];
      if (!highQuality) {
        end = ol.geom.flat.simplify.radialDistance(flatCoordinates, offset, end,
            stride, squaredTolerance,
            simplifiedFlatCoordinates, 0);
        flatCoordinates = simplifiedFlatCoordinates;
        offset = 0;
        stride = 2;
      }
      simplifiedFlatCoordinates.length = ol.geom.flat.simplify.douglasPeucker(
          flatCoordinates, offset, end, stride, squaredTolerance,
          simplifiedFlatCoordinates, 0);
      return simplifiedFlatCoordinates;
    };

    /**
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {number} end End.
     * @param {number} stride Stride.
     * @param {number} squaredTolerance Squared tolerance.
     * @param {Array.<number>} simplifiedFlatCoordinates Simplified flat
     *     coordinates.
     * @param {number} simplifiedOffset Simplified offset.
     * @return {number} Simplified offset.
     */
    ol.geom.flat.simplify.douglasPeucker = function(flatCoordinates, offset, end,
        stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset) {
      var n = (end - offset) / stride;
      if (n < 3) {
        for (; offset < end; offset += stride) {
          simplifiedFlatCoordinates[simplifiedOffset++] =
              flatCoordinates[offset];
          simplifiedFlatCoordinates[simplifiedOffset++] =
              flatCoordinates[offset + 1];
        }
        return simplifiedOffset;
      }
      /** @type {Array.<number>} */
      var markers = new Array(n);
      markers[0] = 1;
      markers[n - 1] = 1;
      /** @type {Array.<number>} */
      var stack = [offset, end - stride];
      var index = 0;
      var i;
      while (stack.length > 0) {
        var last = stack.pop();
        var first = stack.pop();
        var maxSquaredDistance = 0;
        var x1 = flatCoordinates[first];
        var y1 = flatCoordinates[first + 1];
        var x2 = flatCoordinates[last];
        var y2 = flatCoordinates[last + 1];
        for (i = first + stride; i < last; i += stride) {
          var x = flatCoordinates[i];
          var y = flatCoordinates[i + 1];
          var squaredDistance = ol.math.squaredSegmentDistance(
              x, y, x1, y1, x2, y2);
          if (squaredDistance > maxSquaredDistance) {
            index = i;
            maxSquaredDistance = squaredDistance;
          }
        }
        if (maxSquaredDistance > squaredTolerance) {
          markers[(index - offset) / stride] = 1;
          if (first + stride < index) {
            stack.push(first, index);
          }
          if (index + stride < last) {
            stack.push(index, last);
          }
        }
      }
      for (i = 0; i < n; ++i) {
        if (markers[i]) {
          simplifiedFlatCoordinates[simplifiedOffset++] =
              flatCoordinates[offset + i * stride];
          simplifiedFlatCoordinates[simplifiedOffset++] =
              flatCoordinates[offset + i * stride + 1];
        }
      }
      return simplifiedOffset;
    };

    /**
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {Array.<number>} ends Ends.
     * @param {number} stride Stride.
     * @param {number} squaredTolerance Squared tolerance.
     * @param {Array.<number>} simplifiedFlatCoordinates Simplified flat
     *     coordinates.
     * @param {number} simplifiedOffset Simplified offset.
     * @param {Array.<number>} simplifiedEnds Simplified ends.
     * @return {number} Simplified offset.
     */
    ol.geom.flat.simplify.douglasPeuckers = function(flatCoordinates, offset,
        ends, stride, squaredTolerance, simplifiedFlatCoordinates,
        simplifiedOffset, simplifiedEnds) {
      var i, ii;
      for (i = 0, ii = ends.length; i < ii; ++i) {
        var end = ends[i];
        simplifiedOffset = ol.geom.flat.simplify.douglasPeucker(
            flatCoordinates, offset, end, stride, squaredTolerance,
            simplifiedFlatCoordinates, simplifiedOffset);
        simplifiedEnds.push(simplifiedOffset);
        offset = end;
      }
      return simplifiedOffset;
    };

    /**
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {Array.<Array.<number>>} endss Endss.
     * @param {number} stride Stride.
     * @param {number} squaredTolerance Squared tolerance.
     * @param {Array.<number>} simplifiedFlatCoordinates Simplified flat
     *     coordinates.
     * @param {number} simplifiedOffset Simplified offset.
     * @param {Array.<Array.<number>>} simplifiedEndss Simplified endss.
     * @return {number} Simplified offset.
     */
    ol.geom.flat.simplify.douglasPeuckerss = function(
        flatCoordinates, offset, endss, stride, squaredTolerance,
        simplifiedFlatCoordinates, simplifiedOffset, simplifiedEndss) {
      var i, ii;
      for (i = 0, ii = endss.length; i < ii; ++i) {
        var ends = endss[i];
        var simplifiedEnds = [];
        simplifiedOffset = ol.geom.flat.simplify.douglasPeuckers(
            flatCoordinates, offset, ends, stride, squaredTolerance,
            simplifiedFlatCoordinates, simplifiedOffset, simplifiedEnds);
        simplifiedEndss.push(simplifiedEnds);
        offset = ends[ends.length - 1];
      }
      return simplifiedOffset;
    };

    /**
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {number} end End.
     * @param {number} stride Stride.
     * @param {number} squaredTolerance Squared tolerance.
     * @param {Array.<number>} simplifiedFlatCoordinates Simplified flat
     *     coordinates.
     * @param {number} simplifiedOffset Simplified offset.
     * @return {number} Simplified offset.
     */
    ol.geom.flat.simplify.radialDistance = function(flatCoordinates, offset, end,
        stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset) {
      if (end <= offset + stride) {
        // zero or one point, no simplification possible, so copy and return
        for (; offset < end; offset += stride) {
          simplifiedFlatCoordinates[simplifiedOffset++] = flatCoordinates[offset];
          simplifiedFlatCoordinates[simplifiedOffset++] =
              flatCoordinates[offset + 1];
        }
        return simplifiedOffset;
      }
      var x1 = flatCoordinates[offset];
      var y1 = flatCoordinates[offset + 1];
      // copy first point
      simplifiedFlatCoordinates[simplifiedOffset++] = x1;
      simplifiedFlatCoordinates[simplifiedOffset++] = y1;
      var x2 = x1;
      var y2 = y1;
      for (offset += stride; offset < end; offset += stride) {
        x2 = flatCoordinates[offset];
        y2 = flatCoordinates[offset + 1];
        if (ol.math.squaredDistance(x1, y1, x2, y2) > squaredTolerance) {
          // copy point at offset
          simplifiedFlatCoordinates[simplifiedOffset++] = x2;
          simplifiedFlatCoordinates[simplifiedOffset++] = y2;
          x1 = x2;
          y1 = y2;
        }
      }
      if (x2 != x1 || y2 != y1) {
        // copy last point
        simplifiedFlatCoordinates[simplifiedOffset++] = x2;
        simplifiedFlatCoordinates[simplifiedOffset++] = y2;
      }
      return simplifiedOffset;
    };

    /**
     * @param {number} value Value.
     * @param {number} tolerance Tolerance.
     * @return {number} Rounded value.
     */
    ol.geom.flat.simplify.snap = function(value, tolerance) {
      return tolerance * Math.round(value / tolerance);
    };

    /**
     * Simplifies a line string using an algorithm designed by Tim Schaub.
     * Coordinates are snapped to the nearest value in a virtual grid and
     * consecutive duplicate coordinates are discarded.  This effectively preserves
     * topology as the simplification of any subsection of a line string is
     * independent of the rest of the line string.  This means that, for examples,
     * the common edge between two polygons will be simplified to the same line
     * string independently in both polygons.  This implementation uses a single
     * pass over the coordinates and eliminates intermediate collinear points.
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {number} end End.
     * @param {number} stride Stride.
     * @param {number} tolerance Tolerance.
     * @param {Array.<number>} simplifiedFlatCoordinates Simplified flat
     *     coordinates.
     * @param {number} simplifiedOffset Simplified offset.
     * @return {number} Simplified offset.
     */
    ol.geom.flat.simplify.quantize = function(flatCoordinates, offset, end, stride,
        tolerance, simplifiedFlatCoordinates, simplifiedOffset) {
      // do nothing if the line is empty
      if (offset == end) {
        return simplifiedOffset;
      }
      // snap the first coordinate (P1)
      var x1 = ol.geom.flat.simplify.snap(flatCoordinates[offset], tolerance);
      var y1 = ol.geom.flat.simplify.snap(flatCoordinates[offset + 1], tolerance);
      offset += stride;
      // add the first coordinate to the output
      simplifiedFlatCoordinates[simplifiedOffset++] = x1;
      simplifiedFlatCoordinates[simplifiedOffset++] = y1;
      // find the next coordinate that does not snap to the same value as the first
      // coordinate (P2)
      var x2, y2;
      do {
        x2 = ol.geom.flat.simplify.snap(flatCoordinates[offset], tolerance);
        y2 = ol.geom.flat.simplify.snap(flatCoordinates[offset + 1], tolerance);
        offset += stride;
        if (offset == end) {
          // all coordinates snap to the same value, the line collapses to a point
          // push the last snapped value anyway to ensure that the output contains
          // at least two points
          // FIXME should we really return at least two points anyway?
          simplifiedFlatCoordinates[simplifiedOffset++] = x2;
          simplifiedFlatCoordinates[simplifiedOffset++] = y2;
          return simplifiedOffset;
        }
      } while (x2 == x1 && y2 == y1);
      while (offset < end) {
        var x3, y3;
        // snap the next coordinate (P3)
        x3 = ol.geom.flat.simplify.snap(flatCoordinates[offset], tolerance);
        y3 = ol.geom.flat.simplify.snap(flatCoordinates[offset + 1], tolerance);
        offset += stride;
        // skip P3 if it is equal to P2
        if (x3 == x2 && y3 == y2) {
          continue;
        }
        // calculate the delta between P1 and P2
        var dx1 = x2 - x1;
        var dy1 = y2 - y1;
        // calculate the delta between P3 and P1
        var dx2 = x3 - x1;
        var dy2 = y3 - y1;
        // if P1, P2, and P3 are colinear and P3 is further from P1 than P2 is from
        // P1 in the same direction then P2 is on the straight line between P1 and
        // P3
        if ((dx1 * dy2 == dy1 * dx2) &&
            ((dx1 < 0 && dx2 < dx1) || dx1 == dx2 || (dx1 > 0 && dx2 > dx1)) &&
            ((dy1 < 0 && dy2 < dy1) || dy1 == dy2 || (dy1 > 0 && dy2 > dy1))) {
          // discard P2 and set P2 = P3
          x2 = x3;
          y2 = y3;
          continue;
        }
        // either P1, P2, and P3 are not colinear, or they are colinear but P3 is
        // between P3 and P1 or on the opposite half of the line to P2.  add P2,
        // and continue with P1 = P2 and P2 = P3
        simplifiedFlatCoordinates[simplifiedOffset++] = x2;
        simplifiedFlatCoordinates[simplifiedOffset++] = y2;
        x1 = x2;
        y1 = y2;
        x2 = x3;
        y2 = y3;
      }
      // add the last point (P2)
      simplifiedFlatCoordinates[simplifiedOffset++] = x2;
      simplifiedFlatCoordinates[simplifiedOffset++] = y2;
      return simplifiedOffset;
    };

    /**
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {Array.<number>} ends Ends.
     * @param {number} stride Stride.
     * @param {number} tolerance Tolerance.
     * @param {Array.<number>} simplifiedFlatCoordinates Simplified flat
     *     coordinates.
     * @param {number} simplifiedOffset Simplified offset.
     * @param {Array.<number>} simplifiedEnds Simplified ends.
     * @return {number} Simplified offset.
     */
    ol.geom.flat.simplify.quantizes = function(
        flatCoordinates, offset, ends, stride,
        tolerance,
        simplifiedFlatCoordinates, simplifiedOffset, simplifiedEnds) {
      var i, ii;
      for (i = 0, ii = ends.length; i < ii; ++i) {
        var end = ends[i];
        simplifiedOffset = ol.geom.flat.simplify.quantize(
            flatCoordinates, offset, end, stride,
            tolerance,
            simplifiedFlatCoordinates, simplifiedOffset);
        simplifiedEnds.push(simplifiedOffset);
        offset = end;
      }
      return simplifiedOffset;
    };

    /**
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {Array.<Array.<number>>} endss Endss.
     * @param {number} stride Stride.
     * @param {number} tolerance Tolerance.
     * @param {Array.<number>} simplifiedFlatCoordinates Simplified flat
     *     coordinates.
     * @param {number} simplifiedOffset Simplified offset.
     * @param {Array.<Array.<number>>} simplifiedEndss Simplified endss.
     * @return {number} Simplified offset.
     */
    ol.geom.flat.simplify.quantizess = function(
        flatCoordinates, offset, endss, stride,
        tolerance,
        simplifiedFlatCoordinates, simplifiedOffset, simplifiedEndss) {
      var i, ii;
      for (i = 0, ii = endss.length; i < ii; ++i) {
        var ends = endss[i];
        var simplifiedEnds = [];
        simplifiedOffset = ol.geom.flat.simplify.quantizes(
            flatCoordinates, offset, ends, stride,
            tolerance,
            simplifiedFlatCoordinates, simplifiedOffset, simplifiedEnds);
        simplifiedEndss.push(simplifiedEnds);
        offset = ends[ends.length - 1];
      }
      return simplifiedOffset;
    };