Skip to content

Commit

Permalink
regular grids
Browse files Browse the repository at this point in the history
  • Loading branch information
neocarto committed Oct 14, 2024
1 parent 7fd1b4c commit 876ac0c
Show file tree
Hide file tree
Showing 90 changed files with 761 additions and 391 deletions.
25 changes: 23 additions & 2 deletions docs/container_create.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/container_draw.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/container_render.js.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/data/search.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/effect_blur.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/effect_clippath.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/effect_radialgradient.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/effect_shadow.js.html

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions docs/global.html

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions docs/grid_arbitrary.js.html

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions docs/grid_diamond.js.html

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions docs/grid_dot.js.html

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions docs/grid_h3.js.html

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions docs/grid_hexbin.js.html

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions docs/grid_make.js.html

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions docs/grid_square.js.html

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions docs/grid_triangle.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/helpers_tooltip.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/index.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_box.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_choro-horizontal.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_choro-vertical.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_circles-half.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_circles-nested.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_circles.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_mushrooms.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_spikes.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_squares-nested.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_squares.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_symbol-horizontal.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_symbol-vertical.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_typo-horizontal.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/legend_typo-vertical.js.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/mark_circle.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/mark_footer.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/mark_graticule.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/mark_halfcircle.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/mark_header.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/mark_north.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/mark_outline.js.html

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions docs/mark_path.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/mark_scalebar.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/mark_spike.js.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/mark_square.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/mark_symbol.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/mark_text.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/mark_tile.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/plot_plot.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/plot_plot_choro.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/plot_plot_prop.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/plot_plot_propchoro.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/plot_plot_proptypo.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/plot_plot_symbol.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/plot_plot_typo.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_addfonts.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_centroid.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_choro.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_dissolve.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_dodge.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_dotstogrid.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_featurecollection.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_geotable.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_height.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_merge.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_proj4d3.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_project.js.html

Large diffs are not rendered by default.

26 changes: 19 additions & 7 deletions docs/tool_radius.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_random.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_replicate.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_rewind.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_ridge.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_symbols.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_typo.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/tool_unproject.js.html

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"dicopal": "^0.6.4",
"docs": "^0.3.2-canary.0",
"documentation": "^14.0.3",
"h3-js": "^4.1.0",
"rollup": "^4.10.0",
"statsbreaks": "^1.0.6",
"versor": "^0.2.0"
Expand Down
14 changes: 14 additions & 0 deletions src/container/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { blur as addblur } from "../effect/blur.js";
import { shadow as addshadow } from "../effect/shadow.js";
import { radialGradient as addradialGradient } from "../effect/radialgradient.js";

import { make as addgrid } from "../grid/make.js";

