Published
Edited
Nov 7, 2021
19 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
image = Object.assign(
await /*d3.image(
"https://neo.sci.gsfc.nasa.gov/servlet/RenderData?si=1778973&format=PNG&width=720&height=360",
{ crossOrigin: "anonymous" }
) */ FileAttachment(
"ndvi.png"
).image(),
{ style: "max-width:300px" }
)
Insert cell
height = 800
Insert cell
data = {
const width = image.naturalWidth,
height = image.naturalHeight;
const context = DOM.context2d(width, height, 1);
context.drawImage(image, 0, 0);
return context.getImageData(0, 0, width, height).data;
}
Insert cell
function value([lon, lat]) {
const x = Math.round(((lon + 180) / 360) * image.naturalWidth),
y = Math.round(((90 - lat) / 180) * image.naturalHeight),
i = x + y * image.naturalWidth;
const v = 255 - data[4 * i + 1]; // read the green channel
return v < 1 ? NaN : v / 255;
}
Insert cell
Insert cell
import { grid } from "@fil/gray-fuller-grid"
Insert cell
points = grid(N)
Insert cell
cells = d3.geoVoronoi(points).polygons()
Insert cell
Insert cell
color = d3.scaleSequential((t) => d3.interpolateGreens(1 - t))
Insert cell
projection = d3
.geoSatellite()
.rotate([360 * Math.random(), -45])
.fitExtent(
[
[10, 10],
[width - 10, height - 10]
],
{ type: "Sphere" }
)
Insert cell
merge = (f) => {
const topo = topojson.topology({ f });
return topojson.merge(topo, topo.objects.f.geometries);
}
Insert cell
// group by color and merge the cells for faster rendering
groups = d3.rollup(
d3.range(points.length),

(index) =>
merge({
type: "FeatureCollection",
features: index.map((i) => cells.features[i])
}),

(i) => Math.round(value(points[i]) * 10) / 10
)
Insert cell
function render(context, path) {
function draw() {
for (const [value, f] of groups) {
if (isNaN(value)) continue;
context.beginPath();
path(f);
context.fillStyle = color(value);
context.fill();
}
}

// dark blue sphere
context.fillStyle = "#000";
context.fillRect(0, 0, width, height);

context.beginPath();
path({ type: "Sphere" });
context.fillStyle = "#003";
context.fill();

const clipAngle = projection.clipAngle();
context.globalAlpha = 0.3;
projection.clipAngle(180);
draw();

// front globe
context.globalAlpha = 1;
projection.clipAngle(clipAngle);
draw();
}
Insert cell
Insert cell
d3 = require("d3@7", "d3-geo-projection@4", "d3-geo-voronoi@2", "d3-inertia@0.2")
Insert cell
topojson = require("topojson-server@3", "topojson-client@3")
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