Skip to content

Commit 422430d

Browse files
committed
add new geo projection bundle
1 parent 84530d9 commit 422430d

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

src/plots/geo/geo.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ function Geo(options, fullLayout) {
3737
this.container = options.container;
3838
this.topojsonURL = options.topojsonURL;
3939

40-
// add a few projection types to d3.geo,
41-
// a subset of https://github.com/d3/d3-geo-projection
42-
addProjectionsToD3();
40+
// add a few projection types to d3.geo
41+
addProjectionsToD3(d3);
4342

4443
this.hoverContainer = null;
4544

src/plots/geo/projections.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
/**
10-
* Forked from https://github.com/d3/d3-geo-projection
11-
* Pasted from https://github.com/etpinard/d3-geo-projection
9+
/*
10+
* Generated by https://github.com/etpinard/d3-geo-projection-picker
1211
*
13-
* Containing only the 'most useful' projection types
14-
* and compatible with CommonJs
12+
* which is hand-picks projection from https://github.com/d3/d3-geo-projection
1513
*
14+
* into a CommonJS require-able module.
1615
*/
1716

18-
var d3 = require('d3');
17+
'use strict';
1918

20-
function addProjectionToD3() {
19+
function addProjectionsToD3(d3) {
2120
d3.geo.project = function(object, projection) {
2221
var stream = projection.stream;
2322
if (!stream) throw new Error("not yet supported");
@@ -78,7 +77,7 @@ function addProjectionToD3() {
7877
d3_geo_projectPoints.push([ x, y ]);
7978
},
8079
lineEnd: function() {
81-
if (d3_geo_projectPoints.length) d3_geo_projectLines.push(d3_geo_projectPoints),
80+
if (d3_geo_projectPoints.length) d3_geo_projectLines.push(d3_geo_projectPoints),
8281
d3_geo_projectPoints = [];
8382
},
8483
result: function() {
@@ -205,7 +204,7 @@ function addProjectionToD3() {
205204
};
206205
var projection = d3.geo.projection(forward), stream_ = projection.stream;
207206
projection.stream = function(stream) {
208-
var rotate = projection.rotate(), rotateStream = stream_(stream), sphereStream = (projection.rotate([ 0, 0 ]),
207+
var rotate = projection.rotate(), rotateStream = stream_(stream), sphereStream = (projection.rotate([ 0, 0 ]),
209208
stream_(stream));
210209
projection.rotate(rotate);
211210
rotateStream.sphere = function() {
@@ -405,6 +404,39 @@ function addProjectionToD3() {
405404
(d3.geo.sinusoidal = function() {
406405
return projection(sinusoidal);
407406
}).raw = sinusoidal;
407+
function aitoff(λ, φ) {
408+
var cosφ = Math.cos(φ), sinciα = sinci(acos(cosφ * Math.cos(λ /= 2)));
409+
return [ 2 * cosφ * Math.sin(λ) * sinciα, Math.sin(φ) * sinciα ];
410+
}
411+
aitoff.invert = function(x, y) {
412+
if (x * x + 4 * y * y > π * π + ε) return;
413+
var λ = x, φ = y, i = 25;
414+
do {
415+
var sinλ = Math.sin(λ), sinλ_2 = Math.sin(λ / 2), cosλ_2 = Math.cos(λ / 2), sinφ = Math.sin(φ), cosφ = Math.cos(φ), sin_2φ = Math.sin(2 * φ), sin2φ = sinφ * sinφ, cos2φ = cosφ * cosφ, sin2λ_2 = sinλ_2 * sinλ_2, C = 1 - cos2φ * cosλ_2 * cosλ_2, E = C ? acos(cosφ * cosλ_2) * Math.sqrt(F = 1 / C) : F = 0, F, fx = 2 * E * cosφ * sinλ_2 - x, fy = E * sinφ - y, δxδλ = F * (cos2φ * sin2λ_2 + E * cosφ * cosλ_2 * sin2φ), δxδφ = F * (.5 * sinλ * sin_2φ - E * 2 * sinφ * sinλ_2), δyδλ = F * .25 * (sin_2φ * sinλ_2 - E * sinφ * cos2φ * sinλ), δyδφ = F * (sin2φ * cosλ_2 + E * sin2λ_2 * cosφ), denominator = δxδφ * δyδλ - δyδφ * δxδλ;
416+
if (!denominator) break;
417+
var δλ = (fy * δxδφ - fx * δyδφ) / denominator, δφ = (fx * δyδλ - fy * δxδλ) / denominator;
418+
λ -= δλ, φ -= δφ;
419+
} while ((Math.abs(δλ) > ε || Math.abs(δφ) > ε) && --i > 0);
420+
return [ λ, φ ];
421+
};
422+
(d3.geo.aitoff = function() {
423+
return projection(aitoff);
424+
}).raw = aitoff;
425+
function winkel3(λ, φ) {
426+
var coordinates = aitoff(λ, φ);
427+
return [ (coordinates[0] + λ / halfπ) / 2, (coordinates[1] + φ) / 2 ];
428+
}
429+
winkel3.invert = function(x, y) {
430+
var λ = x, φ = y, i = 25;
431+
do {
432+
var cosφ = Math.cos(φ), sinφ = Math.sin(φ), sin_2φ = Math.sin(2 * φ), sin2φ = sinφ * sinφ, cos2φ = cosφ * cosφ, sinλ = Math.sin(λ), cosλ_2 = Math.cos(λ / 2), sinλ_2 = Math.sin(λ / 2), sin2λ_2 = sinλ_2 * sinλ_2, C = 1 - cos2φ * cosλ_2 * cosλ_2, E = C ? acos(cosφ * cosλ_2) * Math.sqrt(F = 1 / C) : F = 0, F, fx = .5 * (2 * E * cosφ * sinλ_2 + λ / halfπ) - x, fy = .5 * (E * sinφ + φ) - y, δxδλ = .5 * F * (cos2φ * sin2λ_2 + E * cosφ * cosλ_2 * sin2φ) + .5 / halfπ, δxδφ = F * (sinλ * sin_2φ / 4 - E * sinφ * sinλ_2), δyδλ = .125 * F * (sin_2φ * sinλ_2 - E * sinφ * cos2φ * sinλ), δyδφ = .5 * F * (sin2φ * cosλ_2 + E * sin2λ_2 * cosφ) + .5, denominator = δxδφ * δyδλ - δyδφ * δxδλ, δλ = (fy * δxδφ - fx * δyδφ) / denominator, δφ = (fx * δyδλ - fy * δxδλ) / denominator;
433+
λ -= δλ, φ -= δφ;
434+
} while ((Math.abs(δλ) > ε || Math.abs(δφ) > ε) && --i > 0);
435+
return [ λ, φ ];
436+
};
437+
(d3.geo.winkel3 = function() {
438+
return projection(winkel3);
439+
}).raw = winkel3;
408440
}
409441

410-
module.exports = addProjectionToD3;
442+
module.exports = addProjectionsToD3;

0 commit comments

Comments
 (0)