All files / lib/geocoder openstreetmapgeocoder.js

90.54% Statements 67/74
59.38% Branches 19/32
100% Functions 8/8
90.54% Lines 67/74
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 1664x 4x         4x 26x   26x     4x   4x   4x             4x 6x   6x 6x 6x 4x   2x 6x 6x     6x     6x 4x       4x   4x       4x 4x 3x           4x 4x             4x   5x 5x 5x     5x 5x 5x     5x 5x 5x     5x                                       4x   5x   5x 5x 13x 13x   5x   5x 5x     5x       5x 5x 3x       2x     5x 5x                   4x 11x   11x 34x 34x 30x   4x     4x     11x     4x 11x 11x     4x  
var util             = require('util'),
    AbstractGeocoder = require('./abstractgeocoder');
 
/**
 * Constructor
 */
var OpenStreetMapGeocoder = function OpenStreetMapGeocoder(httpAdapter, options) {
    this.options = ['language','email','apiKey'];
 
    OpenStreetMapGeocoder.super_.call(this, httpAdapter, options);
};
 
util.inherits(OpenStreetMapGeocoder, AbstractGeocoder);
 
OpenStreetMapGeocoder.prototype._endpoint = 'http://nominatim.openstreetmap.org/search';
 
OpenStreetMapGeocoder.prototype._endpoint_reverse = 'http://nominatim.openstreetmap.org/reverse';
 
/**
* Geocode
* @param <string|object>   value    Value to geocode (Address or parameters, as specified at https://wiki.openstreetmap.org/wiki/Nominatim#Parameters)
* @param <function> callback Callback method
*/
OpenStreetMapGeocoder.prototype._geocode = function(value, callback) {
    var _this = this;
 
    var params = this._getCommonParams();
    params.addressdetails = 1;
    if (typeof value == 'string') {
      params.q = value;
    } else {
      for (var k in value) {
        var v = value[k];
        params[k] = v;
      }
    }
    this._forceParams(params);
 
 
    this.httpAdapter.get(this._endpoint , params, function(err, result) {
        Iif (err) {
            return callback(err);
        } else {
 
            var results = [];
 
            Iif(result.error) {
              return callback(new Error(result.error));
            }
 
            Eif (result instanceof Array) {
              for (var i = 0; i < result.length; i++) {
                results.push(_this._formatResult(result[i]));
              }
            } else {
              results.push(_this._formatResult(result));
            }
 
            results.raw = result;
            callback(false, results);
        }
 
    });
 
};
 
OpenStreetMapGeocoder.prototype._formatResult = function(result) {
 
    var countryCode = result.address.country_code;
    Eif (countryCode) {
        countryCode = countryCode.toUpperCase();
    }
 
    var latitude = result.lat;
    Eif (latitude) {
      latitude = parseFloat(latitude);
    }
 
    var longitude = result.lon;
    Eif (longitude) {
      longitude = parseFloat(longitude);
    }
 
    return {
        'latitude' : latitude,
        'longitude' : longitude,
        'formattedAddress': result.display_name,
        'country' : result.address.country,
        'city' : result.address.city || result.address.town || result.address.village || result.address.hamlet,
        'state': result.address.state,
        'zipcode' : result.address.postcode,
        'streetName': result.address.road || result.address.cycleway,
        'streetNumber' : result.address.house_number,
        'countryCode' : countryCode,
        'neighbourhood': result.address.neighbourhood || ""
    };
};
 
/**
* 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
*/
OpenStreetMapGeocoder.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 = [];
          if (result instanceof Array) {
            for (var i = 0; i < result.length; i++) {
              results.push(_this._formatResult(result[i]));
            }
          } else {
            results.push(_this._formatResult(result));
          }
 
          results.raw = result;
          callback(false, results);
        }
    });
};
 
/**
* Prepare common params
*
* @return <Object> common params
*/
OpenStreetMapGeocoder.prototype._getCommonParams = function(){
    var params = {};
 
    for (var k in this.options) {
      var v = this.options[k];
      if (!v) {
        continue;
      }
      Iif (k === 'language') {
        k = 'accept-language';
      }
      params[k] = v;
    }
 
    return params;
};
 
OpenStreetMapGeocoder.prototype._forceParams = function(params){
    params.format = 'json';
    params.addressdetails = 1;
};
 
module.exports = OpenStreetMapGeocoder;