# All files / lib/geocoder opendatafrancegeocoder.js

89.89% Statements 80/89
63.64% Branches 28/44
100% Functions 8/8
89.89% Lines 80/89

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

    2x
    11x

    11x

    2x

    2x

    2x

    2x
    6x

    6x

    6x
    2x

    4x
    4x

    4x
    1x
    1x

    4x
    1x

    4x
    4x

    4x

    6x

    6x
    5x

    5x

    5x

    5x
    5x
    5x

    5x
    5x

    2x

    6x
    6x
    6x

    6x
    6x
    6x

    6x

    6x

    6x
    3x
    3x
    3x

    3x
    1x
    1x
    2x
    1x
    1x
    1x

    6x

    2x

    1x

    1x
    1x
    2x
    2x

    1x

    1x
    1x

    1x

    1x

    1x
    1x
    1x

    1x
    1x

    2x
    7x

    7x
    21x
    21x
    21x

    7x

    2x
    7x

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

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

        OpendataFranceGeocoder.super_.call(this, httpAdapter, options);
    };

    util.inherits(OpendataFranceGeocoder, AbstractGeocoder);

    OpendataFranceGeocoder.prototype._endpoint = 'http://api-adresse.data.gouv.fr/search';

    OpendataFranceGeocoder.prototype._endpoint_reverse = 'http://api-adresse.data.gouv.fr/reverse';

    /**
    * Geocode
    * @param <string|object>   value    Value to geocode (Address or parameters, as specified at https://opendatafrance/api/)
    * @param <function> callback Callback method
    */
    OpendataFranceGeocoder.prototype._geocode = function(value, callback) {
        var _this = this;

        var params = this._getCommonParams();

        if (typeof value == 'string') {
          params.q = value;
        } else {
          Eif (value.address) {
            params.q = value.address;
          }
          if (value.lat && value.lon) {
            params.lat = value.lat;
            params.lon = value.lon;
          }
          if (value.zipcode) {
            params.postcode = value.zipcode;
          }
          Eif (value.type) {
            params.type = value.type;
          }
          Iif (value.citycode) {
            params.citycode = value.citycode;
          }
        }
        this._forceParams(params);

        this.httpAdapter.get(this._endpoint, params, function(err, result) {
            Iif (err) {
                return callback(err);
            } else {

                Iif (result.error) {
                  return callback(new Error(result.error));
                }

                var results = [];

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

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

        });

    };

    OpendataFranceGeocoder.prototype._formatResult = function(result) {

        var latitude = result.geometry.coordinates[1];
        Eif (latitude) {
          latitude = parseFloat(latitude);
        }

        var longitude = result.geometry.coordinates[0];
        Eif (longitude) {
          longitude = parseFloat(longitude);
        }

        var properties = result.properties;

        var formatedResult = {
            latitude : latitude,
            longitude : longitude,
            state : properties.context,
            city : properties.city,
            zipcode : properties.postcode,
            citycode : properties.citycode,
            countryCode : 'FR',
            country : 'France',
            type: properties.type,
            id: properties.id
        };

        if (properties.type === 'housenumber') {
          formatedResult.streetName = properties.street;
          formatedResult.streetNumber = properties.housenumber;
        } else Iif (properties.type === 'street') {
          formatedResult.streetName = properties.name;
        } else if (properties.type === 'city') {
          formatedResult.population = properties.population;
          formatedResult.adm_weight = properties.adm_weight;
        } else if (properties.type === 'village') {
          formatedResult.population = properties.population;
        } else Eif (properties.type === 'locality') {
          formatedResult.streetName = properties.name;
        }

        return formatedResult;
    };

    /**
    * Reverse geocoding
    * @param {lat:<number>,lon:<number>, ...}  lat: Latitude, lon: Longitude, ... see https://wiki.openstreetmap.org/wiki/Nominatim#Parameters_2
    * @param <function> callback Callback method
    */
    OpendataFranceGeocoder.prototype._reverse = function(query, callback) {

        var _this = this;

        var params = this._getCommonParams();
        for (var k in query) {
          var v = query[k];
          params[k] = v;
        }
        this._forceParams(params);

        this.httpAdapter.get(this._endpoint_reverse , params, function(err, result) {
            Iif (err) {
                return callback(err);
            } else {

              Iif(result.error) {
                return callback(new Error(result.error));
              }

              var results = [];

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

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

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

        for (var k in this.options) {
          var v = this.options[k];
          Eif (!v) {
            continue;
          }
          if (k === 'language') {
            k = 'accept-language';
          }
          params[k] = v;
        }

        return params;
    };

    OpendataFranceGeocoder.prototype._forceParams = function(params){
      params.limit = 20;
    };

    module.exports = OpendataFranceGeocoder;