Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 45 additions & 24 deletions src/lib/geo_location_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

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


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

Expand All @@ -22,16 +20,53 @@ var locationmodeToIdFinder = {
'country names': countryNameToISO3
};

exports.locationToFeature = function(locationmode, location, features) {
function countryNameToISO3(countryName) {
for(var i = 0; i < countryIds.length; i++) {
var iso3 = countryIds[i];
var regex = new RegExp(countryRegex[iso3]);

if(regex.test(countryName.trim().toLowerCase())) return iso3;
}

Lib.log('Unrecognized country name: ' + countryName + '.');

return false;
}

function locationToFeature(locationmode, location, features) {
if(!location || typeof location !== 'string') return false;

var locationId = getLocationId(locationmode, location);
var locationId = locationmodeToIdFinder[locationmode](location);
var features2;
var f, i;

if(locationId) {
for(var i = 0; i < features.length; i++) {
var feature = features[i];
if(locationmode === 'USA-states') {
// Filter out features south of the equator
//
// This is important as the Natural Earth files
// include 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 https://github.com/plotly/plotly.js/issues/3779
//
// A better fix would have us add a "governing unit" properties in subunit features
// in the `sane-topojson` package to avoid conflicts.
features2 = [];
for(i = 0; i < features.length; i++) {
f = features[i];
if(f.properties && f.properties.ct && f.properties.ct[1] > 0) {
features2.push(f);
}
}
} else {
features2 = features;
}

if(feature.id === locationId) return feature;
for(i = 0; i < features2.length; i++) {
f = features2[i];
if(f.id === locationId) return f;
}

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

return false;
};

function getLocationId(locationmode, location) {
var idFinder = locationmodeToIdFinder[locationmode];
return idFinder(location);
}

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

if(regex.test(countryName.trim().toLowerCase())) return iso3;
}

Lib.log('Unrecognized country name: ' + countryName + '.');

return false;
}
module.exports = {
locationToFeature: locationToFeature
};
16 changes: 15 additions & 1 deletion src/plots/geo/geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function Geo(opts) {
this.topojson = null;

this.projection = null;
this.scope = null;
this.viewInitial = null;
this.fitScale = null;
this.bounds = null;
Expand Down Expand Up @@ -71,6 +72,18 @@ module.exports = function createGeo(opts) {
proto.plot = function(geoCalcData, fullLayout, promises) {
var _this = this;
var geoLayout = fullLayout[this.id];

var needsTopojson = false;
for(var k in constants.layerNameToAdjective) {
if(k !== 'frame' && geoLayout['show' + k]) {
needsTopojson = true;
break;
}
}
if(!needsTopojson) {
return _this.update(geoCalcData, fullLayout);
}

var topojsonNameNew = topojsonUtils.getTopojsonName(geoLayout);

if(_this.topojson === null || topojsonNameNew !== _this.topojsonName) {
Expand Down Expand Up @@ -133,9 +146,10 @@ proto.update = function(geoCalcData, fullLayout) {
}
}

if(!this.viewInitial) {
if(!this.viewInitial || this.scope !== geoLayout.scope) {
this.saveViewInitial(geoLayout);
}
this.scope = geoLayout.scope;

this.updateBaseLayers(fullLayout, geoLayout);
this.updateDims(fullLayout, geoLayout);
Expand Down
Binary file added test/image/baselines/geo_skymap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading