Skip to content

Commit ab5537b

Browse files
committed
combine regions
1 parent e569d43 commit ab5537b

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

Diff for: src/lib/geo_location_utils.js

+55-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ function locationToFeature(locationmode, location, features) {
4141
var filteredFeatures;
4242
var f, i;
4343

44+
var allParts = [];
45+
4446
if(locationId) {
4547
if(locationmode === 'USA-states') {
4648
// Filter out features out in USA
@@ -67,8 +69,7 @@ function locationToFeature(locationmode, location, features) {
6769
f.properties &&
6870
f.properties.iso3cd === locationId
6971
) {
70-
// TODO: we may need to collect all the polygons instead?
71-
return f;
72+
allParts.push(f);
7273
}
7374
}
7475

@@ -78,10 +79,35 @@ function locationToFeature(locationmode, location, features) {
7879
].join(' '));
7980
}
8081

82+
if(allParts.length) {
83+
return allParts; //[allParts.length - 1];
84+
}
85+
8186
return false;
8287
}
8388

8489
function feature2polygons(feature) {
90+
if(!Array.isArray(feature)) {
91+
return _feature2polygons(feature);
92+
}
93+
94+
var polygons;
95+
for(var i = 0; i < feature.length; i++) {
96+
var pts = _feature2polygons(feature[i]);
97+
98+
if(pts.length) {
99+
if(!polygons) {
100+
polygons = pts;
101+
} else {
102+
polygons.push(polygon.tester(pts));
103+
}
104+
}
105+
}
106+
107+
return polygons;
108+
}
109+
110+
function _feature2polygons(feature) {
85111
var geometry = feature.geometry;
86112
var coords = geometry.coordinates;
87113
var loc = feature.properties.iso3cd || feature.id;
@@ -373,9 +399,35 @@ function fetchTraceGeoData(calcData) {
373399
return promises;
374400
}
375401

402+
403+
function computeBbox(d) {
404+
if(!Array.isArray(d)) {
405+
return _computeBbox(d);
406+
}
407+
408+
var minLon = Infinity;
409+
var maxLon = -Infinity;
410+
var minLat = Infinity;
411+
var maxLat = -Infinity;
412+
413+
for(var i = 0; i < d.length; i++) {
414+
var p = _computeBbox(d[i]);
415+
416+
minLon = Math.min(minLon, p[0]);
417+
minLat = Math.min(minLat, p[1]);
418+
maxLon = Math.max(maxLon, p[2]);
419+
maxLat = Math.max(maxLat, p[3]);
420+
}
421+
422+
return [
423+
minLon, minLat,
424+
maxLon, maxLat
425+
];
426+
}
427+
376428
// TODO `turf/bbox` gives wrong result when the input feature/geometry
377429
// crosses the anti-meridian. We should try to implement our own bbox logic.
378-
function computeBbox(d) {
430+
function _computeBbox(d) {
379431
return turfBbox(d);
380432
}
381433

Diff for: src/traces/choropleth/plot.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ function calcGeoJSON(calcTrace, fullLayout) {
4949
geoUtils.locationToFeature(locationmode, calcPt.loc, features);
5050

5151
if(feature) {
52-
calcPt.geojson = feature;
53-
calcPt.ct = feature.properties.ct;
52+
var f0 = Array.isArray(feature) ? feature[0] : feature;
53+
calcPt.geojson = f0;
54+
calcPt.ct = f0.properties.ct;
5455
calcPt._polygons = geoUtils.feature2polygons(feature);
5556

5657
var bboxFeature = geoUtils.computeBbox(feature);

0 commit comments

Comments
 (0)