# Code coverage report for ol/source/formatvectorsource.js

## Statements: 34.55% (19 / 55) Branches: 0% (0 / 22) Functions: 25% (1 / 4) Lines: 34.55% (19 / 55) Ignored: none

All files » ol/source/ » formatvectorsource.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

    1

    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1

    1

    3

    3

    1

    1

    1

     // FIXME consider delaying feature reading so projection can be provided by
    // consumer (e.g. the view)

    goog.provide('ol.source.FormatVector');

    goog.require('goog.asserts');
    goog.require('goog.dispose');
    goog.require('goog.events');
    goog.require('goog.net.EventType');
    goog.require('goog.net.XhrIo');
    goog.require('goog.net.XhrIo.ResponseType');
    goog.require('goog.userAgent');
    goog.require('ol.format.FormatType');
    goog.require('ol.has');
    goog.require('ol.source.State');
    goog.require('ol.source.Vector');
    goog.require('ol.xml');

    /**
     * @classdesc
     * Abstract base class; normally only used for creating subclasses and not
     * instantiated in apps.
     * Base class for vector sources in one of the supported formats.
     *
     * @constructor
     * @extends {ol.source.Vector}
     * @param {olx.source.FormatVectorOptions} options Options.
     */
    ol.source.FormatVector = function(options) {

      goog.base(this, {
        attributions: options.attributions,
        logo: options.logo,
        projection: options.projection
      });

      /**
       * @protected
       * @type {ol.format.Feature}
       */
      this.format = options.format;

    };
    goog.inherits(ol.source.FormatVector, ol.source.Vector);

    /**
     * @param {goog.Uri|string} url URL.
     * @param {function(this: T, Array.<ol.Feature>)} success Success Callback.
     * @param {function(this: T)} error Error callback.
     * @param {T} thisArg Value to use as `this` when executing `success` or
     *     `error`.
     * @template T
     */
    ol.source.FormatVector.prototype.loadFeaturesFromURL =
        function(url, success, error, thisArg) {
      var xhrIo = new goog.net.XhrIo();
      var type = this.format.getType();
      var responseType;
      // FIXME maybe use ResponseType.DOCUMENT?
      if (type == ol.format.FormatType.BINARY &&
          ol.has.ARRAY_BUFFER) {
        responseType = goog.net.XhrIo.ResponseType.ARRAY_BUFFER;
      } else {
        responseType = goog.net.XhrIo.ResponseType.TEXT;
      }
      xhrIo.setResponseType(responseType);
      goog.events.listen(xhrIo, goog.net.EventType.COMPLETE,
          /**
           * @param {Event} event Event.
           * @private
           * @this {ol.source.FormatVector}
           */
          function(event) {
            var xhrIo = event.target;
            goog.asserts.assertInstanceof(xhrIo, goog.net.XhrIo,
                'event.target/xhrIo is an instance of goog.net.XhrIo');
            if (xhrIo.isSuccess()) {
              var type = this.format.getType();
              /** @type {ArrayBuffer|Document|Node|Object|string|undefined} */
              var source;
              if (type == ol.format.FormatType.BINARY &&
                  ol.has.ARRAY_BUFFER) {
                source = xhrIo.getResponse();
                goog.asserts.assertInstanceof(source, ArrayBuffer,
                    'source is an instance of ArrayBuffer');
              } else if (type == ol.format.FormatType.JSON) {
                source = xhrIo.getResponseText();
              } else if (type == ol.format.FormatType.TEXT) {
                source = xhrIo.getResponseText();
              } else if (type == ol.format.FormatType.XML) {
                if (!goog.userAgent.IE) {
                  source = xhrIo.getResponseXml();
                }
                if (!goog.isDefAndNotNull(source)) {
                  source = ol.xml.parse(xhrIo.getResponseText());
                }
              } else {
                goog.asserts.fail('unexpected format type');
              }
              if (goog.isDefAndNotNull(source)) {
                success.call(thisArg, this.readFeatures(source));
              } else {
                this.setState(ol.source.State.ERROR);
                goog.asserts.fail('undefined or null source');
              }
            } else {
              error.call(thisArg);
            }
            goog.dispose(xhrIo);
          }, false, this);
      xhrIo.send(url);
    };

    /**
     * @param {ArrayBuffer|Document|Node|Object|string} source Source.
     * @return {Array.<ol.Feature>} Features.
     * @api
     */
    ol.source.FormatVector.prototype.readFeatures = function(source) {
      var format = this.format;
      var projection = this.getProjection();
      return format.readFeatures(source, {featureProjection: projection});
    };