Published
Edited
Sep 26, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
class GeoDictionary {
constructor(decimals = 0) {
this.resolution = 10 ** decimals;
}
add(x, y, val) {
const key = [Math.floor(x * this.resolution), Math.floor(y * this.resolution)];
const tile = this[key];
// Append to tile
if (tile) {
tile.push([x, y, val]);
} else {
this[key] = [[x, y, val]];
}
}
getClosest(x, y) {
const xKey = Math.floor(x * this.resolution);
const yKey = Math.floor(y * this.resolution);
let minDist = Infinity;
let val = null;
// Iterate over a 3x3 grid
for (let i = xKey - 1; i <= xKey + 1; i++) {
for (let j = yKey - 1; j <= yKey + 1; j++) {
const tile = this[[i, j]]; // tile
if(!tile) continue; // If value not set
tile.forEach(t => {
const deltaX = t[0] - x;
const deltaY = t[1] - y;
const dist = deltaX * deltaX + deltaY * deltaY; // Compare squared distance
if (dist < minDist) {
minDist = dist;
val = t[2];
}
});
}
}
return val;
}
}
Insert cell
Insert cell
Insert cell
class GeoDictionaryViz {
constructor(decimals = 0) {
this.resolution = 10 ** decimals;
}
add(x, y, val) {
const key = [Math.floor(x * this.resolution), Math.floor(y * this.resolution)];
const tile = this[key];
// Append to tile
if (tile) {
tile.push([x, y, val]);
} else {
this[key] = [[x, y, val]];
}
}
getClosest(x, y) {
const t0 = performance.now();
const xKey = Math.floor(x * this.resolution);
const yKey = Math.floor(y * this.resolution);
let minDist = Infinity;
let val = null;
let iterations = 0;
const visited = [];
// Iterate over a 3x3 grid
for (let i = xKey - 1; i <= xKey + 1; i++) {
for (let j = yKey - 1; j <= yKey + 1; j++) {
const tile = this[[i, j]]; // tile
if(!tile) continue; // If value not set
tile.forEach(t => {
iterations++; // Add an iteration
const deltaX = t[0] - x;
const deltaY = t[1] - y;
const dist = deltaX * deltaX + deltaY * deltaY;
if(dist > 1 * this.resolution) return;
visited.push(t[2]);
if (dist < minDist) {
minDist = dist;
val = t[2];
}
});
}
}
const t1 = performance.now();
return {
iterations,
visited,
targeted: val,
performance: (t1 - t0),
};
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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