/
peliasDocGenerators.js
171 lines (144 loc) · 5.1 KB
/
peliasDocGenerators.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
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
const through2 = require('through2');
const _ = require('lodash');
const iso3166 = require('iso3166-1');
const logger = require('pelias-logger').get('whosonfirst');
const Document = require('pelias-model').Document;
module.exports = {};
function assignField(hierarchyElement, wofDoc) {
switch (hierarchyElement.place_type) {
case 'neighbourhood':
case 'locality':
case 'borough':
case 'localadmin':
case 'county':
case 'macrocounty':
case 'macroregion':
case 'empire':
case 'continent':
case 'ocean':
case 'marinearea':
// the above place_types don't have abbrevations (yet)
wofDoc.addParent(hierarchyElement.place_type, hierarchyElement.name, hierarchyElement.id.toString());
break;
case 'postalcode':
var sans_whitespace = (hierarchyElement.name||'').replace(/\s/g, '');
if (sans_whitespace !== hierarchyElement.name) {
wofDoc.addParent(hierarchyElement.place_type, hierarchyElement.name, hierarchyElement.id.toString(), sans_whitespace);
} else {
wofDoc.addParent(hierarchyElement.place_type, hierarchyElement.name, hierarchyElement.id.toString());
}
break;
case 'region':
if (hierarchyElement.hasOwnProperty('abbreviation')) {
wofDoc.addParent(hierarchyElement.place_type, hierarchyElement.name, hierarchyElement.id.toString(), hierarchyElement.abbreviation);
} else {
wofDoc.addParent(hierarchyElement.place_type, hierarchyElement.name, hierarchyElement.id.toString());
}
break;
case 'dependency':
case 'country':
// this is country or dependency, so lookup and set the iso3 from abbreviation
if (iso3166.is2(hierarchyElement.abbreviation)) {
var iso3 = iso3166.to3(hierarchyElement.abbreviation);
wofDoc.addParent(hierarchyElement.place_type, hierarchyElement.name, hierarchyElement.id.toString(), iso3);
} else {
// there's no known abbreviation
wofDoc.addParent(hierarchyElement.place_type, hierarchyElement.name, hierarchyElement.id.toString());
}
break;
}
}
function addMultiLangAliases(wofDoc, name_langs) {
for (let lang in name_langs) {
for (let i = 0; i < name_langs[lang].length; i++) {
if (i === 0) {
wofDoc.setName(lang, name_langs[lang][i]);
} else {
wofDoc.setNameAlias(lang, name_langs[lang][i]);
}
}
}
}
// method that extracts the logic for Document creation. `hierarchy` is optional
function setupDocument(record, hierarchy) {
var wofDoc = new Document( 'whosonfirst', record.place_type, record.id );
if (record.name) {
wofDoc.setName('default', record.name);
// index a version of postcode which doesn't contain whitespace
if (record.place_type === 'postalcode' && typeof record.name === 'string') {
var sans_whitespace = record.name.replace(/\s/g, '');
if (sans_whitespace !== record.name) {
wofDoc.setNameAlias('default', sans_whitespace);
}
}
// index name aliases for all other records (where available)
else {
if (record.name_aliases.length) {
record.name_aliases.forEach(alias => {
wofDoc.setNameAlias('default', alias);
});
}
if (record.name_langs) {
addMultiLangAliases(wofDoc, record.name_langs);
}
}
}
wofDoc.setCentroid({ lat: record.lat, lon: record.lon });
// only set population if available
if (record.population) {
wofDoc.setPopulation(record.population);
}
// only set popularity if available
if (record.popularity) {
wofDoc.setPopularity(record.popularity);
}
// WOF bbox is defined as:
// lowerLeft.lon, lowerLeft.lat, upperRight.lon, upperRight.lat
// so convert to what ES understands
if (!_.isUndefined(record.bounding_box)) {
var parsedBoundingBox = record.bounding_box.split(',').map(parseFloat);
var marshaledBoundingBoxBox = {
upperLeft: {
lat: parsedBoundingBox[3],
lon: parsedBoundingBox[0]
},
lowerRight: {
lat: parsedBoundingBox[1],
lon: parsedBoundingBox[2]
}
};
wofDoc.setBoundingBox(marshaledBoundingBoxBox);
}
// a `hierarchy` is composed of potentially multiple WOF records, so iterate
// and assign fields
if (!_.isUndefined(hierarchy)) {
hierarchy.forEach(function(hierarchyElement) {
assignField(hierarchyElement, wofDoc);
});
}
// add self to parent hierarchy for postalcodes only
if (record.place_type === 'postalcode') {
assignField(record, wofDoc);
}
return wofDoc;
}
module.exports.create = function(hierarchy_finder) {
return through2.obj(function(record, enc, next) {
// if there are no hierarchies, then just return the doc as-is
var hierarchies = hierarchy_finder(record);
try {
if (hierarchies && hierarchies.length > 0) {
hierarchies.forEach(function(hierarchy) {
this.push(setupDocument(record, hierarchy));
}, this);
} else {
this.push(setupDocument(record));
}
}
catch (e) {
logger.error(`doc generator error: ${e.message}`);
logger.error(JSON.stringify(record, null, 2));
}
next();
});
};