geostyler
Version:
Framework for styling geodata
153 lines (152 loc) • 4.48 kB
JavaScript
var w = Object.defineProperty;
var $ = (y, e, s) => e in y ? w(y, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : y[e] = s;
var z = (y, e, s) => $(y, typeof e != "symbol" ? e + "" : e, s);
import v from "color";
import d from "./SymbolizerUtil.js";
import { scale as L, limits as x } from "chroma-js";
import P from "../_virtual/isNil.js";
const o = class o {
static getDistinctValues(e, s) {
var i;
const t = [], r = (i = e == null ? void 0 : e.exampleFeatures) == null ? void 0 : i.features;
return r && r.forEach((n) => {
var m;
const l = (m = n == null ? void 0 : n.properties) == null ? void 0 : m[s];
!P(l) && !t.includes(l) && t.push(l);
}), t;
}
static guessSymbolizerFromData(e) {
var t, r, i, n;
switch ((n = (i = (r = (t = e == null ? void 0 : e.exampleFeatures) == null ? void 0 : t.features) == null ? void 0 : r[0]) == null ? void 0 : i.geometry) == null ? void 0 : n.type) {
case "Point":
case "MultiPoint":
return "Mark";
case "LineString":
case "MultiLineString":
return "Line";
case "Polygon":
case "MultiPolygon":
return "Fill";
case "GeometryCollection":
return "Mark";
default:
return "Mark";
}
}
static generateColors(e, s, t = "hsl") {
try {
return L(e).mode(t).colors(s);
} catch {
return;
}
}
static generateRules(e) {
var k;
const {
data: s,
levelOfMeasurement: t,
attributeName: r,
colors: i,
colorSpace: n,
symbolizerKind: l,
wellKnownName: m,
classificationMethod: f
} = e;
let u = e.numberOfRules, h = o.generateColors(i, u, n), F = [];
if (t === "nominal") {
const b = o.getDistinctValues(s, r);
b.splice(u, b.length - 2), F = b.map((g, c) => {
const a = ["==", r, g], p = d.generateSymbolizer(l, {
color: h[c],
wellKnownName: m
});
return {
name: g,
filter: a,
symbolizers: [p]
};
});
} else if (t === "cardinal" && f) {
const b = (k = s == null ? void 0 : s.exampleFeatures) == null ? void 0 : k.features, g = b ? b.map((a) => {
var p;
return (p = a == null ? void 0 : a.properties) == null ? void 0 : p[r];
}) : [];
let c = [];
switch (f) {
case "equalInterval":
c = o.getRanges(g, u, "e");
break;
case "quantile":
c = o.getRanges(g, u, "q");
break;
case "logarithmic":
c = o.getRanges(g, u, "l");
break;
case "kmeans":
c = o.getRanges(g, u, "k"), u = c.length, h = o.generateColors(i, u, n);
break;
}
F = c.map((a, p) => {
const S = p === c.length - 1, C = [
"&&",
[">=", r, a[0]],
[S ? "<=" : "<", r, a[1]]
], M = d.generateSymbolizer(l, {
color: h[p],
wellKnownName: m
});
return {
name: `${r} ${a[0]} - ${a[1]}`,
filter: C,
symbolizers: [M]
};
});
}
return F;
}
/**
* Get Ranges for quantile spread statistic.
*
* @param {number[]} series The data values.
* @param {number} numberOfClasses The number of classes to generate.
* @param {'e'|'q'|'l'|'k'} mode The mode in which the ranges should be generated:
* - e: equidistant
* - q: quantile
* - l: logarihtmic
* - k: k-means
*/
static getRanges(e, s, t = "e") {
const r = x(e, t, s);
return o.boundsToRanges(r);
}
/**
* Inspired by GeoStats.js: http://www.intermezzo-coop.eu/mapping/geostats/
*
* @param {number[]} bounds An array of class boundary values.
*/
static boundsToRanges(e) {
const s = [];
for (let t = 0; t < e.length - 1; t++)
s[t] = [e[t], e[t + 1]];
return s;
}
};
/**
* Generates a style object (CSS) from an array of color strings.
*
* @param {string[]} colors An array of color strings.
* @return {Object} An style object (CSS).
*/
z(o, "generateBackgroundStyleFromColors", (e) => {
const t = e.map((l) => `linear-gradient(${l}, ${l})`).join(","), i = e.map((l, m) => `${(m + 1) * (100 / e.length)}% 100%`).join(","), n = v(e[0]).isLight() ? "#000000" : "#FFFFFF";
return {
backgroundImage: t,
backgroundSize: i,
backgroundRepeat: "no-repeat",
color: n
};
});
let R = o;
export {
R as default
};