Skip to content

Commit f12bff2

Browse files
committed
Allow US states to be looked up by name
1 parent 1c4af9b commit f12bff2

File tree

3 files changed

+84
-16
lines changed

3 files changed

+84
-16
lines changed

src/lib/geo_location_utils.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ var loggers = require('./loggers');
1111
var isPlainObject = require('./is_plain_object');
1212
var nestedProperty = require('./nested_property');
1313
var polygon = require('./polygon');
14+
const { usaLocationAbbreviations, usaLocationList } = require('./usa_location_names');
1415

1516
// make list of all country iso3 ids from at runtime
1617
var countryIds = Object.keys(countryRegex);
1718

1819
var locationmodeToIdFinder = {
1920
'ISO-3': identity,
20-
'USA-states': identity,
21+
'USA-states': usaLocationToAbbreviation,
2122
'country names': countryNameToISO3
2223
};
2324

@@ -34,14 +35,25 @@ function countryNameToISO3(countryName) {
3435
return false;
3536
}
3637

38+
function usaLocationToAbbreviation(loc) {
39+
loc = loc.trim();
40+
const abbreviation = usaLocationAbbreviations.has(loc.toUpperCase())
41+
? loc.toUpperCase()
42+
: usaLocationList[loc.toLowerCase()];
43+
44+
if (abbreviation) return abbreviation;
45+
46+
loggers.log('Unrecognized US location: ' + loc + '.');
47+
48+
return false;
49+
}
50+
3751
function locationToFeature(locationmode, location, features) {
3852
if (!location || typeof location !== 'string') return false;
3953

40-
var locationId = locationmodeToIdFinder[locationmode](location);
41-
var filteredFeatures;
42-
var f, i;
43-
54+
const locationId = locationmodeToIdFinder[locationmode](location);
4455
if (locationId) {
56+
let filteredFeatures;
4557
if (locationmode === 'USA-states') {
4658
// Filter out features out in USA
4759
//
@@ -50,24 +62,18 @@ function locationToFeature(locationmode, location, features) {
5062
// which have some overlay in their two-letter ids. For example,
5163
// 'WA' is used for both Washington state and Western Australia.
5264
filteredFeatures = [];
53-
for (i = 0; i < features.length; i++) {
54-
f = features[i];
55-
if (f.properties && f.properties.gu && f.properties.gu === 'USA') {
56-
filteredFeatures.push(f);
57-
}
65+
for (const f of features) {
66+
if (f?.properties?.gu === 'USA') filteredFeatures.push(f);
5867
}
5968
} else {
6069
filteredFeatures = features;
6170
}
6271

63-
for (i = 0; i < filteredFeatures.length; i++) {
64-
f = filteredFeatures[i];
72+
for (const f of filteredFeatures) {
6573
if (f.id === locationId) return f;
6674
}
6775

68-
loggers.log(
69-
['Location with id', locationId, 'does not have a matching topojson feature at this resolution.'].join(' ')
70-
);
76+
loggers.log(`Location with id ${locationId} does not have a matching topojson feature at this resolution.`);
7177
}
7278

7379
return false;

src/lib/usa_location_names.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
// Mapping of lowercase full US state names to two-letter abbreviations
4+
export const usaLocationList = {
5+
alabama: 'AL',
6+
alaska: 'AK',
7+
arizona: 'AZ',
8+
arkansas: 'AR',
9+
california: 'CA',
10+
colorado: 'CO',
11+
connecticut: 'CT',
12+
delaware: 'DE',
13+
'district of columbia': 'DC',
14+
florida: 'FL',
15+
georgia: 'GA',
16+
hawaii: 'HI',
17+
idaho: 'ID',
18+
illinois: 'IL',
19+
indiana: 'IN',
20+
iowa: 'IA',
21+
kansas: 'KS',
22+
kentucky: 'KY',
23+
louisiana: 'LA',
24+
maine: 'ME',
25+
maryland: 'MD',
26+
massachusetts: 'MA',
27+
michigan: 'MI',
28+
minnesota: 'MN',
29+
mississippi: 'MS',
30+
missouri: 'MO',
31+
montana: 'MT',
32+
nebraska: 'NE',
33+
nevada: 'NV',
34+
'new hampshire': 'NH',
35+
'new jersey': 'NJ',
36+
'new mexico': 'NM',
37+
'new york': 'NY',
38+
'north carolina': 'NC',
39+
'north dakota': 'ND',
40+
ohio: 'OH',
41+
oklahoma: 'OK',
42+
oregon: 'OR',
43+
pennsylvania: 'PA',
44+
'rhode island': 'RI',
45+
'south carolina': 'SC',
46+
'south dakota': 'SD',
47+
tennessee: 'TN',
48+
texas: 'TX',
49+
utah: 'UT',
50+
vermont: 'VT',
51+
virginia: 'VA',
52+
washington: 'WA',
53+
'washington dc': 'DC',
54+
'washington d.c.': 'DC',
55+
'west virginia': 'WV',
56+
wisconsin: 'WI',
57+
wyoming: 'WY'
58+
};
59+
60+
export const usaLocationAbbreviations = new Set(Object.values(usaLocationList));

src/traces/scattergeo/attributes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ module.exports = overrideAll(
4848
'to regions on the map.',
4949
'Values *ISO-3*, *USA-states*, *country names* correspond to features on',
5050
'the base map and value *geojson-id* corresponds to features from a custom',
51-
'GeoJSON linked to the `geojson` attribute.'
51+
'GeoJSON linked to the `geojson` attribute.',
52+
'*USA-states* accepts both two-letter abbreviations (e.g. *CA*) and',
53+
'full state names (e.g. *California*).'
5254
].join(' ')
5355
},
5456

0 commit comments

Comments
 (0)