# All files / lib/geocoder opencagegeocoder.js

93.55% Statements 58/62
70.59% Branches 24/34
100% Functions 7/7
93.55% Lines 58/62

    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
    1622x
    2x

    2x

    2x
    9x

    9x

    8x
    1x

    7x
    7x
    7x

    2x

    2x
    3x

    3x
    3x
    1x
    1x
    1x

    1x
    1x

    1x
    1x

    1x
    1x

    1x

    2x

    3x

    1x

    1x

    1x
    1x
    1x

    1x
    1x

    2x
    2x
    2x

    2x
    1x
    1x

    1x

    1x
    1x

    1x
    1x

    1x

    1x
    1x
    1x

    1x
    1x

    2x
    4x
    4x

    4x

    4x

    2x
     var util = require('util'),
      AbstractGeocoder = require('./abstractgeocoder');

    // http://geocoder.opencagedata.com/api.html#confidence
    var ConfidenceInKM = {
      10: 0.25,
      9: 0.5,
      8: 1,
      7: 5,
      6: 7.5,
      5: 10,
      4: 15,
      3: 20,
      2: 25,
      1: Number.POSITIVE_INFINITY,
      0: Number.NaN
    };

    /**
     * Constructor
     */
    var OpenCageGeocoder = function OpenCageGeocoder(httpAdapter, apiKey, options) {
      this.options = ['language'];

      OpenCageGeocoder.super_.call(this, httpAdapter, options);

      if (!apiKey || apiKey == 'undefined') {
        throw new Error(this.constructor.name + ' needs an apiKey');
      }

      this.apiKey = apiKey;
      this._endpoint = 'http://api.opencagedata.com/geocode/v1/json';
      this._ConfidenceInKM = ConfidenceInKM; // In case we need to support v1/v2 and this changes
    };

    util.inherits(OpenCageGeocoder, AbstractGeocoder);

    /**
     * Geocode
     * @param <string>   value    Value to geocode (Address)
     * @param <function> callback Callback method
     */
    OpenCageGeocoder.prototype._geocode = function (value, callback) {
      var _this = this;

      var params = this._getCommonParams();
      if (value.address) {
        Eif (value.bounds) {
          Eif (Array.isArray(value.bounds)) {
            params.bounds = value.bounds.join(',');
          }
          else {
            params.bounds = value.bounds;
          }
        }
        Eif (value.countryCode) {
          params.countrycode = value.countryCode;
        }
        Eif (value.limit) {
          params.limit = value.limit;
        }
        Eif (value.minConfidence) {
          params.min_confidence = value.minConfidence;
        }
        params.q = value.address;
      }
      else {
        params.q = value;
      }

      this.httpAdapter.get(this._endpoint, params, function (err, result) {

        Iif (err) {
          return callback(err);
        } else {

          var results = [];

          Eif (result && result.results instanceof Array) {
            for (var i = 0; i < result.results.length; i++) {
              results.push(_this._formatResult(result.results[i]));
            }
          }

          results.raw = result;
          callback(false, results);
        }

      });

    };

    OpenCageGeocoder.prototype._formatResult = function (result) {
      var confidence = result.confidence || 0;
      return {
        'latitude': result.geometry.lat,
        'longitude': result.geometry.lng,
        'country': result.components.country,
        'city': result.components.city,
        'state': result.components.state,
        'zipcode': result.components.postcode,
        'streetName': result.components.road,
        'streetNumber': result.components.house_number,
        'countryCode': result.components.country_code,
        'county': result.components.county,
        'extra': {
          confidence: confidence,
          confidenceKM: this._ConfidenceInKM[result.confidence] || Number.NaN
        }
      };
    };

    /**
     * Reverse geocoding
     * @param {lat:<number>,lon:<number>}  lat: Latitude, lon: Longitude
     * @param <function> callback Callback method
     */
    OpenCageGeocoder.prototype._reverse = function (query, callback) {
      var lat = query.lat;
      var lng = query.lon;

      var _this = this;

      var params = this._getCommonParams();
      params.q = lat + ' ' + lng;

      this.httpAdapter.get(this._endpoint, params, function (err, result) {
        Iif (err) {
          callback(err);
        } else {
          var results = [];

          Eif (result && result.results instanceof Array) {
            for (var i = 0; i < result.results.length; i++) {
              results.push(_this._formatResult(result.results[i]));
            }
          }

          results.raw = result;
          callback(false, results);
        }
      });
    };

    /**
     * Prepare common params
     *
     * @return <Object> common params
     */
    OpenCageGeocoder.prototype._getCommonParams = function () {
      var params = {};
      params.key = this.apiKey;

      Iif (this.options.language) {
        params.language = this.options.language;
      }

      return params;
    };

    module.exports = OpenCageGeocoder;