Skip to content

Commit

Permalink
Fix #45 - better default scales.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jul 7, 2016
1 parent 16f901f commit e95c14e
Show file tree
Hide file tree
Showing 25 changed files with 89 additions and 21 deletions.
Binary file modified img/azimuthalEqualArea.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/azimuthalEquidistant.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/conicConformal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/conicEqualArea.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/conicEquidistant.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/equirectangular.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/gnomonic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/orthographic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/stereographic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/transverseMercator.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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"devDependencies": {
"canvas": "1",
"d3-format": "1",
"eslint": "2",
"package-preamble": "0.0",
"rollup": "0.33",
Expand Down
2 changes: 1 addition & 1 deletion src/projection/azimuthalEqualArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ azimuthalEqualAreaRaw.invert = azimuthalInvert(function(z) {

export default function() {
return projection(azimuthalEqualAreaRaw)
.scale(120)
.scale(124.75)
.clipAngle(180 - 1e-3);
}
4 changes: 2 additions & 2 deletions src/projection/azimuthalEquidistant.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {acos, sin, tau} from "../math";
import {acos, sin} from "../math";
import {azimuthalRaw, azimuthalInvert} from "./azimuthal";
import projection from "./index";

Expand All @@ -12,6 +12,6 @@ azimuthalEquidistantRaw.invert = azimuthalInvert(function(z) {

export default function() {
return projection(azimuthalEquidistantRaw)
.scale(480 / tau)
.scale(79.4188)
.clipAngle(180 - 1e-3);
}
3 changes: 2 additions & 1 deletion src/projection/conicConformal.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ export function conicConformalRaw(y0, y1) {
}

export default function() {
return conicProjection(conicConformalRaw);
return conicProjection(conicConformalRaw)
.scale(131.8);
}
4 changes: 2 additions & 2 deletions src/projection/conicEqualArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export function conicEqualAreaRaw(y0, y1) {

export default function() {
return conicProjection(conicEqualAreaRaw)
.scale(151)
.translate([480, 347]);
.scale(155.424)
.center([0, 33.6442]);
}
4 changes: 2 additions & 2 deletions src/projection/conicEquidistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export function conicEquidistantRaw(y0, y1) {

export default function() {
return conicProjection(conicEquidistantRaw)
.scale(128)
.translate([480, 280]);
.scale(131.154)
.center([0, 13.9389]);
}
4 changes: 2 additions & 2 deletions src/projection/equirectangular.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {pi} from "../math";
import projection from "./index";

export function equirectangularRaw(lambda, phi) {
Expand All @@ -8,5 +7,6 @@ export function equirectangularRaw(lambda, phi) {
equirectangularRaw.invert = equirectangularRaw;

export default function() {
return projection(equirectangularRaw).scale(480 / pi);
return projection(equirectangularRaw)
.scale(152.63);
}
2 changes: 1 addition & 1 deletion src/projection/gnomonic.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ gnomonicRaw.invert = azimuthalInvert(atan);

export default function() {
return projection(gnomonicRaw)
.scale(139)
.scale(144.049)
.clipAngle(60);
}
8 changes: 5 additions & 3 deletions src/projection/mercator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ mercatorRaw.invert = function(x, y) {
};

export default function() {
return mercatorProjection(mercatorRaw);
return mercatorProjection(mercatorRaw)
.scale(961 / tau);
}

export function mercatorProjection(project) {
Expand All @@ -31,12 +32,13 @@ export function mercatorProjection(project) {
m.clipExtent = function(_) {
if (!arguments.length) return clipAuto ? null : clipExtent();
if (clipAuto = _ == null) {
var k = pi * scale(), t = translate();
var k = pi * scale(),
t = translate();
_ = [[t[0] - k, t[1] - k], [t[0] + k, t[1] + k]];
}
clipExtent(_);
return m;
};

return m.clipExtent(null).scale(961 / tau);
return m.clipExtent(null);
}
2 changes: 1 addition & 1 deletion src/projection/orthographic.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ orthographicRaw.invert = azimuthalInvert(asin);

export default function() {
return projection(orthographicRaw)
.scale(240)
.scale(249.5)
.clipAngle(90 + epsilon);
}
2 changes: 1 addition & 1 deletion src/projection/stereographic.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ stereographicRaw.invert = azimuthalInvert(function(z) {

export default function() {
return projection(stereographicRaw)
.scale(240)
.scale(250)
.clipAngle(142);
}
3 changes: 2 additions & 1 deletion src/projection/transverseMercator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ export default function() {
return arguments.length ? rotate([_[0], _[1], _.length > 2 ? _[2] + 90 : 90]) : (_ = rotate(), [_[0], _[1], _[2] - 90]);
};

return rotate([0, 0, 90]);
return rotate([0, 0, 90])
.scale(159.155);
}
47 changes: 47 additions & 0 deletions test/compute-scale
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env node

var width = 960 - 1,
height = 500 - 1,
projectionName = process.argv[2],
projectionSymbol = "geo" + projectionName[0].toUpperCase() + projectionName.slice(1);

if (!/^[a-z0-9]+$/i.test(projectionName)) throw new Error;

var topojson = require("topojson"),
d3_format = require("d3-format"),
d3_geo = require("../");

var formatNumber = d3_format.format(".6");

var projection = d3_geo[projectionSymbol]()
.precision(0.01)
.scale(1)
.translate([0, 0])
.center([0, 0]);

if (projection.rotate) projection.rotate([0, 0]);

var land = {type: "Sphere"};

switch (projectionName) {
case "conicConformal":
case "stereographic": {
projection.clipAngle(90);
break;
}
}

var path = d3_geo.geoPath()
.projection(projection);

var bounds = path.bounds(land),
dx = bounds[1][0] - bounds[0][0],
dy = bounds[1][1] - bounds[0][1],
cx = (bounds[1][0] + bounds[0][0]) / 2,
cy = (bounds[1][1] + bounds[0][1]) / 2,
scale = Math.min(width / dx, height / dy);

console.log(`d3.${projectionSymbol}()
.scale(${formatNumber(scale)})
.center([${(projection.invert ? projection.invert([cx, cy]) : [0, 0]).map(formatNumber).join(", ")}]);
`);
2 changes: 1 addition & 1 deletion test/render-us
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var canvas = new Canvas(width, height),
var us = require("./data/us-10m.json");

var path = d3_geo.geoPath()
.projection(d3_geo[projectionSymbol]())
.projection(d3_geo[projectionSymbol]().precision(0.1))
.context(context);

context.fillStyle = "#fff";
Expand Down
22 changes: 19 additions & 3 deletions test/render-world
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,30 @@ var fs = require("fs"),
var canvas = new Canvas(width, height),
context = canvas.getContext("2d");

var world = require("./data/world-50m.json");
var world = require("./data/world-50m.json"),
graticule = d3_geo.geoGraticule(),
outline = {type: "Sphere"};

var graticule = d3_geo.geoGraticule();
// switch (projectionName) {
// case "littrow": outline = graticule.extent([[-90, -60], [90, 60]]).outline(); break;
// }

var path = d3_geo.geoPath()
.projection(d3_geo[projectionSymbol]().precision(0.1))
.context(context);

context.fillStyle = "#fff";
context.fillRect(0, 0, width, height);
context.save();

// switch (projectionName) {
// case "armadillo": {
// context.beginPath();
// path(outline);
// context.clip();
// break;
// }
// }

context.beginPath();
path(topojson.feature(world, world.objects.land));
Expand All @@ -36,8 +50,10 @@ path(graticule());
context.strokeStyle = "rgba(119,119,119,0.5)";
context.stroke();

context.restore();

context.beginPath();
path({type: "Sphere"});
path(outline);
context.strokeStyle = "#000";
context.stroke();

Expand Down

0 comments on commit e95c14e

Please sign in to comment.