import { circles_nested as addcircles_nested } from "../legend/circles-nested";
import { circles as addcircles } from "../legend/circles";
import { squares as addsquares } from "../legend/squares";
Expand All @@ -56,6 +58,7 @@ import { plot as addplot } from "../plot/plot.js";
* @see {@link https://observablehq.com/@neocartocnrs/geoviz}
*
* @property {number} [width = 1000] - width of the container.
* @property {number} [id = "map"] - id of the svg container.
* @property {number} [height] - height of the container. This value is automatically calculated according to `domain`. But it can be forced by entering a value.
* @property {object|object[]} [domain] - the domain corresponds to the geographical area to be displayed. It is defined by a geoJSON or an array containing geoJSONs.
* @property {function|string} [projection] - d3 function of projection. See [d3-geo](https://github.com/d3/d3-geo) & [d3-geo-projection](https://github.com/d3/d3-geo-projection). You can aslo write "mercator" to use tiles. (default: "none")
Expand All @@ -74,6 +77,7 @@ import { plot as addplot } from "../plot/plot.js";

export function create({
height = null,
id = "map",
//projection = d3.geoEquirectangular(),
projection = "none",
domain,
Expand Down Expand Up @@ -159,6 +163,7 @@ export function create({
.create2("svg")
.attr("width", width)
.attr("height", height)
.attr("id", id)
.attr("viewBox", [0, 0, width, height])
.style("background-color", background);
svg.append("defs").attr("id", "defs");
Expand Down Expand Up @@ -208,6 +213,14 @@ export function create({
})
);

let grid = {};
[{ id: "make", func: addgrid }].forEach(
(d) =>
(grid[d.id] = function () {
return d.func(output, arguments[0]);
})
);

let legend = {};
[
{ id: "circles_nested", func: addcircles_nested },
Expand Down Expand Up @@ -250,6 +263,7 @@ export function create({
return Object.assign(output, {
...mark,
legend,
grid,
effect,
render: function () {
return addrender(output, arguments[0]);
Expand Down
11 changes: 11 additions & 0 deletions src/grid/arbitrary.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { Delaunay } from "d3-delaunay";
const d3 = Object.assign({}, { Delaunay });

/**
* @function grid/arbitrary
* @description The `grid.arbitrary` function allows to create an arbitrary geoJSON grid in SVG coordinates.
* @see {@link https://observablehq.com/@neocartocnrs/regular-grids}
* @property {number} [step = 50] - step of the grid
* @property {number} [width = 1000] - width of the grid
* @property {number} [height = 500] - height of the grid
* @example
* geoviz.grid.arbitrary(50, 1000, 500)
*/

export function arbitrary(step = 50, width = 1000, height = 500) {
let grid = [];
let nb = Math.round((width / step) * (height / step));
Expand Down
10 changes: 10 additions & 0 deletions src/grid/diamond.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { range } from "d3-array";
const d3 = Object.assign({}, { range });

/**
* @function grid/diamond
* @description The `grid.diamond` function allows to create a diamond geoJSON grid in SVG coordinates.
* @see {@link https://observablehq.com/@neocartocnrs/regular-grids}
* @property {number} [step = 50] - step of the grid
* @property {number} [width = 1000] - width of the grid
* @property {number} [height = 500] - height of the grid
* @example
* geoviz.grid.diamond(50, 1000, 500)
*/
export function diamond(step = 50, width = 1000, height = 500) {
let size = step * Math.sqrt(2);

Expand Down
53 changes: 0 additions & 53 deletions src/grid/diamondgeo.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/grid/dot.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { range } from "d3-array";
const d3 = Object.assign({}, { range });

/**
* @function grid/dot
* @description The `grid.dot` function allows to create a geoJSON vith regular dots in SVG coordinates.
* @see {@link https://observablehq.com/@neocartocnrs/regular-grids}
* @property {number} [step = 50] - step of the grid
* @property {number} [width = 1000] - width of the grid
* @property {number} [height = 500] - height of the grid
* @example
* geoviz.grid.dot(50, 1000, 500)
*/
export function dot(step = 30, width = 1000, height = 500) {
// build grid
let y = d3.range(0 + step / 2, height, step).reverse();
Expand Down
46 changes: 0 additions & 46 deletions src/grid/dotgeo.js

This file was deleted.

31 changes: 31 additions & 0 deletions src/grid/h3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
getRes0Cells,
cellToChildren,
isPentagon,
cellToBoundary,
} from "h3-js";

/**
* @function grid/h3
* @description The `grid.h3` function allows to create a hexbin geoJSON grid in geographical coordinates.
* @see {@link https://observablehq.com/@neocartocnrs/regular-grids}
* @property {number} [level = 0] - level of the grid. Form 0 (large hexagons) to 4 (small hexagons). See: https://h3geo.org
* @example
* geoviz.grid.h3(1)
*/
export function h3(level = 0) {
return {
type: "FeatureCollection",
features: getRes0Cells()
.map((i) => cellToChildren(i, level))
.flat()
.map((d) => ({
type: "Feature",
properties: { id: d, pentagon: isPentagon(d) },
geometry: {
type: "Polygon",
coordinates: [cellToBoundary(d, true).reverse()],
},
})),
};
}
10 changes: 10 additions & 0 deletions src/grid/hexbin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { range, max } from "d3-array";
const d3 = Object.assign({}, { range, max });

/**
* @function grid/hexbin
* @description The `grid.hexbin` function allows to create a hexbin geoJSON grid in SVG coordinates.
* @see {@link https://observablehq.com/@neocartocnrs/regular-grids}
* @property {number} [step = 50] - step of the grid
* @property {number} [width = 1000] - width of the grid
* @property {number} [height = 500] - height of the grid
* @example
* geoviz.grid.hexbin(50, 1000, 500)
*/
export function hexbin(step = 50, width = 1000, height = 500) {
let w = step;
let size = w / Math.sqrt(3);
Expand Down
45 changes: 38 additions & 7 deletions src/grid/make.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
import { square } from "./square.js";
import { dot } from "./dot.js";
import { dotgeo } from "./dotgeo.js";
import { squaregeo } from "./squaregeo.js";
import { diamond } from "./diamond.js";
import { arbitrary } from "./arbitrary.js";
import { hexbin } from "./hexbin.js";
import { triangle } from "./triangle.js";
import { h3 } from "./h3.js";

export function make(svg, { step, type = "square" } = {}) {
/**
* @function grid/make
* @description The `grid.make` function allows to create a regular grid geoJSON. For all types, For all grid types (except "h3"), the function returns a geojson with svg coordinates in the layout of the page. For type "h3", the function returns geographic coordinates in latitude and longitude.
* @see {@link https://observablehq.com/@neocartocnrs/regular-grids}
*
* @property {string} [type = "square"] - Type of grid ("square", "dot", "diamond", "hexbin" (or "hex"), "trangle", "arbitrary" (or "randmon"), "h3" (or "h3geo", "hexgeo", "hexbingeo"))
* @property {number} [step = 50] - step of grids (except for "h3" type)
* @property {number} [level = 0] - level oh geographical hexbin grids ("h3" type only). Form 0 (large hexagons) to 4 (small hexagons). See: https://h3geo.org
* @example
* // There are several ways to use this function
* geoviz.grid.make(svg, { type:"diamond", step:100 }) // where svg is the container
* svg.grid.make({ type:"diamond", step:100 }) // where svg is the container
*/

export function make(svg, { step = 50, type = "square", level = 0 } = {}) {
switch (type) {
case "square":
return square(step, svg.width, svg.height);
break;
case "arbitrary":
case "random":
return arbitrary(step, svg.width, svg.height);
break;
case "dot":
return dot(step, svg.width, svg.height);
break;
case "dotgeo":
return dotgeo(step, svg.domain, svg.projection);
case "diamond":
return diamond(step, svg.width, svg.height);
break;
case "hexbin":
case "hex":
return hexbin(step, svg.width, svg.height);
break;
case "triangle":
return triangle(step, svg.width, svg.height);
break;
case "squaregeo":
return squaregeo(step, svg.domain, svg.projection);
case "h3":
case "h3geo":
case "hexgeo":
case "hexbingeo":
return h3(level);
break;
}
}
10 changes: 10 additions & 0 deletions src/grid/square.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { range } from "d3-array";
const d3 = Object.assign({}, { range });

/**
* @function grid/square
* @description The `grid.square` function allows to create a square geoJSON grid in SVG coordinates.
* @see {@link https://observablehq.com/@neocartocnrs/regular-grids}
* @property {number} [step = 50] - step of the grid
* @property {number} [width = 1000] - width of the grid
* @property {number} [height = 500] - height of the grid
* @example
* geoviz.grid.square(50, 1000, 500)
*/
export function square(step = 50, width = 1000, height = 500) {
// build grid
let y = d3.range(0 + step / 2, height, step).reverse();
Expand Down
Loading

0 comments on commit 876ac0c

Please sign in to comment.