Skip to content

Commit 1c4af9b

Browse files
committed
Linting/formatting
1 parent 928ad92 commit 1c4af9b

File tree

1 file changed

+82
-76
lines changed

1 file changed

+82
-76
lines changed

src/lib/geo_location_utils.js

Lines changed: 82 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ var locationmodeToIdFinder = {
2222
};
2323

2424
function countryNameToISO3(countryName) {
25-
for(var i = 0; i < countryIds.length; i++) {
25+
for (var i = 0; i < countryIds.length; i++) {
2626
var iso3 = countryIds[i];
2727
var regex = new RegExp(countryRegex[iso3]);
2828

29-
if(regex.test(countryName.trim().toLowerCase())) return iso3;
29+
if (regex.test(countryName.trim().toLowerCase())) return iso3;
3030
}
3131

3232
loggers.log('Unrecognized country name: ' + countryName + '.');
@@ -35,40 +35,39 @@ function countryNameToISO3(countryName) {
3535
}
3636

3737
function locationToFeature(locationmode, location, features) {
38-
if(!location || typeof location !== 'string') return false;
38+
if (!location || typeof location !== 'string') return false;
3939

4040
var locationId = locationmodeToIdFinder[locationmode](location);
4141
var filteredFeatures;
4242
var f, i;
4343

44-
if(locationId) {
45-
if(locationmode === 'USA-states') {
44+
if (locationId) {
45+
if (locationmode === 'USA-states') {
4646
// Filter out features out in USA
4747
//
4848
// This is important as the Natural Earth files
4949
// include state/provinces from USA, Canada, Australia and Brazil
5050
// which have some overlay in their two-letter ids. For example,
5151
// 'WA' is used for both Washington state and Western Australia.
5252
filteredFeatures = [];
53-
for(i = 0; i < features.length; i++) {
53+
for (i = 0; i < features.length; i++) {
5454
f = features[i];
55-
if(f.properties && f.properties.gu && f.properties.gu === 'USA') {
55+
if (f.properties && f.properties.gu && f.properties.gu === 'USA') {
5656
filteredFeatures.push(f);
5757
}
5858
}
5959
} else {
6060
filteredFeatures = features;
6161
}
6262

63-
for(i = 0; i < filteredFeatures.length; i++) {
63+
for (i = 0; i < filteredFeatures.length; i++) {
6464
f = filteredFeatures[i];
65-
if(f.id === locationId) return f;
65+
if (f.id === locationId) return f;
6666
}
6767

68-
loggers.log([
69-
'Location with id', locationId,
70-
'does not have a matching topojson feature at this resolution.'
71-
].join(' '));
68+
loggers.log(
69+
['Location with id', locationId, 'does not have a matching topojson feature at this resolution.'].join(' ')
70+
);
7271
}
7372

7473
return false;
@@ -83,46 +82,43 @@ function feature2polygons(feature) {
8382
var appendPolygon, j, k, m;
8483

8584
function doesCrossAntiMerdian(pts) {
86-
for(var l = 0; l < pts.length - 1; l++) {
87-
if(pts[l][0] > 0 && pts[l + 1][0] < 0) return l;
85+
for (var l = 0; l < pts.length - 1; l++) {
86+
if (pts[l][0] > 0 && pts[l + 1][0] < 0) return l;
8887
}
8988
return null;
9089
}
9190

92-
if(loc === 'RUS' || loc === 'FJI') {
91+
if (loc === 'RUS' || loc === 'FJI') {
9392
// Russia and Fiji have landmasses that cross the antimeridian,
9493
// we need to add +360 to their longitude coordinates, so that
9594
// polygon 'contains' doesn't get confused when crossing the antimeridian.
9695
//
9796
// Note that other countries have polygons on either side of the antimeridian
9897
// (e.g. some Aleutian island for the USA), but those don't confuse
9998
// the 'contains' method; these are skipped here.
100-
appendPolygon = function(_pts) {
99+
appendPolygon = function (_pts) {
101100
var pts;
102101

103-
if(doesCrossAntiMerdian(_pts) === null) {
102+
if (doesCrossAntiMerdian(_pts) === null) {
104103
pts = _pts;
105104
} else {
106105
pts = new Array(_pts.length);
107-
for(m = 0; m < _pts.length; m++) {
106+
for (m = 0; m < _pts.length; m++) {
108107
// do not mutate calcdata[i][j].geojson !!
109-
pts[m] = [
110-
_pts[m][0] < 0 ? _pts[m][0] + 360 : _pts[m][0],
111-
_pts[m][1]
112-
];
108+
pts[m] = [_pts[m][0] < 0 ? _pts[m][0] + 360 : _pts[m][0], _pts[m][1]];
113109
}
114110
}
115111

116112
polygons.push(polygon.tester(pts));
117113
};
118-
} else if(loc === 'ATA') {
114+
} else if (loc === 'ATA') {
119115
// Antarctica has a landmass that wraps around every longitudes which
120116
// confuses the 'contains' methods.
121-
appendPolygon = function(pts) {
117+
appendPolygon = function (pts) {
122118
var crossAntiMeridianIndex = doesCrossAntiMerdian(pts);
123119

124120
// polygon that do not cross anti-meridian need no special handling
125-
if(crossAntiMeridianIndex === null) {
121+
if (crossAntiMeridianIndex === null) {
126122
return polygons.push(polygon.tester(pts));
127123
}
128124

@@ -135,10 +131,10 @@ function feature2polygons(feature) {
135131
var stitch = new Array(pts.length + 1);
136132
var si = 0;
137133

138-
for(m = 0; m < pts.length; m++) {
139-
if(m > crossAntiMeridianIndex) {
134+
for (m = 0; m < pts.length; m++) {
135+
if (m > crossAntiMeridianIndex) {
140136
stitch[si++] = [pts[m][0] + 360, pts[m][1]];
141-
} else if(m === crossAntiMeridianIndex) {
137+
} else if (m === crossAntiMeridianIndex) {
142138
stitch[si++] = pts[m];
143139
stitch[si++] = [pts[m][0], -90];
144140
} else {
@@ -155,21 +151,21 @@ function feature2polygons(feature) {
155151
};
156152
} else {
157153
// otherwise using same array ref is fine
158-
appendPolygon = function(pts) {
154+
appendPolygon = function (pts) {
159155
polygons.push(polygon.tester(pts));
160156
};
161157
}
162158

163-
switch(geometry.type) {
159+
switch (geometry.type) {
164160
case 'MultiPolygon':
165-
for(j = 0; j < coords.length; j++) {
166-
for(k = 0; k < coords[j].length; k++) {
161+
for (j = 0; j < coords.length; j++) {
162+
for (k = 0; k < coords[j].length; k++) {
167163
appendPolygon(coords[j][k]);
168164
}
169165
}
170166
break;
171167
case 'Polygon':
172-
for(j = 0; j < coords.length; j++) {
168+
for (j = 0; j < coords.length; j++) {
173169
appendPolygon(coords[j]);
174170
}
175171
break;
@@ -185,7 +181,7 @@ function getTraceGeojson(trace) {
185181

186182
// This should not happen, but just in case something goes
187183
// really wrong when fetching the GeoJSON
188-
if(!isPlainObject(geojsonIn)) {
184+
if (!isPlainObject(geojsonIn)) {
189185
loggers.error('Oops ... something went wrong when fetching ' + g);
190186
return false;
191187
}
@@ -197,15 +193,15 @@ function extractTraceFeature(calcTrace) {
197193
var trace = calcTrace[0].trace;
198194

199195
var geojsonIn = getTraceGeojson(trace);
200-
if(!geojsonIn) return false;
196+
if (!geojsonIn) return false;
201197

202198
var lookup = {};
203199
var featuresOut = [];
204200
var i;
205201

206-
for(i = 0; i < trace._length; i++) {
202+
for (i = 0; i < trace._length; i++) {
207203
var cdi = calcTrace[i];
208-
if(cdi.loc || cdi.loc === 0) {
204+
if (cdi.loc || cdi.loc === 0) {
209205
lookup[cdi.loc] = cdi;
210206
}
211207
}
@@ -214,10 +210,10 @@ function extractTraceFeature(calcTrace) {
214210
var id = nestedProperty(fIn, trace.featureidkey || 'id').get();
215211
var cdi = lookup[id];
216212

217-
if(cdi) {
213+
if (cdi) {
218214
var geometry = fIn.geometry;
219215

220-
if(geometry.type === 'Polygon' || geometry.type === 'MultiPolygon') {
216+
if (geometry.type === 'Polygon' || geometry.type === 'MultiPolygon') {
221217
var fOut = {
222218
type: 'Feature',
223219
id: id,
@@ -238,11 +234,15 @@ function extractTraceFeature(calcTrace) {
238234

239235
featuresOut.push(fOut);
240236
} else {
241-
loggers.log([
242-
'Location', cdi.loc, 'does not have a valid GeoJSON geometry.',
243-
'Traces with locationmode *geojson-id* only support',
244-
'*Polygon* and *MultiPolygon* geometries.'
245-
].join(' '));
237+
loggers.log(
238+
[
239+
'Location',
240+
cdi.loc,
241+
'does not have a valid GeoJSON geometry.',
242+
'Traces with locationmode *geojson-id* only support',
243+
'*Polygon* and *MultiPolygon* geometries.'
244+
].join(' ')
245+
);
246246
}
247247
}
248248

@@ -251,31 +251,36 @@ function extractTraceFeature(calcTrace) {
251251
delete lookup[id];
252252
}
253253

254-
switch(geojsonIn.type) {
254+
switch (geojsonIn.type) {
255255
case 'FeatureCollection':
256256
var featuresIn = geojsonIn.features;
257-
for(i = 0; i < featuresIn.length; i++) {
257+
for (i = 0; i < featuresIn.length; i++) {
258258
appendFeature(featuresIn[i]);
259259
}
260260
break;
261261
case 'Feature':
262262
appendFeature(geojsonIn);
263263
break;
264264
default:
265-
loggers.warn([
266-
'Invalid GeoJSON type', (geojsonIn.type || 'none') + '.',
267-
'Traces with locationmode *geojson-id* only support',
268-
'*FeatureCollection* and *Feature* types.'
269-
].join(' '));
265+
loggers.warn(
266+
[
267+
'Invalid GeoJSON type',
268+
(geojsonIn.type || 'none') + '.',
269+
'Traces with locationmode *geojson-id* only support',
270+
'*FeatureCollection* and *Feature* types.'
271+
].join(' ')
272+
);
270273
return false;
271274
}
272275

273-
for(var loc in lookup) {
274-
loggers.log([
275-
'Location *' + loc + '*',
276-
'does not have a matching feature with id-key',
277-
'*' + trace.featureidkey + '*.'
278-
].join(' '));
276+
for (var loc in lookup) {
277+
loggers.log(
278+
[
279+
'Location *' + loc + '*',
280+
'does not have a matching feature with id-key',
281+
'*' + trace.featureidkey + '*.'
282+
].join(' ')
283+
);
279284
}
280285

281286
return featuresOut;
@@ -289,14 +294,14 @@ function findCentroid(feature) {
289294
var geometry = feature.geometry;
290295
var poly;
291296

292-
if(geometry.type === 'MultiPolygon') {
297+
if (geometry.type === 'MultiPolygon') {
293298
var coords = geometry.coordinates;
294299
var maxArea = 0;
295300

296-
for(var i = 0; i < coords.length; i++) {
297-
var polyi = {type: 'Polygon', coordinates: coords[i]};
301+
for (var i = 0; i < coords.length; i++) {
302+
var polyi = { type: 'Polygon', coordinates: coords[i] };
298303
var area = turfArea(polyi);
299-
if(area > maxArea) {
304+
if (area > maxArea) {
300305
maxArea = area;
301306
poly = polyi;
302307
}
@@ -313,13 +318,14 @@ function fetchTraceGeoData(calcData) {
313318
var promises = [];
314319

315320
function fetch(url) {
316-
return new Promise(function(resolve, reject) {
317-
d3.json(url, function(err, d) {
318-
if(err) {
321+
return new Promise(function (resolve, reject) {
322+
d3.json(url, function (err, d) {
323+
if (err) {
319324
delete PlotlyGeoAssets[url];
320-
var msg = err.status === 404 ?
321-
('GeoJSON at URL "' + url + '" does not exist.') :
322-
('Unexpected error while fetching from ' + url);
325+
var msg =
326+
err.status === 404
327+
? 'GeoJSON at URL "' + url + '" does not exist.'
328+
: 'Unexpected error while fetching from ' + url;
323329
return reject(new Error(msg));
324330
}
325331

@@ -330,14 +336,14 @@ function fetchTraceGeoData(calcData) {
330336
}
331337

332338
function wait(url) {
333-
return new Promise(function(resolve, reject) {
339+
return new Promise(function (resolve, reject) {
334340
var cnt = 0;
335-
var interval = setInterval(function() {
336-
if(PlotlyGeoAssets[url] && PlotlyGeoAssets[url] !== 'pending') {
341+
var interval = setInterval(function () {
342+
if (PlotlyGeoAssets[url] && PlotlyGeoAssets[url] !== 'pending') {
337343
clearInterval(interval);
338344
return resolve(PlotlyGeoAssets[url]);
339345
}
340-
if(cnt > 100) {
346+
if (cnt > 100) {
341347
clearInterval(interval);
342348
return reject('Unexpected error while fetching from ' + url);
343349
}
@@ -346,15 +352,15 @@ function fetchTraceGeoData(calcData) {
346352
});
347353
}
348354

349-
for(var i = 0; i < calcData.length; i++) {
355+
for (var i = 0; i < calcData.length; i++) {
350356
var trace = calcData[i][0].trace;
351357
var url = trace.geojson;
352358

353-
if(typeof url === 'string') {
354-
if(!PlotlyGeoAssets[url]) {
359+
if (typeof url === 'string') {
360+
if (!PlotlyGeoAssets[url]) {
355361
PlotlyGeoAssets[url] = 'pending';
356362
promises.push(fetch(url));
357-
} else if(PlotlyGeoAssets[url] === 'pending') {
363+
} else if (PlotlyGeoAssets[url] === 'pending') {
358364
promises.push(wait(url));
359365
}
360366
}

0 commit comments

Comments
 (0)