2019-05-29 19:21:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
2019-06-10 22:24:28 +00:00
|
|
|
import L from 'leaflet';
|
|
|
|
import { Delaunay } from 'd3-delaunay';
|
2019-06-11 13:00:59 +00:00
|
|
|
import chroma from 'chroma-js';
|
2019-05-29 20:06:00 +00:00
|
|
|
|
|
|
|
import Vector2 from '../Helpers/Vector2.mjs';
|
|
|
|
import Rectangle from '../Helpers/Rectangle.mjs';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates and manages a single voronoi SVGOverlay layer.
|
|
|
|
*/
|
2019-05-29 19:21:03 +00:00
|
|
|
class VoronoiOverlay {
|
|
|
|
constructor() {
|
2019-05-29 20:06:00 +00:00
|
|
|
this.border = new Vector2(0.1, 0.1); // lat / long
|
|
|
|
|
|
|
|
this.cells = [];
|
|
|
|
}
|
|
|
|
|
2019-06-11 11:42:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the list of cells in the voronoi overlay.
|
|
|
|
* @param {VoronoiCell[]} cells The cells to add, as an array.
|
|
|
|
*/
|
2019-06-11 12:45:26 +00:00
|
|
|
set_cells(cells) {
|
2019-06-11 11:42:59 +00:00
|
|
|
this.cells.length = 0;
|
2019-06-11 12:45:26 +00:00
|
|
|
this.add_cells(...cells);
|
2019-06-11 11:42:59 +00:00
|
|
|
}
|
2019-06-10 23:19:37 +00:00
|
|
|
/**
|
|
|
|
* Adds a cell to the voronoi overlay.
|
|
|
|
* @param {VoronoiCell} cells The cell to add. May be specified as many times as requires to add cells in bulk.
|
|
|
|
*/
|
2019-06-11 12:45:26 +00:00
|
|
|
add_cells(...cells) {
|
2019-05-29 20:06:00 +00:00
|
|
|
this.cells.push(...cells);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the bounding box of all the currently registered points.
|
|
|
|
* Includes a border, which is defined by this.border.
|
|
|
|
* @return {Rectangle} The bounding box of the currently registered points.
|
|
|
|
*/
|
|
|
|
computeBoundingBox() {
|
2019-06-10 22:24:28 +00:00
|
|
|
let result = {
|
|
|
|
x_min: Infinity,
|
|
|
|
x_max: -Infinity,
|
|
|
|
y_min: Infinity,
|
|
|
|
y_max: -Infinity
|
|
|
|
}
|
2019-05-29 20:06:00 +00:00
|
|
|
|
|
|
|
for(let cell of this.cells) {
|
2019-06-10 23:19:37 +00:00
|
|
|
if(cell.point.x < result.x_min) result.x_min = cell.point.x;
|
|
|
|
if(cell.point.x > result.x_max) result.x_max = cell.point.x;
|
|
|
|
if(cell.point.y < result.y_min) result.y_min = cell.point.y;
|
|
|
|
if(cell.point.y > result.y_max) result.y_max = cell.point.y;
|
2019-05-29 20:06:00 +00:00
|
|
|
}
|
2019-05-29 19:21:03 +00:00
|
|
|
|
2019-06-10 22:24:28 +00:00
|
|
|
result.x_min -= this.border.x;
|
|
|
|
result.y_min -= this.border.y;
|
|
|
|
result.x_max += this.border.x;
|
|
|
|
result.y_max += this.border.y;
|
2019-05-29 20:06:00 +00:00
|
|
|
|
2019-06-10 22:24:28 +00:00
|
|
|
return new Rectangle(
|
|
|
|
result.x_min,
|
|
|
|
result.y_min,
|
|
|
|
result.x_max - result.x_min,
|
|
|
|
result.y_max - result.y_min
|
|
|
|
);
|
2019-05-29 20:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let bounding_box = this.computeBoundingBox();
|
|
|
|
|
2019-06-10 22:24:28 +00:00
|
|
|
let voronoi = Delaunay.from(this.cells.map(
|
|
|
|
(cell) => [cell.point.x, cell.point.y])
|
|
|
|
).voronoi([
|
|
|
|
bounding_box.x, bounding_box.y,
|
|
|
|
bounding_box.Right, bounding_box.Bottom
|
|
|
|
]);
|
2019-05-29 20:06:00 +00:00
|
|
|
|
2019-06-10 22:24:28 +00:00
|
|
|
let i = 0;
|
|
|
|
for(let polygon of voronoi.cellPolygons()) {
|
|
|
|
let our_cell = this.cells[i];
|
|
|
|
our_cell.polygon = polygon.map((point) => new Vector2(...point));
|
|
|
|
|
|
|
|
i++;
|
2019-06-09 22:51:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 11:13:20 +00:00
|
|
|
let geojson = [];
|
2019-05-29 20:08:01 +00:00
|
|
|
|
|
|
|
// TODO: Render the SVG here
|
2019-06-10 22:24:28 +00:00
|
|
|
for(let cell of this.cells) {
|
2019-06-11 11:13:20 +00:00
|
|
|
if(cell.polygon == null) {
|
|
|
|
console.warn("Warning: Null cell polygon encountered.", cell);
|
|
|
|
continue;
|
2019-06-10 23:19:37 +00:00
|
|
|
}
|
2019-06-11 11:13:20 +00:00
|
|
|
|
|
|
|
geojson.push({
|
|
|
|
"type": "Feature",
|
|
|
|
"geometry": {
|
|
|
|
"type": "Polygon",
|
|
|
|
"coordinates": [cell.polygon.map((point) => [point.x, point.y])],
|
|
|
|
},
|
|
|
|
"properties": {
|
2019-06-11 12:45:26 +00:00
|
|
|
"colour": cell.colour == null ? "hsl(0, 100%, 100%)" : cell.colour
|
2019-06-11 11:13:20 +00:00
|
|
|
}
|
|
|
|
});
|
2019-06-10 22:24:28 +00:00
|
|
|
}
|
2019-06-11 11:13:20 +00:00
|
|
|
return geojson;
|
2019-06-10 22:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 12:45:26 +00:00
|
|
|
generate_layer() {
|
|
|
|
return L.geoJSON(this.render(), {
|
|
|
|
// FUTURE: If we want to be even moar fanceh, we can check out https://leafletjs.com/reference-1.5.0.html#path
|
|
|
|
style: (feature) => { return {
|
2019-06-11 13:00:59 +00:00
|
|
|
// Stroke
|
|
|
|
color: chroma(feature.properties.colour).darken(0.75).toString(),
|
|
|
|
|
|
|
|
// Fill
|
|
|
|
fillColor: feature.properties.colour,
|
2019-06-11 12:45:26 +00:00
|
|
|
fillOpacity: 0.4
|
|
|
|
} }
|
2019-06-11 11:13:20 +00:00
|
|
|
});
|
2019-05-29 19:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 20:06:00 +00:00
|
|
|
|
2019-05-29 19:21:03 +00:00
|
|
|
export default VoronoiOverlay;
|