geostyler
Version:
Framework for styling geodata
163 lines (162 loc) • 4.7 kB
JavaScript
var v = Object.defineProperty;
var L = (y, e, s) => e in y ? v(y, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : y[e] = s;
var d = (y, e, s) => L(y, typeof e != "symbol" ? e + "" : e, s);
import x from "color";
import F from "./SymbolizerUtil.js";
import { scale as O, limits as P } from "chroma-js";
import T from "../_virtual/isNil.js";
const i = class i {
static getDistinctValues(e, s) {
var l;
const t = [], r = (l = e == null ? void 0 : e.exampleFeatures) == null ? void 0 : l.features;
return r && r.forEach((o) => {
var m;
const n = (m = o == null ? void 0 : o.properties) == null ? void 0 : m[s];
!T(n) && !t.includes(n) && t.push(n);
}), t;
}
static guessSymbolizerFromData(e) {
var t, r, l, o;
switch ((o = (l = (r = (t = e == null ? void 0 : e.exampleFeatures) == null ? void 0 : t.features) == null ? void 0 : r[0]) == null ? void 0 : l.geometry) == null ? void 0 : o.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 O(e).mode(t).colors(s);
} catch {
return;
}
}
static generateRules(e) {
var S;
const {
data: s,
levelOfMeasurement: t,
attributeName: r,
colors: l,
colorSpace: o,
symbolizerKind: n,
wellKnownName: m,
classificationMethod: z,
elseRule: k
} = e;
let c = e.numberOfRules;
const R = k ? c + 1 : c;
let f = i.generateColors(l, R, o), h = [];
if (t === "nominal") {
const b = i.getDistinctValues(s, r);
b.splice(c, b.length - 2), h = b.map((g, u) => {
const a = ["==", r, g], p = F.generateSymbolizer(n, {
color: f[u],
wellKnownName: m
});
return {
name: g,
filter: a,
symbolizers: [p]
};
});
} else if (t === "cardinal" && z) {
const b = (S = s == null ? void 0 : s.exampleFeatures) == null ? void 0 : S.features, g = b ? b.map((a) => {
var p;
return (p = a == null ? void 0 : a.properties) == null ? void 0 : p[r];
}) : [];
let u = [];
switch (z) {
case "equalInterval":
u = i.getRanges(g, c, "e");
break;
case "quantile":
u = i.getRanges(g, c, "q");
break;
case "logarithmic":
u = i.getRanges(g, c, "l");
break;
case "kmeans":
u = i.getRanges(g, c, "k"), c = u.length, f = i.generateColors(l, c, o);
break;
}
h = u.map((a, p) => {
const M = p === u.length - 1, w = [
"&&",
[">=", r, a[0]],
[M ? "<=" : "<", r, a[1]]
], $ = F.generateSymbolizer(n, {
color: f[p],
wellKnownName: m
});
return {
name: `${r} ${a[0]} - ${a[1]}`,
filter: w,
symbolizers: [$]
};
});
}
return k && h.push({
name: "Else",
elseRule: !0,
symbolizers: [F.generateSymbolizer(n, {
color: f[R - 1],
wellKnownName: m
})]
}), h;
}
/**
* 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 = P(e, t, s);
return i.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).
*/
d(i, "generateBackgroundStyleFromColors", (e) => {
const t = e.map((n) => `linear-gradient(${n}, ${n})`).join(","), l = e.map((n, m) => `${(m + 1) * (100 / e.length)}% 100%`).join(","), o = x(e[0]).isLight() ? "#000000" : "#FFFFFF";
return {
backgroundImage: t,
backgroundSize: l,
backgroundRepeat: "no-repeat",
color: o
};
});
let C = i;
export {
C as default
};