8000 Geo improvements: fitbounds, 'geojson-id' locationmode and 'featureidkey' by etpinard · Pull Request #4419 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Geo improvements: fitbounds, 'geojson-id' locationmode and 'featureidkey' #4419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 20, 2019
Merged
Prev Previous commit
Next Next commit
fix handling of locations[i] 0
... in choropleth and scattergeo traces
  • Loading branch information
etpinard committed Dec 11, 2019
commit 5854f6fb8d3733cf4e677c870de8ee3dfd5f9254
4 changes: 3 additions & 1 deletion src/lib/geo_location_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ function extractTraceFeature(calcTrace) {

for(i = 0; i < trace._length; i++) {
var cdi = calcTrace[i];
if(cdi.loc) lookup[cdi.loc] = cdi;
if(cdi.loc || cdi.loc === 0) {
lookup[cdi.loc] = cdi;
}
}

function appendFeature(fIn) {
Expand Down
13 changes: 12 additions & 1 deletion src/traces/scattergeo/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,28 @@ var calcSelection = require('../scatter/calc_selection');

var _ = require('../../lib')._;

function isNonBlankString(v) {
return v && typeof v === 'string';
}

module.exports = function calc(gd, trace) {
var hasLocationData = Array.isArray(trace.locations);
var len = hasLocationData ? trace.locations.length : trace._length;
var calcTrace = new Array(len);

var isValidLoc;
if(trace.geojson) {
isValidLoc = function(v) { return isNonBlankString(v) || isNumeric(v); };
} else {
isValidLoc = isNonBlankString;
}

for(var i = 0; i < len; i++) {
var calcPt = calcTrace[i] = {};

if(hasLocationData) {
var loc = trace.locations[i];
calcPt.loc = typeof loc === 'string' ? loc : null;
calcPt.loc = isValidLoc(loc) ? loc : null;
} else {
var lon = trace.lon[i];
var lat = trace.lat[i];
Expand Down
Binary file added test/image/baselines/geo_fitbounds-geojson.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
180 changes: 180 additions & 0 deletions test/image/mocks/geo_fitbounds-geojson.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
{
"data": [{
"type": "choropleth",
"locations": [0],
"z": [10],
"showscale": false,
"geojson": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"id": 0,
"geometry": {
"type": "Polygon",
"coordinates": [[
[-87.359296, 35.00118], [-85.606675, 34.984749], [-85.431413, 34.124869], [-85.184951, 32.859696],
[-85.069935, 32.580372], [-84.960397, 32.421541], [-85.004212, 32.322956], [-84.889196, 32.262709],
[-85.058981, 32.13674], [-85.053504, 32.01077], [-85.141136, 31.840985], [-85.042551, 31.539753],
[-85.113751, 31.27686], [-85.004212, 31.003013], [-85.497137, 30.997536], [-87.600282, 30.997536],
[-87.633143, 30.86609], [-87.408589, 30.674397], [-87.446927, 30.510088], [-87.37025, 30.427934],
[-87.518128, 30.280057], [-87.655051, 30.247195], [-87.90699, 30.411504], [-87.934375, 30.657966],
[-88.011052, 30.685351], [-88.10416, 30.499135], [-88.137022, 30.318396], [-88.394438, 30.367688],
[-88.471115, 31.895754], [-88.241084, 33.796253], [-88.098683, 34.891641], [-88.202745, 34.995703],
[-87.359296, 35.00118]
]]
}
}, {
"type": "Feature",
"id": 1,
"geometry": {
"type": "Polygon",
"coordinates": [[[-83.109191,35.00118],[-83.322791,34.787579],[-83.339222,34.683517],[-83.005129,34.469916],[-82.901067,34.486347],[-82.747713,34.26727],[-82.714851,34.152254],[-82.55602,33.94413],[-82.325988,33.81816],[-82.194542,33.631944],[-81.926172,33.462159],[-81.937125,33.347144],[-81.761863,33.160928],[-81.493493,33.007573],[-81.42777,32.843265],[-81.416816,32.629664],[-81.279893,32.558464],[-81.121061,32.290094],[-81.115584,32.120309],[-80.885553,32.032678],[-81.132015,31.693108],[-81.175831,31.517845],[-81.279893,31.364491],[-81.290846,31.20566],[-81.400385,31.13446],[-81.444201,30.707258],[-81.718048,30.745597],[-81.948079,30.827751],[-82.041187,30.751074],[-82.002849,30.564858],[-82.046664,30.362211],[-82.167157,30.356734],[-82.216449,30.570335],[-83.498053,30.647012],[-84.867289,30.712735],[-85.004212,31.003013],[-85.113751,31.27686],[-85.042551,31.539753],[-85.141136,31.840985],[-85.053504,32.01077],[-85.058981,32.13674],[-84.889196,32.262709],[-85.004212,32.322956],[-84.960397,32.421541],[-85.069935,32.580372],[-85.184951,32.859696],[-85.431413,34.124869],[-85.606675,34.984749],[-84.319594,34.990226],[-83.618546,34.984749],[-83.109191,35.00118]]]
}
}]
}
}, {
"type": "scattergeo",
"locations": [0],
"marker": {"size": 40},
"geo": "geo2",
"geojson": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"id": 0,
"geometry": {
"type": "Polygon",
"coordinates": [[
[-87.359296, 35.00118], [-85.606675, 34.984749], [-85.431413, 34.124869], [-85.184951, 32.859696],
[-85.069935, 32.580372], [-84.960397, 32.421541], [-85.004212, 32.322956], [-84.889196, 32.262709],
[-85.058981, 32.13674], [-85.053504, 32.01077], [-85.141136, 31.840985], [-85.042551, 31.539753],
[-85.113751, 31.27686], [-85.004212, 31.003013], [-85.497137, 30.997536], [-87.600282, 30.997536],
[-87.633143, 30.86609], [-87.408589, 30.674397], [-87.446927, 30.510088], [-87.37025, 30.427934],
[-87.518128, 30.280057], [-87.655051, 30.247195], [-87.90699, 30.411504], [-87.934375, 30.657966],
[-88.011052, 30.685351], [-88.10416, 30.499135], [-88.137022, 30.318396], [-88.394438, 30.367688],
[-88.471115, 31.895754], [-88.241084, 33.796253], [-88.098683, 34.891641], [-88.202745, 34.995703],
[-87.359296, 35.00118]
]]
}
}, {
"type": "Feature",
"id": 1,
"geometry": {
"type": "Polygon",
"coordinates": [[[-83.109191,35.00118],[-83.322791,34.787579],[-83.339222,34.683517],[-83.005129,34.469916],[-82.901067,34.486347],[-82.747713,34.26727],[-82.714851,34.152254],[-82.55602,33.94413],[-82.325988,33.81816],[-82.194542,33.631944],[-81.926172,33.462159],[-81.937125,33.347144],[-81.761863,33.160928],[-81.493493,33.007573],[-81.42777,32.843265],[-81.416816,32.629664],[-81.279893,32.558464],[-81.121061,32.290094],[-81.115584,32.120309],[-80.885553,32.032678],[-81.132015,31.693108],[-81.175831,31.517845],[-81.279893,31.364491],[-81.290846,31.20566],[-81.400385,31.13446],[-81.444201,30.707258],[-81.718048,30.745597],[-81.948079,30.827751],[-82.041187,30.751074],[-82.002849,30.564858],[-82.046664,30.362211],[-82.167157,30.356734],[-82.216449,30.570335],[-83.498053,30.647012],[-84.867289,30.712735],[-85.004212,31.003013],[-85.113751,31.27686],[-85.042551,31.539753],[-85.141136,31.840985],[-85.053504,32.01077],[-85.058981,32.13674],[-84.889196,32.262709],[-85.004212,32.322956],[-84.960397,32.421541],[-85.069935,32.580372],[-85.184951,32.859696],[-85.431413,34.124869],[-85.606675,34.984749],[-84.319594,34.990226],[-83.618546,34.984749],[-83.109191,35.00118]]]
}
}]
}
}, {
"type": "choropleth",
"locations": [0],
"z": [10],
"showscale": false,
"geo": "geo3",
"geojson": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"id": 0,
"geometry": {
"type": "Polygon",
"coordinates": [[
[-87.359296, 35.00118], [-85.606675, 34.984749], [-85.431413, 34.124869], [-85.184951, 32.859696],
[-85.069935, 32.580372], [-84.960397, 32.421541], [-85.004212, 32.322956], [-84.889196, 32.262709],
[-85.058981, 32.13674], [-85.053504, 32.01077], [-85.141136, 31.840985], [-85.042551, 31.539753],
[-85.113751, 31.27686], [-85.004212, 31.003013], [-85.497137, 30.997536], [-87.600282, 30.997536],
[-87.633143, 30.86609], [-87.408589, 30.674397], [-87.446927, 30.510088], [-87.37025, 30.427934],
[-87.518128, 30.280057], [-87.655051, 30.247195], [-87.90699, 30.411504], [-87.934375, 30.657966],
[-88.011052, 30.685351], [-88.10416, 30.499135], [-88.137022, 30.318396], [-88.394438, 30.367688],
[-88.471115, 31.895754], [-88.241084, 33.796253], [-88.098683, 34.891641], [-88.202745, 34.995703],
[-87.359296, 35.00118]
]]
}
}, {
"type": "Feature",
"id": 1,
"geometry": {
"type": "Polygon",
"coordinates": [[[-83.109191,35.00118],[-83.322791,34.787579],[-83.339222,34.683517],[-83.005129,34.469916],[-82.901067,34.486347],[-82.747713,34.26727],[-82.714851,34.152254],[-82.55602,33.94413],[-82.325988,33.81816],[-82.194542,33.631944],[-81.926172,33.462159],[-81.937125,33.347144],[-81.761863,33.160928],[-81.493493,33.007573],[-81.42777,32.843265],[-81.416816,32.629664],[-81.279893,32.558464],[-81.121061,32.290094],[-81.115584,32.120309],[-80.885553,32.032678],[-81.132015,31.693108],[-81.175831,31.517845],[-81.279893,31.364491],[-81.290846,31.20566],[-81.400385,31.13446],[-81.444201,30.707258],[-81.718048,30.745597],[-81.948079,30.827751],[-82.041187,30.751074],[-82.002849,30.564858],[-82.046664,30.362211],[-82.167157,30.356734],[-82.216449,30.570335],[-83.498053,30.647012],[-84.867289,30.712735],[-85.004212,31.003013],[-85.113751,31.27686],[-85.042551,31.539753],[-85.141136,31.840985],[-85.053504,32.01077],[-85.058981,32.13674],[-84.889196,32.262709],[-85.004212,32.322956],[-84.960397,32.421541],[-85.069935,32.580372],[-85.184951,32.859696],[-85.431413,34.124869],[-85.606675,34.984749],[-84.319594,34.990226],[-83.618546,34.984749],[-83.109191,35.00118]]]
}
}]
}
}, {
"type": "scattergeo",
"locations": [0],
"marker": {"size": 40},
"geo": "geo4",
"geojson": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"id": 0,
"geometry": {
"type": "Polygon",
"coordinates": [[
[-87.359296, 35.00118], [-85.606675, 34.984749], [-85.431413, 34.124869], [-85.184951, 32.859696],
[-85.069935, 32.580372], [-84.960397, 32.421541], [-85.004212, 32.322956], [-84.889196, 32.262709],
[-85.058981, 32.13674], [-85.053504, 32.01077], [-85.141136, 31.840985], [-85.042551, 31.539753],
[-85.113751, 31.27686], [-85.004212, 31.003013], [-85.497137, 30.997536], [-87.600282, 30.997536],
[-87.633143, 30.86609], [-87.408589, 30.674397], [-87.446927, 30.510088], [-87.37025, 30.427934],
[-87.518128, 30.280057], [-87.655051, 30.247195], [-87.90699, 30.411504], [-87.934375, 30.657966],
[-88.011052, 30.685351], [-88.10416, 30.499135], [-88.137022, 30.318396], [-88.394438, 30.367688],
[-88.471115, 31.895754], [-88.241084, 33.796253], [-88.098683, 34.891641], [-88.202745, 34.995703],
[-87.359296, 35.00118]
]]
}
}, {
"type": "Feature",
"id": 1,
"geometry": {
"type": "Polygon",
"coordinates": [[[-83.109191,35.00118],[-83.322791,34.787579],[-83.339222,34.683517],[-83.005129,34.469916],[-82.901067,34.486347],[-82.747713,34.26727],[-82.714851,34.152254],[-82.55602,33.94413],[-82.325988,33.81816],[-82.194542,33.631944],[-81.926172,33.462159],[-81.937125,33.347144],[-81.761863,33.160928],[-81.493493,33.007573],[-81.42777,32.843265],[-81.416816,32.629664],[-81.279893,32.558464],[-81.121061,32.290094],[-81.115584,32.120309],[-80.885553,32.032678],[-81.132015,31.693108],[-81.175831,31.517845],[-81.279893,31.364491],[-81.290846,31.20566],[-81.400385,31.13446],[-81.444201,30.707258],[-81.718048,30.745597],[-81.948079,30.827751],[-82.041187,30.751074],[-82.002849,30.564858],[-82.046664,30.362211],[-82.167157,30.356734],[-82.216449,30.570335],[-83.498053,30.647012],[-84.867289,30.712735],[-85.004212,31.003013],[-85.113751,31.27686],[-85.042551,31.539753],[-85.141136,31.840985],[-85.053504,32.01077],[-85.058981,32.13674],[-84.889196,32.262709],[-85.004212,32.322956],[-84.960397,32.421541],[-85.069935,32.580372],[-85.184951,32.859696],[-85.431413,34.124869],[-85.606675,34.984749],[-84.319594,34.990226],[-83.618546,34.984749],[-83.109191,35.00118]]]
}
}]
}
}],
"layout": {
"width": 600,
"height": 400,
"showlegend": false,
"grid": {"rows": 2, "columns": 2},
"geo": {
"domain": {"row": 0, "column": 0},
"fitbounds": "geojson"
},
"geo2": {
"domain": {"row": 1, "column": 0},
"fitbounds": "geojson"
},
"geo3": {
"domain": {"row": 0, "column": 1},
"fitbounds": "locations"
},
"geo4": {
"domain": {"row": 1, "column": 1},
"fitbounds": "locations"
},
"annotations": [{
"text": "fitbounds: 'geojson'",
"font": {"size": 20},
"showarrow": false,
"xref": "paper",
"x": 0,
"xanchor": "left",
"yref": "paper",
"y": 1.02,
"yanchor": "bottom"
}, {
"text": "fitbounds: 'locations'",
"font": {"size": 20},
"showarrow": false,
"xref": "paper",
"x": 1,
"xanchor": "right",
"yref": "paper",
"y": 1.02,
"yanchor": "bottom"
}],
"margin": {"l": 20, "r": 20, "b": 20, "t": 40}
}
}
0