Public
Edited
Jan 20
Importers
14 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
goode = lobes => `
// inverse interrupted homolosine projection
float lambdamax = 180.0;
float lambdamin = -180.0;

float sinuMollweidePhi = 0.7109889596207567;
float sinuMollweideY = 0.0528035274542;
float cy = sqrt(2.0);
float cx = cy / halfPi;
float y1;

if (abs(y) > sinuMollweidePhi) {
// mollweide invert
float y0 = y + sign(y) * sinuMollweideY;
y1 = asin(y0 / cy);
float y2 = (2.0 * y1 + sin(2.0 * y1)) / pi;
if (abs(y2) < 1.0) {
phi = asin(y2);
} else {
transparent = true;
}
} else {
// sinusoidal invert
lambda = x / cos(y);
y1 = y;
phi = y;
}

float deg2 = 0.0;
float deg = x * degrees;

// a vec4 can hold up to 4 lobes (in the usual Goode projections, we need only 3).
vec4 n, m;

// north lobes
${
!lobes
? ""
: `
if (y > 0.0) {
${lobes[0]
.map(
(d, i) =>
`m[${i}] = ${d[1][0].toFixed(1)}; ` +
`n[${i}] = ${d[2][0].toFixed(1)};`
)
.join("\n ")}
}
// south lobes
else {
${lobes[1]
.map(
(d, i) =>
`m[${i}] = ${d[1][0].toFixed(1)}; ` +
`n[${i}] = ${d[2][0].toFixed(1)};`
)
.join("\n ")}
}`
}

float b, a = -180.0;
for (int i = 0; i < 4; i++) {
b = a;
a = n[i];
if (deg >= b && deg < a) {
lambdamax = a;
lambdamin = b;
deg2 = m[i];
}
}

if (abs(y) > sinuMollweidePhi) {
x -= (1.0 - cx * cos(y1)) * deg2 * radians;
lambda = x / (cx * cos(y1));
} else {
x -= (1.0 - cos(phi)) * deg2 * radians;
lambda = x / cos(y);
}

if (lambda * degrees > lambdamax
|| lambda * degrees < lambdamin
|| abs(phi * degrees) > 90.0)
transparent = true;
`
Insert cell
Insert cell
projection = d3
.geoInterrupt(d3.geoHomolosineRaw, [
[
// northern hemisphere
[[-180, 0], [-130, 90], [-90, 0]],
[[-90, 0], [-30, 90], [60, 0]],
[[60, 0], [120, 90], [180, 0]]
],
[
// southern hemisphere
[[-180, 0], [-120, -90], [-60, 0]],
[[-60, 0], [20, -90], [100, 0]],
[[100, 0], [140, -90], [180, 0]]
]
])
.rotate([-200, 0])
.precision(0.1)
Insert cell
Insert cell
scaleExtent = (height, [0.8 * projection.scale(), 8 * projection.scale()])
Insert cell
zoom = (projection) =>
d3
.zoom()
.scaleExtent(scaleExtent)
.on("zoom", (event) =>
projection
.scale(event.transform.k)
.translate([event.transform.x, event.transform.y])
)
Insert cell
Insert cell
maxTextureSize = {
const gl = document.createElement("canvas").getContext("webgl");
return gl.getParameter(gl.MAX_TEXTURE_SIZE);
}
Insert cell
image = {
// https://neo.gsfc.nasa.gov/view.php?datasetId=MY1DMM_CHLORA
yield Object.assign(await FileAttachment("low-res.jpg").image(), {
style: "max-height: 100px"
});

if (false)
yield Object.assign(
await d3.image(
"https://neo.gsfc.nasa.gov/servlet/RenderData?si=1964974&cs=rgb&format=JPEG&width=3600&height=1800",
{ crossOrigin: "anonymous" }
),
{ style: "max-height: 100px" }
);
}
Insert cell
// the image must be "power of 2" to use a mipmap
imageP2 = {
const w = Math.min(
maxTextureSize,
2 ** Math.ceil(Math.log(image.naturalWidth) / Math.log(2))
), // better upscale if possible
h = w / 2;
const context = DOM.context2d(w, h, 1);
context.drawImage(image, 0, 0, w, h);

return Object.assign(context.canvas, { style: "max-height: 100px;" });
}
Insert cell
Insert cell
createREGL = require("regl")
Insert cell
reglCanvas = {
const pixelRatio = Math.min(1.5, devicePixelRatio);

const canvas = document.createElement("canvas");
canvas.width = Math.floor(width * pixelRatio);
canvas.height = Math.floor(height * pixelRatio);
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;

const regl = createREGL({
canvas,
attributes: { antialias: false, preserveDrawingBuffer: true },
optionalExtensions: ['OES_standard_derivatives', 'EXT_shader_texture_lod']
});

return Object.assign(canvas, { value: regl, style: "max-height: 100px;" });
}
Insert cell
viewof regl = reglCanvas
Insert cell
glproj = goode(projection.lobes())
Insert cell
createDrawCommand = regl =>
regl({
frag: fragmentShader(glproj),

vert: vertexShader(),

attributes: {
position: [-4, 0, 0, -4, 4, 4]
},
count: 3,

uniforms: {
texture,
u_scale: () => projection.scale(),
u_angle: () => projection.angle(),
u_translate: () => {
// accounts for projection.center()
const r = projection.rotate(),
t = projection.rotate([0, 0])([0, 0]);
projection.rotate(r);
return t;
},
u_rotate: () => projection.rotate(),
u_size: () => [width, height]
},

depth: { enable: false }
})
Insert cell
texture = regl.texture({
data: imageP2,
mipmap: "nice",
min: "linear mipmap linear",
mag: "linear",
wrapS: "repeat",
flipY: true
})
Insert cell
Insert cell
fragmentShader = projection => `

#ifdef GL_EXT_shader_texture_lod
#extension GL_EXT_shader_texture_lod : enable
#endif

#ifdef GL_OES_standard_derivatives
#extension GL_OES_standard_derivatives : enable
#endif

precision highp float;
uniform sampler2D texture;
uniform vec3 u_rotate;

const float pi = 3.141592653589793;
const float halfPi = pi * 0.5;
const float tau = pi * 2.0;
const float radians = pi / 180.0;
const float degrees = 1.0 / radians;

varying vec2 p;

${preamble()}

${applyRotation()}

void main(void) {
float x = p.x;
float y = p.y;
float lambda, phi;

bool transparent = false;

// inverse projection
${projection}

// rotate
applyRotation(u_rotate, lambda, phi);

// texture coordinates
vec2 t = vec2(fract(lambda / tau - 0.5), phi / pi + 0.5);
${postprojection()}

#ifdef GL_OES_standard_derivatives
// avoid a mipmap seam by controlling the derivative of t.x
if (fwidth(t.x) > 0.25 && t.x < 0.5) t.x += 1.0;
#endif

// read the textures
#ifdef GL_EXT_shader_texture_lod
float scale = 0.8 * cos(phi * 0.97);
gl_FragColor = texture2DGradEXT(texture, vec2(t.x, t.y * 0.996 + 0.002), dFdx(t) * scale, dFdy(t) * scale);
#else
gl_FragColor = texture2D(texture, t);
#endif

if (transparent) gl_FragColor.a = 0.0;

}
`
Insert cell
Insert cell
vertexShader = () => `
precision highp float;
uniform float u_scale;
uniform float u_angle;
uniform vec2 u_translate;
uniform vec2 u_size;
attribute vec2 position;
varying vec2 p;

vec2 rotate2d(vec2 p, float a) {
float s = sin(a), c = cos(a);
return mat2(c, -s, s, c) * p;
}

void main () {
p = u_translate - 0.5 * u_size * (position * vec2(1, -1) + 1.0);
p = rotate2d(p, -u_angle * 0.017453292519943295) / u_scale * vec2(-1, 1);
gl_Position = vec4(position, 0, 1);
}`
Insert cell
Insert cell
applyRotation = () => `

// rotations, ported from d3-geo

void applyRotation(in vec3 rotate, inout float lambda, inout float phi) {
float x, y, rho, c, cosphi, z, deltaLambda, deltaPhi, deltaGamma, cosDeltaPhi,
sinDeltaPhi, cosDeltaGamma, sinDeltaGamma, k, circle, proj, a, b;

cosphi = cos(phi);
x = cos(lambda) * cosphi;
y = sin(lambda) * cosphi;
z = sin(phi);

deltaLambda = rotate.x * radians;
deltaPhi = rotate.y * radians;
deltaGamma = rotate.z * radians;

cosDeltaPhi = cos(deltaPhi);
sinDeltaPhi = sin(deltaPhi);
cosDeltaGamma = cos(deltaGamma);
sinDeltaGamma = sin(deltaGamma);

k = z * cosDeltaGamma - y * sinDeltaGamma;

lambda = atan(y * cosDeltaGamma + z * sinDeltaGamma,
x * cosDeltaPhi + k * sinDeltaPhi)
- deltaLambda;
k = k * cosDeltaPhi - x * sinDeltaPhi;

k = clamp(k, -1.0, 1.0); // avoid a hole at the poles
phi = asin(k);
}
`
Insert cell
preamble = () => `` // a way to insert more functions, see Bertin1953
Insert cell
postprojection = () => `
// example: apply webmercator
// t.y = 0.5 + log(tan(clamp(halfPi + phi, 0.0001, pi - 0.0001) * 0.5)) / pi / 2.0;
`
Insert cell
Insert cell
height = {
// fullscreen?
if (width > 1000)
return window.screen ? window.screen.height + 10 : width * .75;

const [[x0, y0], [x1, y1]] = d3
.geoPath(projection.fitWidth(width, { type: "Sphere" }))
.bounds({ type: "Sphere" });
const dy = Math.ceil(y1 - y0),
l = Math.min(Math.ceil(x1 - x0), dy);
projection.scale((projection.scale() * (l - 1)) / l).precision(0.2);
return dy;
}
Insert cell
projection.fitExtent([[10, 10], [width - 10, height - 10]], { type: "Sphere" })
Insert cell
d3 = require("d3@7", "d3-geo-projection@3")
Insert cell
import { checkbox, select } from "@jashkenas/inputs"
Insert cell
import {fullscreen} from "@fil/fullscreen"
Insert cell
fullscreen(canvas)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more