Skip to content

Instantly share code, notes, and snippets.

@mthh
Last active April 5, 2017 20:19
Show Gist options
  • Save mthh/9e467a0ddd4e77a73a4dd91d61f15af8 to your computer and use it in GitHub Desktop.
Save mthh/9e467a0ddd4e77a73a4dd91d61f15af8 to your computer and use it in GitHub Desktop.
Winkel 2 projection
license: gpl-3.0
border: no
Winkel 2

The Winkel II projection. It allows to define the latitude of the first standard parallel (here defined at 45°).

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.graticule {
fill: none;
stroke: #777;
stroke-width: 0.5px;
stroke-opacity: 0.5;
}
.land {
fill: #222;
}
.boundary {
fill: none;
stroke: #fff;
stroke-width: 0.5px;
}
</style>
<svg width="960" height="500"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-geo-projection/2.1.0/d3-geo-projection.min.js"></script>
<script src="wink2.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
g = svg.append("g");
var projection = d3.geoProjection(Winkel2Raw(45))
.scale(155)
.translate([width / 2, height / 2])
.precision(0.1);
var path = d3.geoPath()
.projection(projection);
var graticule = d3.geoGraticule();
g.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path);
g.append("path")
.datum({type: "Sphere"})
.attr("class", "sphere")
.attr("d", path)
.attr("fill", "none")
.attr("stroke", "black");
d3.json("world-50m.json", function(error, world) {
if (error) throw error;
g.insert("path", ".graticule")
.datum(topojson.feature(world, world.objects.land))
.attr("class", "land")
.attr("d", path);
g.insert("path", ".graticule")
.datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }))
.attr("class", "boundary")
.attr("d", path);
});
</script>
var PI = Math.PI,
TWO_D_PI = 2 / PI,
FORT_PI = PI / 4,
HALF_PI = PI / 2,
MAX_ITER = 10,
LOOP_TOL = 1e-7;
var cos = Math.cos,
abs = Math.abs,
sin = Math.sin;
function Winkel2Raw(lat_std_parallel) {
var cosphi1 = lat_std_parallel ? cos(lat_std_parallel) : 0;
function forward(lambda, phi) {
var y = phi * TWO_D_PI,
k = PI * sin(phi),
V, i;
phi *= 1.8;
for(i = MAX_ITER; i; i--){
phi -= V = (phi + sin(phi) - k) / (1 + cos(phi));
if(abs(V) < LOOP_TOL) break;
}
if(!i){
phi = (phi < 0) ? -HALF_PI : HALF_PI;
} else {
phi *= 0.5;
}
return [0.5 * lambda * (cosphi1 + cos(phi)),
FORT_PI * (sin(phi) + y)];
}
return forward;
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment