# Code coverage report for browser/arcgis.js

## Statements: 95.77% (136 / 142) Branches: 81.43% (57 / 70) Functions: 100% (9 / 9) Lines: 95.77% (136 / 142)

All files » browser/ » arcgis.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
    1

    1

    1

    1
    1

    1

    1
    1

    1
    1

    1

    1

    1
    21
    21
    21
    21
    21
    81
    81
    81

    21

    1
    9
    9

    9
    6

    9

    9
    2
    2

    2

    9

    1
    2
    2
    4
    4
    5
    5

    4

    2

    1
    5
    5
    5
    2

    3

    1
    6
    6

    6
    10

    10
    7
    7

    3

    6

    3
    3

    3
    5
    5

    2

    2

    2

    3
    1

    6
    4

    2

    1
    14
    14

    14
    2
    2

    14
    1
    1

    14
    2
    1
    1

    1
    1

    14
    6

    14
    3
    3
    3

    14

    14
    1

    14

    1
    21
    21
    21
    21

    21

    3
    3
    3
    3

    1
    1
    1

    3
    3
    3

    1
    1
    1

    5
    5
    5

    2
    2
    2

    4
    4
    4

    1
    1
    3

    1

    1
    1
    3

    1

    21

    1
    1

    1
     /* globals Terraformer */
    (function (root, factory) {

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

      // AMD.
      Iif(typeof define === 'function' && define.amd) {
        define(["terraformer/terraformer"],factory);
      }

      // Browser Global.
      Eif(typeof root.navigator === "object") {
        Iif (typeof root.Terraformer === "undefined"){
          root.Terraformer = {};
        }
        root.Terraformer.ArcGIS = factory();
      }

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

      // Local Reference To Browser Global
      Eif(typeof this.navigator === "object") {
        Terraformer = this.Terraformer;
      }

      // Setup Node Dependencies
      Iif(typeof module === 'object' && typeof module.exports === 'object') {
        Terraformer = require('terraformer');
      }

      // Setup AMD Dependencies
      Iif(arguments[0] && typeof define === 'function' && define.amd) {
        Terraformer = arguments[0];
      }

      // determine if polygon ring coordinates are clockwise. clockwise signifies outer ring, counter-clockwise an inner ring
      // or hole. this logic was found at http://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-
      // points-are-in-clockwise-order
      function ringIsClockwise(ringToTest) {
        var total = 0,i = 0;
        var rLength = ringToTest.length;
        var pt1 = ringToTest[i];
        var pt2;
        for (i; i < rLength - 1; i++) {
          pt2 = ringToTest[i + 1];
          total += (pt2[0] - pt1[0]) * (pt2[1] + pt1[1]);
          pt1 = pt2;
        }
        return (total >= 0);
      }

      // This function flattens holes in polygons to one array of rings
      //
      // [
      //   [ array of outer coordinates ]
      //   [ hole coordinates ]
      //   [ hole coordinates ]
      // ]
      // becomes
      // [
      //   [ array of outer coordinates ]
      //   [ hole coordinates ]
      //   [ hole coordinates ]
      // ]
      function flattenPolygonRings(polygon){
        var output = [];
        var outerRing = polygon.shift();

        if(!ringIsClockwise(outerRing)){
          outerRing.reverse();
        }

        output.push(outerRing);

        for (var i = 0; i < polygon.length; i++) {
          var hole = polygon[i];
          Iif(ringIsClockwise(hole)){
            outerRing.reverse();
          }
          output.push(polygon[i]);
        }

        return output;
      }

      // This function flattens holes in multipolygons to one array of polygons
      // so
      // [
      //   [
      //     [ array of outer coordinates ]
      //     [ hole coordinates ]
      //     [ hole coordinates ]
      //   ],
      //   [
      //     [ array of outer coordinates ]
      //     [ hole coordinates ]
      //     [ hole coordinates ]
      //   ],
      // ]
      // becomes
      // [
      //   [ array of outer coordinates ]
      //   [ hole coordinates ]
      //   [ hole coordinates ]
      //   [ array of outer coordinates ]
      //   [ hole coordinates ]
      //   [ hole coordinates ]
      // ]
      function flattenMultiPolygonRings(rings){
        var output = [];
        for (var i = 0; i < rings.length; i++) {
          var polygon = flattenPolygonRings(rings[i]);
          for (var x = polygon.length - 1; x >= 0; x--) {
            var ring = polygon[x];
            output.push(ring);
          }
          output.push();
        }
        return output;
      }

      function coordinatesContainCoordinates(outer, inner){
        var intersects = Terraformer.Tools.arrayIntersectsArray(outer, inner);
        var contains = Terraformer.Tools.coordinatesContainPoint(outer, inner[0]);
        if(!intersects && contains){
          return true;
        }
        return false;
      }

      // do any polygons in this array contain any other polygons in this array?
      // used for checking for holes in arcgis rings
      function convertRingsToGeoJSON(rings){
        var outerRings = [];
        var holes = [];

        // for each ring
        for (var r = 0; r < rings.length; r++) {
          var ring = rings[r];

          // is this ring an outer ring? is it clockwise?
          if(ringIsClockwise(ring)){
            var polygon = [ ring ];
            outerRings.push(polygon); // push to outer rings
          } else {
            holes.push(ring); // counterclockwise push to holes
          }
        }

        // while there are holes left...
        while(holes.length){
          // pop a hole off out stack
          var hole = holes.pop();
          var matched = false;

          // loop over all outer rings and see if they contain our hole.
          for (var x = outerRings.length - 1; x >= 0; x--) {
            var outerRing = outerRings[x][0];
            if(coordinatesContainCoordinates(outerRing, hole)){
              // the hole is contained push it into our polygon
              outerRings[x].push(hole);

              // we matched the hole
              matched = true;

              // stop checking to see if other outer rings contian this hole
              break;
            }
          }

          // no outer rings contain this hole turn it into and outer ring (reverse it)
          if(!matched){
            outerRings.push([ hole.reverse() ]);
          }
        }

        if(outerRings.length === 1){
          return {
            type: "Polygon",
            coordinates: outerRings[0]
          };
        } else {
          return {
            type: "MultiPolygon",
            coordinates: outerRings
          };
        }
      }

      // ArcGIS -> GeoJSON
      function parse(input){
        var arcgis = JSON.parse(JSON.stringify(input));
        var geojson = {};

        if(arcgis.x && arcgis.y){
          geojson.type = "Point";
          geojson.coordinates = [arcgis.x, arcgis.y];
        }

        if(arcgis.points){
          geojson.type = "MultiPoint";
          geojson.coordinates = arcgis.points;
        }

        if(arcgis.paths) {
          if(arcgis.paths.length === 1){
            geojson.type = "LineString";
            geojson.coordinates = arcgis.paths[0];
          } else {
            geojson.type = "MultiLineString";
            geojson.coordinates = arcgis.paths;
          }
        }

        if(arcgis.rings) {
          geojson = convertRingsToGeoJSON(arcgis.rings);
        }

        if(arcgis.geometry) {
          geojson.type = "Feature";
          geojson.geometry = parse(arcgis.geometry);
          geojson.properties = arcgis.attributes || {};
        }

        var inputSpatialReference = (arcgis.geometry) ? arcgis.geometry.spatialReference : arcgis.spatialReference;

        //convert spatial ref if needed
        if(inputSpatialReference && inputSpatialReference.wkid === 102100){
          geojson = Terraformer.toGeographic(geojson);
        }

        return new Terraformer.Primitive(geojson);
      }

      // GeoJSON -> ArcGIS
      function convert(input){
        var geojson = JSON.parse(JSON.stringify(input));
        var spatialReference = { wkid: 4326 };
        var result = {};
        var i;

        switch(geojson.type){
        case "Point":
          result.x = geojson.coordinates[0];
          result.y = geojson.coordinates[1];
          result.spatialReference = spatialReference;
          break;
        case "MultiPoint":
          result.points = geojson.coordinates;
          result.spatialReference = spatialReference;
          break;
        case "LineString":
          result.paths = [geojson.coordinates];
          result.spatialReference = spatialReference;
          break;
        case "MultiLineString":
          result.paths = geojson.coordinates;
          result.spatialReference = spatialReference;
          break;
        case "Polygon":
          result.rings = flattenPolygonRings(geojson.coordinates);
          result.spatialReference = spatialReference;
          break;
        case "MultiPolygon":
          result.rings = flattenMultiPolygonRings(geojson.coordinates);
          result.spatialReference = spatialReference;
          break;
        case "Feature":
          result.geometry = convert(geojson.geometry);
          result.attributes = geojson.properties;
          break;
        case "FeatureCollection":
          result = [];
          for (i = 0; i < geojson.features.length; i++){
            result.push(convert(geojson.features[i]));
          }
          break;
        case "GeometryCollection":
          result = [];
          for (i = 0; i < geojson.geometries.length; i++){
            result.push(convert(geojson.geometries[i]));
          }
          break;
        }

        return result;
      }

      exports.parse   = parse;
      exports.convert = convert;

      return exports;
    }));