# all files / ol/format/ featureformat.js

100% Statements 32/32
90.32% Branches 28/31
100% Functions 4/4
100% Lines 32/32

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

    1×
    1×

    1×

    537×

    1×

    1×
    239×
    239×
    56×

    239×

    1×
    564×
    564×
    98×

    564×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1308×

    1308×

    1308×

    59×
    56×

    3×

    1249×

     goog.provide('ol.format.Feature');

    goog.require('ol.geom.Geometry');
    goog.require('ol.proj');

    /**
     * @classdesc
     * Abstract base class; normally only used for creating subclasses and not
     * instantiated in apps.
     * Base class for feature formats.
     * {ol.format.Feature} subclasses provide the ability to decode and encode
     * {@link ol.Feature} objects from a variety of commonly used geospatial
     * file formats.  See the documentation for each format for more details.
     *
     * @constructor
     * @api stable
     */
    ol.format.Feature = function() {

      /**
       * @protected
       * @type {ol.proj.Projection}
       */
      this.defaultDataProjection = null;

    };

    /**
     * @return {Array.<string>} Extensions.
     */
    ol.format.Feature.prototype.getExtensions = goog.abstractMethod;

    /**
     * Adds the data projection to the read options.
     * @param {Document|Node|Object|string} source Source.
     * @param {olx.format.ReadOptions=} opt_options Options.
     * @return {olx.format.ReadOptions|undefined} Options.
     * @protected
     */
    ol.format.Feature.prototype.getReadOptions = function(source, opt_options) {
      var options;
      if (opt_options) {
        options = {
          dataProjection: opt_options.dataProjection ?
              opt_options.dataProjection : this.readProjection(source),
          featureProjection: opt_options.featureProjection
        };
      }
      return this.adaptOptions(options);
    };

    /**
     * Sets the `defaultDataProjection` on the options, if no `dataProjection`
     * is set.
     * @param {olx.format.WriteOptions|olx.format.ReadOptions|undefined} options
     *     Options.
     * @protected
     * @return {olx.format.WriteOptions|olx.format.ReadOptions|undefined}
     *     Updated options.
     */
    ol.format.Feature.prototype.adaptOptions = function(options) {
      var updatedOptions;
      if (options) {
        updatedOptions = {
          featureProjection: options.featureProjection,
          dataProjection: options.dataProjection ?
              options.dataProjection : this.defaultDataProjection,
          rightHanded: options.rightHanded
        };
      }
      return updatedOptions;
    };

    /**
     * @return {ol.format.FormatType} Format.
     */
    ol.format.Feature.prototype.getType = goog.abstractMethod;

    /**
     * Read a single feature from a source.
     *
     * @param {Document|Node|Object|string} source Source.
     * @param {olx.format.ReadOptions=} opt_options Read options.
     * @return {ol.Feature} Feature.
     */
    ol.format.Feature.prototype.readFeature = goog.abstractMethod;

    /**
     * Read all features from a source.
     *
     * @param {Document|Node|ArrayBuffer|Object|string} source Source.
     * @param {olx.format.ReadOptions=} opt_options Read options.
     * @return {Array.<ol.Feature>} Features.
     */
    ol.format.Feature.prototype.readFeatures = goog.abstractMethod;

    /**
     * Read a single geometry from a source.
     *
     * @param {Document|Node|Object|string} source Source.
     * @param {olx.format.ReadOptions=} opt_options Read options.
     * @return {ol.geom.Geometry} Geometry.
     */
    ol.format.Feature.prototype.readGeometry = goog.abstractMethod;

    /**
     * Read the projection from a source.
     *
     * @param {Document|Node|Object|string} source Source.
     * @return {ol.proj.Projection} Projection.
     */
    ol.format.Feature.prototype.readProjection = goog.abstractMethod;

    /**
     * Encode a feature in this format.
     *
     * @param {ol.Feature} feature Feature.
     * @param {olx.format.WriteOptions=} opt_options Write options.
     * @return {string} Result.
     */
    ol.format.Feature.prototype.writeFeature = goog.abstractMethod;

    /**
     * Encode an array of features in this format.
     *
     * @param {Array.<ol.Feature>} features Features.
     * @param {olx.format.WriteOptions=} opt_options Write options.
     * @return {string} Result.
     */
    ol.format.Feature.prototype.writeFeatures = goog.abstractMethod;

    /**
     * Write a single geometry in this format.
     *
     * @param {ol.geom.Geometry} geometry Geometry.
     * @param {olx.format.WriteOptions=} opt_options Write options.
     * @return {string} Result.
     */
    ol.format.Feature.prototype.writeGeometry = goog.abstractMethod;

    /**
     * @param {ol.geom.Geometry|ol.Extent} geometry Geometry.
     * @param {boolean} write Set to true for writing, false for reading.
     * @param {(olx.format.WriteOptions|olx.format.ReadOptions)=} opt_options
     *     Options.
     * @return {ol.geom.Geometry|ol.Extent} Transformed geometry.
     * @protected
     */
    ol.format.Feature.transformWithOptions = function(
        geometry, write, opt_options) {
      var featureProjection = opt_options ?
          ol.proj.get(opt_options.featureProjection) : null;
      var dataProjection = opt_options ?
          ol.proj.get(opt_options.dataProjection) : null;
      if (featureProjection && dataProjection &&
          !ol.proj.equivalent(featureProjection, dataProjection)) {
        if (geometry instanceof ol.geom.Geometry) {
          return (write ? geometry.clone() : geometry).transform(
              write ? featureProjection : dataProjection,
              write ? dataProjection : featureProjection);
        } else {
          // FIXME this is necessary because ol.format.GML treats extents
          // as geometries
          return ol.proj.transformExtent(
              write ? geometry.slice() : geometry,
              write ? featureProjection : dataProjection,
              write ? dataProjection : featureProjection);
        }
      } else {
        return geometry;
      }
    };