Skip to content

Commit

Permalink
Wagner customizable projection, by Tobias Jung
Browse files Browse the repository at this point in the history
closes #139
  • Loading branch information
Fil committed Nov 24, 2018
1 parent 856c164 commit c0d795c
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,29 @@ The Van der Grinten III projection.

The Van der Grinten IV projection.

<a href="#geoWagner" name="geoWagner">#</a> d3.<b>geoWagner</b>() [<>](https://github.com/d3/d3-geo-projection/blob/master/src/wagner.js "Source")
<br><a href="#geoWagnerRaw" name="geoWagnerRaw">#</a> d3.<b>geoWagnerRaw</b>

[<img src="https://raw.githubusercontent.com/d3/d3-geo-projection/master/img/wagner.png" width="480" height="250">](https://map-projections.net/d3-customizable-wagner/)

The Wagner projection is customizable: default values produce the Wagner VIII projection.

<a href="#wagner_poleline" name="wagner_poleline">#</a> <i>wagner</i>.<b>poleline</b>([<i>poleline</i>])

Defaults to 65°.

<a href="#wagner_parallels" name="wagner_parallels">#</a> <i>wagner</i>.<b>parallels</b>([<i>parallels</i>])

Defaults to 60°.

<a href="#wagner_inflation" name="wagner_inflation">#</a> <i>wagner</i>.<b>inflation</b>([<i>inflation</i>])

Defaults to 20.

<a href="#wagner_ratio" name="wagner_ratio">#</a> <i>wagner</i>.<b>ratio</b>([<i>ratio</i>])

Defaults to 200.

<a href="#geoWagner4" name="geoWagner4">#</a> d3.<b>geoWagner4</b>() [<>](https://github.com/d3/d3-geo-projection/blob/master/src/wagner4.js "Source")
<br><a href="#geoWagner4Raw" name="geoWagner4Raw">#</a> d3.<b>geoWagner4Raw</b>

Expand Down
Binary file added img/wagner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export {default as geoVanDerGrinten, vanDerGrintenRaw as geoVanDerGrintenRaw} fr
export {default as geoVanDerGrinten2, vanDerGrinten2Raw as geoVanDerGrinten2Raw} from "./vanDerGrinten2";
export {default as geoVanDerGrinten3, vanDerGrinten3Raw as geoVanDerGrinten3Raw} from "./vanDerGrinten3";
export {default as geoVanDerGrinten4, vanDerGrinten4Raw as geoVanDerGrinten4Raw} from "./vanDerGrinten4";
export {default as geoWagner, wagnerRaw as geoWagnerRaw} from "./wagner.js";
export {default as geoWagner4, wagner4Raw as geoWagner4Raw} from "./wagner4.js";
export {default as geoWagner6, wagner6Raw as geoWagner6Raw} from "./wagner6.js";
export {default as geoWagner7, wagner7Raw as geoWagner7Raw} from "./wagner7.js";
Expand Down
89 changes: 89 additions & 0 deletions src/wagner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {geoProjectionMutator as projectionMutator} from "d3-geo";
import {acos, asin, atan2, cos, degrees, epsilon, halfPi, max, min, pi, radians, sin, sqrt, tan} from "./math";

function wagnerFormula(cx, cy, m1, m2, n) {
function forward(lambda, phi) {
var s = m1 * sin(m2 * phi),
c0 = sqrt(1 - s * s),
c1 = sqrt(2 / (1 + c0 * cos(lambda *= n)));
return [
cx * c0 * c1 * sin(lambda),
cy * s * c1
];
}

forward.invert = function(x, y) {
var t1 = x / cx,
t2 = y / cy,
p = sqrt(t1 * t1 + t2 * t2),
c = 2 * asin(p / 2);
return [
atan2(x * tan(c), cx * p) / n,
p && asin(y * sin(c) / (cy * m1 * p)) / m2
];
};

return forward;
}

export function wagnerRaw(poleline, parallels, inflation, ratio) {
// 60 is always used as reference parallel
var phi1 = pi / 3;

// sanitizing the input values
// poleline and parallels may approximate but never equal 0
poleline = max(poleline, epsilon);
parallels = max(parallels, epsilon);
// poleline must be <= 90; parallels may approximate but never equal 180
poleline = min(poleline, halfPi);
parallels = min(parallels, pi - epsilon);
// 0 <= inflation <= 99.999
inflation = max(inflation, 0);
inflation = min(inflation, 100 - epsilon);
// ratio > 0.
// sensible values, i.e. something that renders a map which still can be
// recognized as world map, are e.g. 20 <= ratio <= 1000.
ratio = max(ratio, epsilon);

// convert values from boehm notation
// areal inflation e.g. from 0 to 1 or 20 to 1.2:
var vinflation = inflation/100 + 1;
// axial ratio e.g. from 200 to 2:
var vratio = ratio / 100;
// the other ones are a bit more complicated...
var m2 = acos(vinflation * cos(phi1)) / phi1,
m1 = sin(poleline) / sin(m2 * halfPi),
n = parallels / pi,
k = sqrt(vratio * sin(poleline / 2) / sin(parallels / 2)),
cx = k / sqrt(n * m1 * m2),
cy = 1 / (k * sqrt(n * m1 * m2));

return wagnerFormula(cx, cy, m1, m2, n);
}

export default function() {
// default values generate wagner8
var poleline = 65 * radians,
parallels = 60 * radians,
inflation = 20,
ratio = 200,
mutate = projectionMutator(wagnerRaw),
projection = mutate(poleline, parallels, inflation, ratio);

projection.poleline = function(_) {
return arguments.length ? mutate(poleline = +_ * radians, parallels, inflation, ratio) : poleline * degrees;
};

projection.parallels = function(_) {
return arguments.length ? mutate(poleline, parallels = +_ * radians, inflation, ratio) : parallels * degrees;
};
projection.inflation = function(_) {
return arguments.length ? mutate(poleline, parallels, inflation = +_, ratio) : inflation;
};
projection.ratio = function(_) {
return arguments.length ? mutate(poleline, parallels, inflation, ratio = +_) : ratio;
};

return projection
.scale(163.775);
}
1 change: 1 addition & 0 deletions test/compare-images
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ for i in \
vanDerGrinten2 \
vanDerGrinten3 \
vanDerGrinten4 \
wagner \
wagner4 \
wagner6 \
wagner7 \
Expand Down
1 change: 1 addition & 0 deletions test/invert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ var points = [[0, 0], [30.3, 24.1], [-10, 42], [-2, -5], [0,-55]];
d3.geoVanDerGrinten2,
d3.geoVanDerGrinten3,
d3.geoVanDerGrinten4,
d3.geoWagner,
d3.geoWagner4,
d3.geoWagner6,
d3.geoWagner7,
Expand Down

0 comments on commit c0d795c

Please sign in to comment.