jubilee/server/reducers/opencage.js
2018-11-04 12:03:30 +00:00

61 lines
1.5 KiB
JavaScript

const logger = require('log4js').getLogger('GeoCode 🔧');
const { get, isEmpty, has, uniq } = require('lodash');
logger.level = 'debug';
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
};
function formatResult (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,
'suburb': result.components.suburb,
'neighbourhood' : result.components.neighbourhood,
'village' : result.components.village,
'formatted' : result.formatted,
'extra': {
'flag' : result.annotations.flag,
'confidence': confidence,
'confidenceKM': ConfidenceInKM[result.confidence] || Number.NaN,
'map' : result.annotations.OSM.url
}
};
}
function reduceOpencage(result) {
logger.debug(JSON.stringify(result));
const results = [];
if (result && result.results instanceof Array)
for (let i = 0; i < result.results.length; i++)
results.push(formatResult(result.results[i]));
return results;
}
module.exports = { reduceOpencage };