forked from scmorrison/node-geonames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (133 loc) · 4.22 KB
/
index.js
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
var
mongoose = require('mongoose'),
async = require('async'),
Geoname,
GeonameFactory = require('./models/geoname');
function Geonames() {
}
module.exports = new Geonames();
function getWords(s) {
if (s) {
var a = s.split(/[ \f\n\r\t\v\u00A0\u201C\u201D\u2026\u003F\u00BF\u007E\u2028\u2029\u201C\u201D.,:;¡\¿\?\!\-\_\\\/\(\)\"\'\&\~\^]+/);
}
return s ? a : [];
}
Geonames.prototype.connect = function(mongodb) {
GeoName = GeonameFactory(mongodb);
return GeoName;
}
Geonames.prototype.findByName = function(name, limit, cb) {
Geoname.findByName(name, limit, cb);
}
Geonames.prototype.findByNameAndUTCOffset = function(name, UTCOffset, limit, cb) {
Geoname.findByNameAndUTCOffset(name, UTCOffset, limit, cb);
}
Geonames.prototype.findByCoords = function(lon, lat, limit, cb) {
Geoname.findByCoords(lon, lat, limit, cb);
}
Geonames.prototype.findByLocation = function(location, utc_offset, cb) {
var coords = location.match(/(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)/);
if (coords && coords.length == 5) {
//console.log('>> By Coords');
Geoname.findByCoords(parseFloat(coords[3]), parseFloat(coords[1]), 1, function(err, results) {
if (err) return cb(err);
//console.log(' ---', location, results);
if (results && results.length > 0) {
cb(null, results[0]);
}
else cb();
});
}
else {
var winners = [];
var winnersByName = [];
var found = false;
var words = getWords(location);
words = words.slice(0, 3);
if (words.length > 2) {
//Generate consecutives word couples
var locations = words.slice(0, 3);
var i = 0;
locations.push(location); //Add the full location as a possible place
async.forEachSeries(words, function(word, next) {
if (i == words.length - 1) return next();
locations.push(words[i] + ' ' + words[i + 1]);
i++;
return next();
}, function() {
processLocations(locations);
});
}
else {
if (words.length > 1) words.push(location); //Add the full location as a possible place
processLocations(words);
}
function processLocations(locations) {
//console.log('locations', locations);
async.forEachSeries(locations, function(location, next) {
if (utc_offset !== undefined) {
Geoname.findByNameAndUTCOffset(location, utc_offset, 1, function(err, results) {
if (err) return next(err);
if (results && results.length > 0) {
//console.log(' >> By UTC Offset ', results[0]);
winners.push(results[0]);
found = true;
}
Geoname.findByName(location, 1, function(err, results) {
if (err) return next(err);
if (results && results.length > 0) {
//console.log(' >> By Name ', results[0]);
winnersByName.push(results[0]);
}
next();
});
});
}
else {
Geoname.findByName(location, 1, function(err, results) {
if (err) return next(err);
if (results && results.length > 0) {
//console.log(' >> By Name ', results[0]);
winners.push(results[0]);
}
next();
});
}
}, function(err) {
if (err) return cb(err);
async.series([
function stepFilterWinnersByName(callback) {
//console.log('stepFilterWinnersByName', winnersByName.length);
if (!found || winnersByName.length == 0) return callback();
//If found someone by UTC offset then add only countries without offset info
async.filter(winnersByName, function(place, next) {
return next(place.offset_raw === null);
}, function(results) {
winnersByName = results;
callback();
});
},
function stepConcatWinnersByName(callback) {
//console.log('stepConcatWinnersByName');
if (winnersByName.length == 0) return callback();
//console.log('winners', winners, winnersByName);
async.forEach(winnersByName, function(winner, next) {
winners.push(winner);
return next();
}, callback);
},
function stepSortWinners(callback) {
//console.log('stepSortWinners');
if (winners.length == 0) return cb();
//Sort winners by population DESC
async.sortBy(winners, function(item, next) {
return next(null, -1 * item.population);
}, function(err, results) {
return cb(err, results[0]);
});
}
]);
});
}
}
}