Skip to content

Commit 68bd47b

Browse files
committed
fix #3779 - filter out lat<0 features in USA-states location search
This is important as the Natural Earth files nclude state/provinces from USA, Canada, Australia and Brazil which have some overlay in their two-letter ids. For example, 'WA' is used for both Washington state and Western Australia. As subunits from USA and Canada never conflict, filtering out features south of the equator suffices to fix #3779 A better fix would have us add a "governing unit" properties in subunit features in the `sane-topojson` package to avoid conflicts.
1 parent b04102b commit 68bd47b

File tree

3 files changed

+226
-24
lines changed

3 files changed

+226
-24
lines changed

src/lib/geo_location_utils.js

+45-24
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var countryRegex = require('country-regex');
1312
var Lib = require('../lib');
1413

15-
1614
// make list of all country iso3 ids from at runtime
1715
var countryIds = Object.keys(countryRegex);
1816

@@ -22,16 +20,53 @@ var locationmodeToIdFinder = {
2220
'country names': countryNameToISO3
2321
};
2422

25-
exports.locationToFeature = function(locationmode, location, features) {
23+
function countryNameToISO3(countryName) {
24+
for(var i = 0; i < countryIds.length; i++) {
25+
var iso3 = countryIds[i];
26+
var regex = new RegExp(countryRegex[iso3]);
27+
28+
if(regex.test(countryName.trim().toLowerCase())) return iso3;
29+
}
30+
31+
Lib.log('Unrecognized country name: ' + countryName + '.');
32+
33+
return false;
34+
}
35+
36+
function locationToFeature(locationmode, location, features) {
2637
if(!location || typeof location !== 'string') return false;
2738

28-
var locationId = getLocationId(locationmode, location);
39+
var locationId = locationmodeToIdFinder[locationmode](location);
40+
var features2;
41+
var f, i;
2942

3043
if(locationId) {
31-
for(var i = 0; i < features.length; i++) {
32-
var feature = features[i];
44+
if(locationmode === 'USA-states') {
45+
// Filter out features south of the equator
46+
//
47+
// This is important as the Natural Earth files
48+
// include state/provinces from USA, Canada, Australia and Brazil
49+
// which have some overlay in their two-letter ids. For example,
50+
// 'WA' is used for both Washington state and Western Australia.
51+
// As subunits from USA and Canada never conflict, filtering out features
52+
// south of the equator suffices to fix https://github.com/plotly/plotly.js/issues/3779
53+
//
54+
// A better fix would have us add a "governing unit" properties in subunit features
55+
// in the `sane-topojson` package to avoid conflicts.
56+
features2 = [];
57+
for(i = 0; i < features.length; i++) {
58+
f = features[i];
59+
if(f.properties && f.properties.ct && f.properties.ct[1] > 0) {
60+
features2.push(f);
61+
}
62+
}
63+
} else {
64+
features2 = features;
65+
}
3366

34-
if(feature.id === locationId) return feature;
67+
for(i = 0; i < features2.length; i++) {
68+
f = features2[i];
69+
if(f.id === locationId) return f;
3570
}
3671

3772
Lib.log([
@@ -41,22 +76,8 @@ exports.locationToFeature = function(locationmode, location, features) {
4176
}
4277

4378
return false;
44-
};
45-
46-
function getLocationId(locationmode, location) {
47-
var idFinder = locationmodeToIdFinder[locationmode];
48-
return idFinder(location);
4979
}
5080

51-
function countryNameToISO3(countryName) {
52-
for(var i = 0; i < countryIds.length; i++) {
53-
var iso3 = countryIds[i];
54-
var regex = new RegExp(countryRegex[iso3]);
55-
56-
if(regex.test(countryName.trim().toLowerCase())) return iso3;
57-
}
58-
59-
Lib.log('Unrecognized country name: ' + countryName + '.');
60-
61-
return false;
62-
}
81+
module.exports = {
82+
locationToFeature: locationToFeature
83+
};
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
{
2+
"data": [
3+
{
4+
"type": "choropleth",
5+
"zmax": 80,
6+
"zmin": 0,
7+
"z": [
8+
"2",
9+
"6",
10+
"1",
11+
"138",
12+
"7",
13+
"6",
14+
"2",
15+
"5",
16+
"19",
17+
"17",
18+
"22",
19+
"6",
20+
"2",
21+
"1",
22+
"7",
23+
"5",
24+
"12",
25+
"18",
26+
"40",
27+
"13",
28+
"1",
29+
"1",
30+
"2",
31+
"14",
32+
"74",
33+
"3",
34+
"1",
35+
"19",
36+
"16",
37+
"21",
38+
"4",
39+
"6",
40+
"26",
41+
"3",
42+
"6",
43+
"15",
44+
"2",
45+
"3"
46+
],
47+
"marker": {
48+
"line": {
49+
"color": "rgb(27, 88, 114)",
50+
"width": 0.2
51+
}
52+
},
53+
"text": [
54+
"Alabama",
55+
"Arizona",
56+
"Arkansas",
57+
"California",
58+
"Colorado",
59+
"Connecticut",
60+
"Delaware",
61+
"District of Columbia",
62+
"Florida",
63+
"Georgia",
64+
"Illinois",
65+
"Indiana",
66+
"Iowa",
67+
"Kansas",
68+
"Kentucky",
69+
"Louisiana",
70+
"Maryland",
71+
"Massachusetts",
72+
"Michigan",
73+
"Minnesota",
74+
"Missouri",
75+
"Nebraska",
76+
"Nevada",
77+
"New Jersey",
78+
"New York",
79+
"North Carolina",
80+
"North Dakota",
81+
"Ohio",
82+
"Oregon",
83+
"Pennsylvania",
84+
"Rhode Island",
85+
"Tennessee",
86+
"Texas",
87+
"Utah",
88+
"Virginia",
89+
"Washington",
90+
"West Virginia",
91+
"Wisconsin"
92+
],
93+
"colorbar": {
94+
"len": 0.5,
95+
"ticksuffix": "+",
96+
"showticksuffix": "last"
97+
},
98+
"hoverinfo": "z+text",
99+
"colorscale": [
100+
[
101+
0,
102+
"rgb(234,241,247)"
103+
],
104+
[
105+
0.25,
106+
"rgb(131,222,147)"
107+
],
108+
[
109+
0.5,
110+
"rgb(70,176,150)"
111+
],
112+
[
113+
0.75,
114+
"rgb(31,138,171)"
115+
],
116+
[
117+
1,
118+
"rgb(77,60,176)"
119+
]
120+
],
121+
"locationmode": "USA-states",
122+
"locations": [
123+
"AL",
124+
"AZ",
125+
"AR",
126+
"CA",
127+
"CO",
128+
"CT",
129+
"DE",
130+
"DC",
131+
"FL",
132+
"GA",
133+
"IL",
134+
"IN",
135+
"IA",
136+
"KS",
137+
"KY",
138+
"LA",
139+
"MD",
140+
"MA",
141+
"MI",
142+
"MN",
143+
"MO",
144+
"NE",
145+
"NV",
146+
"NJ",
147+
"NY",
148+
"NC",
149+
"ND",
150+
"OH",
151+
"OR",
152+
"PA",
153+
"RI",
154+
"TN",
155+
"TX",
156+
"UT",
157+
"VA",
158+
"WA",
159+
"WV",
160+
"WI"
161+
]
162+
}
163+
],
164+
"layout": {
165+
"geo": {
166+
"showland": true,
167+
"landcolor": "rgb(163,213,235)",
168+
"showframe": false,
169+
"showocean": true,
170+
"oceancolor": "rgb(131, 199, 227)",
171+
"projection": {
172+
"type": "miller"
173+
},
174+
"resolution": 50,
175+
"showcoastlines": false
176+
},
177+
"title": {
178+
"text": "Number of artists by country or region"
179+
}
180+
}
181+
}

0 commit comments

Comments
 (0)