Skip to content

Commit e6926e1

Browse files
committed
geo: make sure that locations w/o matching ISO-3 return *false*
- instead of null feature - add geojson / topojson test to geo_test.js
1 parent 0601640 commit e6926e1

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

src/lib/geo_location_utils.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,21 @@ var locationmodeToIdFinder = {
2424

2525
exports.locationToFeature = function(locationmode, location, features) {
2626
var locationId = getLocationId(locationmode, location);
27-
var feature;
2827

29-
for(var i = 0; i < features.length; i++) {
30-
feature = features[i];
28+
if(locationId) {
29+
for(var i = 0; i < features.length; i++) {
30+
var feature = features[i];
3131

32-
if(feature.id === locationId) return feature;
32+
if(feature.id === locationId) return feature;
33+
}
34+
35+
Lib.warn([
36+
'Location with id', locationId,
37+
'does not have a matching topojson feature at this resolution.'
38+
].join(' '));
3339
}
3440

35-
Lib.warn([
36-
'Location with id', locationId,
37-
'does not have a matching topojson feature at this resolution.'
38-
].join(' '));
41+
return false;
3942
};
4043

4144
function getLocationId(locationmode, location) {
@@ -44,14 +47,14 @@ function getLocationId(locationmode, location) {
4447
}
4548

4649
function countryNameToISO3(countryName) {
47-
var iso3, regex;
48-
4950
for(var i = 0; i < countryIds.length; i++) {
50-
iso3 = countryIds[i];
51-
regex = new RegExp(countryRegex[iso3]);
51+
var iso3 = countryIds[i],
52+
regex = new RegExp(countryRegex[iso3]);
5253

5354
if(regex.test(countryName.toLowerCase())) return iso3;
5455
}
5556

5657
Lib.warn('Unrecognized country name: ' + countryName + '.');
58+
59+
return false;
5760
}

src/traces/choropleth/plot.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ plotChoropleth.calcGeoJSON = function(trace, topojson) {
4040
for(var i = 0; i < len; i++) {
4141
feature = locationToFeature(trace.locationmode, locations[i], features);
4242

43-
if(feature === undefined) continue; // filter the blank features here
43+
if(!feature) continue; // filter the blank features here
4444

4545
// 'data_array' attributes
4646
feature.z = trace.z[i];

test/jasmine/tests/geo_test.js

+46
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ var Plotly = require('@lib/index');
22
var Lib = require('@src/lib');
33

44
var Geo = require('@src/plots/geo');
5+
var GeoAssets = require('@src/assets/geo_assets');
56
var params = require('@src/plots/geo/constants');
67
var supplyLayoutDefaults = require('@src/plots/geo/layout/axis_defaults');
8+
var geoLocationUtils = require('@src/lib/geo_location_utils');
9+
var topojsonUtils = require('@src/lib/topojson_utils');
710

811
var d3 = require('d3');
912
var createGraphDiv = require('../assets/create_graph_div');
@@ -336,6 +339,49 @@ describe('Test Geo layout defaults', function() {
336339
});
337340
});
338341

342+
describe('geojson / topojson utils', function() {
343+
'use strict';
344+
345+
function _locationToFeature(topojson, loc, locationmode) {
346+
var trace = { locationmode: locationmode };
347+
var features = topojsonUtils.getTopojsonFeatures(trace, topojson);
348+
349+
var feature = geoLocationUtils.locationToFeature(locationmode, loc, features);
350+
return feature;
351+
}
352+
353+
describe('should be able to extract topojson feature from *locations* items', function() {
354+
var topojsonName = 'world_110m';
355+
var topojson = GeoAssets.topojson[topojsonName];
356+
357+
it('with *ISO-3* locationmode', function() {
358+
var out = _locationToFeature(topojson, 'CAN', 'ISO-3');
359+
360+
expect(Object.keys(out)).toEqual(['type', 'id', 'properties', 'geometry']);
361+
expect(out.id).toEqual('CAN');
362+
});
363+
364+
it('with *ISO-3* locationmode (not-found case)', function() {
365+
var out = _locationToFeature(topojson, 'XXX', 'ISO-3');
366+
367+
expect(out).toEqual(false);
368+
});
369+
370+
it('with *country names* locationmode', function() {
371+
var out = _locationToFeature(topojson, 'United States', 'country names');
372+
373+
expect(Object.keys(out)).toEqual(['type', 'id', 'properties', 'geometry']);
374+
expect(out.id).toEqual('USA');
375+
});
376+
377+
it('with *country names* locationmode (not-found case)', function() {
378+
var out = _locationToFeature(topojson, 'XXX', 'country names');
379+
380+
expect(out).toEqual(false);
381+
});
382+
});
383+
});
384+
339385
describe('Test geo interactions', function() {
340386
'use strict';
341387

0 commit comments

Comments
 (0)