Published
Edited
May 10, 2021
2 forks
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
features = {
const position = []
const elements = []
let count = 0;

for (const feature of countries.features) {
if (feature.id) {
let coordinates;
if (feature.geometry.type == "Polygon") {
coordinates = [feature.geometry.coordinates[0].map(([lon, lat]) => polarToCartesian(lon, lat, 1))]
} else if (feature.geometry.type == "MultiPolygon") {
coordinates = feature.geometry.coordinates.map(coord => coord[0].map(([lon, lat]) => polarToCartesian(lon, lat, 1)))
} else {
throw `All elements must be polygons or multipolgyons. ${feature.geometry}`
}

for (const coords of coordinates) {
const offset = position.length

position.push(...coords)

const range = d3.range(offset, offset + coords.length)
const [, ...tail] = range
elements.push(...d3.zip(range, tail).flat(1))
}

count++
}
}
return {
position: gl.regl.buffer(position),
elements: gl.regl.elements({
primitive: 'lines',
count: elements.length,
data: elements
}),
count,
}
}
Insert cell
data = {
const position = []
const cdata = []
const maxCO2 = d3.max(envData.filter(x => countries.features.some(f => f.properties.name === x.country)), x => +x.co2_per_capita)

for (const feature of countries.features) {
let coordinates;
if (feature.geometry.type == "Polygon") {
coordinates = feature.geometry.coordinates
} else if (feature.geometry.type == "MultiPolygon") {
coordinates = feature.geometry.coordinates[0]
} else {
throw `All elements must be polygons or multipolgyons. ${feature.geometry}`
}
const center = (() => {
const centers = coordinates.map(coords =>
[
d3.mean(coords, x => x[0]),
d3.mean(coords, x => x[1]),
]
)
return centers[0]
})();
if (feature.id) {
cdata.push({ center, id: +feature.id })
}
}
return {
position: gl.regl.buffer(cdata.map(x => polarToCartesian(x.center[0], x.center[1], 1 + 0.01))),
countryIndex: gl.regl.buffer({
data: cdata.map(x => {
const u = Math.floor(x.id / 32)
const v = x.id - (u * 32)
return [v / 32, u / 32]
})
}),
count: cdata.length,
}
}
Insert cell
Insert cell
Insert cell
drawCountry = gl.regl({
vert: `
precision mediump float;

uniform mat4 model, view, projection;

attribute vec3 position;

void main () {
gl_Position = projection * view * model * vec4(position, 1);
}
`,
frag: `
precision mediump float;

void main () {
gl_FragColor = vec4(0, 0, 0, 0.5);
}
`,
attributes: {
position: gl.regl.prop("position")
},
uniforms: {
model: gl.regl.prop("model"),
},
elements: gl.regl.prop("elements"),
})
Insert cell
drawCountryData = gl.regl({
vert: `
precision mediump float;

uniform mat4 model, view, projection;

attribute vec3 position;
attribute vec2 countryIndex;

varying vec2 uv;

void main () {
gl_PointSize = 4.;
uv = countryIndex;
gl_Position = projection * view * model * vec4(position, 1);
}
`,
frag: `
precision mediump float;

uniform sampler2D texture;

varying vec2 uv;

void main () {
gl_FragColor = texture2D(texture, uv);
}
`,
attributes: {
position: gl.regl.prop("position"),
countryIndex: gl.regl.prop("countryIndex"),
},
uniforms: {
model: gl.regl.prop("model"),
texture: gl.regl.prop("texture"),
},
primitive: 'points',
count: gl.regl.prop("count"),
})
Insert cell
setupCamera = gl.regl({
context: {
projection: (context, props) =>
mat4.perspective(
[],
Math.PI / 4,
context.viewportWidth / context.viewportHeight,
0.01,
4 * 0.92 * Math.sqrt(Math.pow(3 * Math.cos(props.time), 2) + Math.pow(3 * Math.sin(props.time), 2)),
),

view: (context, props) =>
mat4.lookAt(
[],
[
3 * Math.cos(props.time),
3 * Math.sin(props.time),
0,
],
[0, 0, 0],
[0, 0, 1],
)
},

uniforms: {
view: gl.regl.context('view'),
projection: gl.regl.context('projection')
}
})
Insert cell
function draw(time) {
gl.regl.clear({
color: [1, 1, 1, 1],
depth: 1,
})
setupCamera({ time }, () => {
const model = mat4.identity([])

const { position, elements } = features
drawCountry({
model,
position,
elements
})
drawCountryData({
model,
position: data.position,
countryIndex: data.countryIndex,
count: data.count,
texture: mkTexture(year),
})

// drawGraticule(graticule.map(points => ({
// model,
// position: points,
// count: points.length,
// })))
})
}
Insert cell
draw(now / 5000)
Insert cell
function polarToCartesian(lon0, lat0, R) {
const lat = lat0 * Math.PI / 180
const lon = lon0 * Math.PI / 180
const x = R * Math.cos(lat) * Math.cos(lon)
const y = R * Math.cos(lat) * Math.sin(lon)
const z = R * Math.sin(lat)

return [x, y, z];
}
Insert cell
interpolateColor = d3.interpolateYlOrRd
Insert cell
envData = d3.csvParse(await fetch("https://raw.githubusercontent.com/owid/co2-data/master/owid-co2-data.csv").then(res => res.text()))
Insert cell
Insert cell
countries = {
const world = await fetch("https://cdn.jsdelivr.net/npm/world-atlas@2/countries-10m.json")
.then(res => res.json())
return topojson.feature(world, world.objects.countries)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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