Public
Edited
Jun 7, 2023
1 fork
Importers
4 stars
Insert cell
Insert cell
topojson.feature(n2j, n2j.objects.cntrg).features
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
w = 600
Insert cell
h = 600
Insert cell
Insert cell
geoExtent = {
return {
type: "MultiPoint",
coordinates: [
[3000000, 1200000],
[6000000, 5500000]
]
};
}
Insert cell
Insert cell
projection = d3.geoIdentity().reflectY(true).fitSize([w, h], geoExtent)
Insert cell
Insert cell
pixSizeM = {
const p0 = projection([0, 0]);
const p1 = projection([1, 0]);
return 1 / (p1[0] - p0[0]);
}
Insert cell
path = d3.geoPath().projection(projection)
Insert cell
Insert cell
Insert cell
n2j = (
await fetch(
"https://raw.githubusercontent.com/eurostat/Nuts2json/master/pub/v1/2021/3035/60M/0.json"
)
).json()
Insert cell
Insert cell
Insert cell
Insert cell
nutsrg = topojson.feature(n2j, n2j.objects.nutsrg).features.filter((r) => {
for (let cc of eu) if (r.properties.id == cc) return true;
return false;
})
Insert cell
nutsbn = topojson.feature(n2j, n2j.objects.nutsbn).features
Insert cell
Insert cell
import { grid20000 as db, getPosition } from "@jgaffuri/eurpopgrid"
Insert cell
resolution = 20000
Insert cell
year = 2021
Insert cell
Insert cell
cells = {
const where = eu.map((cc) => "CNTR_ID LIKE '%" + cc + "%'").join(" OR ");
return db.query("SELECT * FROM data WHERE " + where);
}
Insert cell
Insert cell
cellsF = cells
.map((r) => {
//geo position
const posGeo = getPosition(r.GRD_ID);
//screen position
const posPix = projection([posGeo.x, posGeo.y]);
return { pop: r["TOT_P_" + year], posPix: posPix, posGeo: posGeo };
})
.filter((r) => r.pop > 0)
Insert cell
Insert cell
Insert cell
v = {
const a = (Math.PI * angleDeg) / 180;
return [Math.cos(a), Math.sin(a)];
}
Insert cell
Insert cell
pTop = {
const w2 = widthKM * 500;
return [positionX - v[1] * w2, positionY + v[0] * w2];
}
Insert cell
Insert cell
pBottom = {
const w2 = widthKM * 500;
return [positionX + v[1] * w2, positionY - v[0] * w2];
}
Insert cell
Insert cell
line = {
const fun = (t) => [positionX + t * v[0], positionY + t * v[1]];
const t = 30000000;
return [projection(fun(-t)), projection(fun(t))];
}
Insert cell
Insert cell
isWithinStripe = (cell) => {
//check if cell is below bottom line
if (cp(pBottom, cell) < 0) return false;
//check if cell is above top line
if (cp(pTop, cell) > 0) return false;
return true;
}
Insert cell
Insert cell
cp = (p, cell) => {
const x0 = p[0],
y0 = p[1];
const x1 = x0 + 10000 * v[0],
y1 = y0 + 10000 * v[1];
//cell center point
const x2 = cell.posGeo.x + resolution / 2,
y2 = cell.posGeo.y + resolution / 2;
return (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0);
}
Insert cell
Insert cell
popShare = (fun) => {
let popStripe = 0;
let popTotal = 0;

for (let c of cellsF) {
if (fun(c)) popStripe += c.pop;
popTotal += c.pop;
}

return [popStripe, popTotal];
}
Insert cell
popStripe = popShare(isWithinStripe)
Insert cell
Insert cell
share = popStripe[0] / popStripe[1]
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more